additional check for valid move

This commit is contained in:
Sandipsinh Rathod 2024-10-10 18:41:57 -04:00
parent 1843fd06de
commit 937d223a8c

@ -210,8 +210,8 @@ void printScores(int *points) {
dealloc(arr); dealloc(arr);
} }
bool isValidMove(int x, int y) { bool isValidMove(int x, int y, char **board) {
return (x & 1) != (y & 1); return (x & 1) != (y & 1) && board[x][y] == ' ';
} }
void addPoint( void addPoint(
@ -310,7 +310,7 @@ void solve(Moves moves, char **board) {
int moveY = move.moveY; int moveY = move.moveY;
char player = move.player; char player = move.player;
if (!isValidMove(moveX, moveY)) { if (!isValidMove(moveX, moveY, board)) {
pt "Invalid move by: " << player << " at (x, y): (" << moveX << ", " << moveY << ")" << nl; pt "Invalid move by: " << player << " at (x, y): (" << moveX << ", " << moveY << ")" << nl;
board[moveX][moveY] = 'X'; board[moveX][moveY] = 'X';
printBoard(board, moves.x, moves.y); printBoard(board, moves.x, moves.y);
@ -323,9 +323,9 @@ void solve(Moves moves, char **board) {
board[moveX][moveY] = toLowerCase(player); board[moveX][moveY] = toLowerCase(player);
// TODO: // TODO:
if (moveX % 2 == 0 && moveY % 2 == 1) { if (!(moveX&1) && (moveY&1)) {
solveVertical(moveX, moveY, board, points, moves.x, moves.y, player); solveVertical(moveX, moveY, board, points, moves.x, moves.y, player);
} else if (moveX % 2 == 1 && moveY % 2 == 0) { } else if ((moveX&1) && !(moveY&1)) {
solveHorizontal(moveX, moveY, board, points, moves.y, player); solveHorizontal(moveX, moveY, board, points, moves.y, player);
} else { } else {
pt "Invalid move by: " << player << " at (x, y): (" << moveX << ", " << moveY << ")" << nl; pt "Invalid move by: " << player << " at (x, y): (" << moveX << ", " << moveY << ")" << nl;