diff --git a/main.cpp b/main.cpp index cb35210..8b8e9e4 100644 --- a/main.cpp +++ b/main.cpp @@ -176,19 +176,25 @@ Moves serialize(char *s) { result.x = (result.x << 1) - 1; result.y = (result.y << 1) - 1; - string player; + char *player = static_cast(alloc(4 * sizeof(char))); int x, y; while (iss >> player >> x >> y) { if (player == "END") break; // TODO: move this exceptions to runtime checks if (x >= result.x || y >= result.y) { - string err = "Invalid move by: " + player + " (" + toString(x) + ", " + toString(y) + ")"; + string err = "Invalid move by: "; + err.append(player); + err.append(" at (x, y): ("); + err.append(toString(x)); + err.append(", "); + err.append(toString(y)); + err.append(")"); throw invalid_argument(err); } if (!isValidPlayer(player[0])) { - string err = "Invalid player: " + player; + string err = "Invalid player: " + player[0]; throw invalid_argument(err); } Move move; @@ -198,6 +204,7 @@ Moves serialize(char *s) { result.moves.push_back(move); } + dealloc(player); return result; } @@ -249,7 +256,7 @@ void printBoard(char **board, int x, int y) { // Print the board with row numbers for (int row = 0; row < x; ++row) { - if (row % 10 == 0) { + if (!(row % 10)) { pt row / 10; } else { pt " "; @@ -362,14 +369,6 @@ void solveVertical( int y, char player ) { - // TODO: fix ambiguity: - // - - // | - // - - // in the condition above, we do not know what to do at the end of the board - - // need to check for x-1, x+1, y-1, y+1 - // check for any box below current move if (moveX < x - 2 && board[moveX + 2][moveY] != ' ' && board[moveX + 1][moveY - 1] != ' ') { int rightEdge = moveY == y - 1 ? 1 : board[moveX + 1][moveY + 1] != ' '; @@ -387,6 +386,7 @@ void solveVertical( } } +// check for any box on the left or right of the current move void solveHorizontal( int moveX, int moveY, @@ -419,6 +419,7 @@ void solveHorizontal( } } +// Solve the game void solve(Moves moves, char **board) { int *points = static_cast(alloc(26 * (sizeof(int)))); for (int i = 0; i < 26; i++) {