add some comments and drop all possible strings

This commit is contained in:
Sandipsinh Rathod 2024-10-11 17:02:37 -04:00
parent 388a52eca1
commit 2acdae42e4

@ -176,19 +176,25 @@ Moves serialize(char *s) {
result.x = (result.x << 1) - 1; result.x = (result.x << 1) - 1;
result.y = (result.y << 1) - 1; result.y = (result.y << 1) - 1;
string player; char *player = static_cast<char *>(alloc(4 * sizeof(char)));
int x, y; int x, y;
while (iss >> player >> x >> y) { while (iss >> player >> x >> y) {
if (player == "END") break; if (player == "END") break;
// TODO: move this exceptions to runtime checks // TODO: move this exceptions to runtime checks
if (x >= result.x || y >= result.y) { 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); throw invalid_argument(err);
} }
if (!isValidPlayer(player[0])) { if (!isValidPlayer(player[0])) {
string err = "Invalid player: " + player; string err = "Invalid player: " + player[0];
throw invalid_argument(err); throw invalid_argument(err);
} }
Move move; Move move;
@ -198,6 +204,7 @@ Moves serialize(char *s) {
result.moves.push_back(move); result.moves.push_back(move);
} }
dealloc(player);
return result; return result;
} }
@ -249,7 +256,7 @@ void printBoard(char **board, int x, int y) {
// Print the board with row numbers // Print the board with row numbers
for (int row = 0; row < x; ++row) { for (int row = 0; row < x; ++row) {
if (row % 10 == 0) { if (!(row % 10)) {
pt row / 10; pt row / 10;
} else { } else {
pt " "; pt " ";
@ -362,14 +369,6 @@ void solveVertical(
int y, int y,
char player 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 // check for any box below current move
if (moveX < x - 2 && board[moveX + 2][moveY] != ' ' && board[moveX + 1][moveY - 1] != ' ') { if (moveX < x - 2 && board[moveX + 2][moveY] != ' ' && board[moveX + 1][moveY - 1] != ' ') {
int rightEdge = moveY == y - 1 ? 1 : 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( void solveHorizontal(
int moveX, int moveX,
int moveY, int moveY,
@ -419,6 +419,7 @@ void solveHorizontal(
} }
} }
// Solve the game
void solve(Moves moves, char **board) { void solve(Moves moves, char **board) {
int *points = static_cast<int *>(alloc(26 * (sizeof(int)))); int *points = static_cast<int *>(alloc(26 * (sizeof(int))));
for (int i = 0; i < 26; i++) { for (int i = 0; i < 26; i++) {