From 937d223a8c45eb870bd94009a6255233c5620392 Mon Sep 17 00:00:00 2001 From: Sandipsinh Rathod Date: Thu, 10 Oct 2024 18:41:57 -0400 Subject: [PATCH] additional check for valid move --- main.cpp | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/main.cpp b/main.cpp index e65fd86..114b0f8 100644 --- a/main.cpp +++ b/main.cpp @@ -210,8 +210,8 @@ void printScores(int *points) { dealloc(arr); } -bool isValidMove(int x, int y) { - return (x & 1) != (y & 1); +bool isValidMove(int x, int y, char **board) { + return (x & 1) != (y & 1) && board[x][y] == ' '; } void addPoint( @@ -310,7 +310,7 @@ void solve(Moves moves, char **board) { int moveY = move.moveY; char player = move.player; - if (!isValidMove(moveX, moveY)) { + if (!isValidMove(moveX, moveY, board)) { pt "Invalid move by: " << player << " at (x, y): (" << moveX << ", " << moveY << ")" << nl; board[moveX][moveY] = 'X'; printBoard(board, moves.x, moves.y); @@ -323,9 +323,9 @@ void solve(Moves moves, char **board) { board[moveX][moveY] = toLowerCase(player); // TODO: - if (moveX % 2 == 0 && moveY % 2 == 1) { + if (!(moveX&1) && (moveY&1)) { 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); } else { pt "Invalid move by: " << player << " at (x, y): (" << moveX << ", " << moveY << ")" << nl;