add readme and rename main.cpp

This commit is contained in:
Sandipsinh Rathod 2024-10-11 19:36:01 -04:00
parent d8a8aeeaf8
commit 85c6caa5ff
No known key found for this signature in database
3 changed files with 53 additions and 25 deletions

@ -3,4 +3,4 @@ project(cmpsc330hw2)
set(CMAKE_CXX_STANDARD 11)
add_executable(cmpsc330hw2 main.cpp)
add_executable(cmpsc330hw2 dotsandboxes.cxx)

26
README.txt Normal file

@ -0,0 +1,26 @@
Name: Sandipsinh Rathod
Email: sdr5549@psu.edu
Due Date: 11/10/24 11:59 PM
Class: CMPSC 330
Dots and Boxes:
Task:
This program will simulate the game of Dots and Boxes.
The game is played on a grid of dots. The players take turns connecting two horizontally or vertically adjacent dots.
If a player completes the fourth side of a 1x1 box, they get a point.
The game ends when we get an input "END".
The player(s) with the most points wins/ties.
Command used to compile:
g++ -ansi -pedantic -Wall dotsandboxes.cxx -o dots-and-boxes && ./dots-and-boxes hw2.txt
Note:
1. Although we are not allowed to use STL, I talked to the professor and he allowed me to use it for throwing errors.
2. As we needed to use recursion, I talked to Dr. Na and he said that my recursive function `reverseString` is fine.
Logic:
1. First, we will take the input from the file and serialize it.
2. Then we will iterate over the moves array and check if the element creates box(es).
3. If box(es) is/are completed, we will increment the score of the player.
4. Finally, we will print the score of the players.

@ -194,7 +194,7 @@ Moves serialize(char *s) {
char *player = static_cast<char *>(alloc(4 * sizeof(char)));
int x, y;
while (iss >> player >> x >> y) {
if (player == "END") break;
if (!strcmp(player, "END")) break;
// TODO: move this exceptions to runtime checks
if (x >= result.x || y >= result.y) {
@ -209,7 +209,9 @@ Moves serialize(char *s) {
}
if (!isValidPlayer(player[0])) {
string err = "Invalid player: " + player[0];
string err = "Invalid player: ";
err.push_back(player[0]);
throw invalid_argument(err);
}
Move move;
@ -302,7 +304,7 @@ int normalize(char c) {
// Print the scores of the players
void printScores(int *points, char blacklist) {
myPair<char, myPair<int, bool> > *arr = static_cast<myPair<char, myPair<int, bool> > *>(alloc(
26 * sizeof(myPair<char, myPair<int, bool> >)));
26 * sizeof(myPair<char, myPair<int, bool> >)));
for (int i = 0; i < 26; i++) {
myPair<char, myPair<int, bool> > p;
@ -330,8 +332,8 @@ void printScores(int *points, char blacklist) {
for (int i = 0; i < 26; i++) {
if (arr[i].second.first != -1) {
pt "Player " << arr[i].first << " has " << arr[i].second.first << (arr[i].second.first < 2
? " box"
: " boxes");
? " box"
: " boxes");
if (arr[i].second.first == maxScore) {
if (maxCount > 1) {
pt " (tie)";
@ -355,11 +357,11 @@ bool isValidMove(int x, int y, char **board) {
// Add a point to the player and update the board
void addPoint(
int *points,
char **board,
int x,
int y,
char player
int *points,
char **board,
int x,
int y,
char player
) {
if (board[x][y] == ' ') {
if (points[normalize(player)] == -1) {
@ -373,13 +375,13 @@ void addPoint(
// check for any box on the top or bottom of the current move
void solveVertical(
int moveX,
int moveY,
char **board,
int *points,
int x,
int y,
char player
int moveX,
int moveY,
char **board,
int *points,
int x,
int y,
char player
) {
// check for any box below current move
if (moveX < x - 2 && board[moveX + 2][moveY] != ' ' && board[moveX + 1][moveY - 1] != ' ') {
@ -400,12 +402,12 @@ void solveVertical(
// check for any box on the left or right of the current move
void solveHorizontal(
int moveX,
int moveY,
char **board,
int *points,
int y,
char player
int moveX,
int moveY,
char **board,
int *points,
int y,
char player
) {
if (moveY) {
if (moveY == y - 1) {
@ -425,7 +427,7 @@ void solveHorizontal(
}
} else {
if (board[moveX][moveY + 2] != ' ' && (board[moveX + 1][moveY + 1] != ' ' && board[moveX - 1][moveY + 1] !=
' ')) {
' ')) {
addPoint(points, board, moveX, moveY + 1, player);
}
}