Question: Need help to debug this C++ program, it only matches in the last case, for if row=0 and col=1 it gives the correct output but

Need help to debug this C++ program, it only matches in the last case, for if row=0 and col=1 it gives the correct output but everything other time, it does not match what the output is supposed to be. P.S (0 means it is an empty space, 1 means it is X, and -1 means it is O)Need help to debug this C++ program, it only matches in thelast case, for if row=0 and col=1 it gives the correct output

#include

using namespace std;

void playMove(int board[3][3], int row, int col, bool& turn, bool& validMove, bool& gameOver, int& winCode) {

if(gameOver)

return;

validMove = true;

if(row>2 || row2 || col

validMove=false;

return;

}

board[row][col] = (turn==true)?1:-1;

winCode = 0;

if(board[0][0]==board[0][1] && board[0][2]==board[0][1])

winCode = 1;

else if (board[1][0]==board[1][1] && board[1][2]==board[1][1])

winCode = 2;

else if (board[2][0]==board[2][1] && board[2][2]==board[2][1])

winCode = 3;

else if (board[0][0]==board[1][0] && board[2][0]==board[1][0])

winCode = 4;

else if (board[0][1]==board[1][1] && board[2][1]==board[1][1])

winCode = 5;

else if (board[0][2]==board[1][2] && board[2][2]==board[1][2])

winCode = 6;

else if (board[0][0]==board[1][1] && board[2][2]==board[1][1])

winCode = 7;

else if (board[0][2]==board[1][1] && board[2][0]==board[1][1])

winCode = 8;

gameOver = false;

if(winCode!=0) {

gameOver = true;

}

for(int i=0;i

for(int j=0;j

cout

}

}

turn = (turn==true)?false:true;

cout

}

int main() {

int board[3][3] = {1,0,0,0,-1,0,0,0,0};

bool turn = false, validMove, gameOver =false;

int winCode = 0;

playMove(board, 1,2,turn, validMove, gameOver, winCode);

}

winCode is integer variable is set to a code that indicates which cells on the board have marks that form a line, as shown in Table 1. If game0ver is false, the code should be set to 0. If game0ver is true and the game is a draw, then winCode should also be set to 0. if gameOver is true and one of the players won, the code should be set to one of the integer values as indicated in the table. The main purpose of this code is to simplify drawing a line in the graphics part of the assignment Code Sequence No win 1Row 0 of the grid, cell (0,0) to cell (0,2) 2Row 1 of the grid, cell (1,0) to cell (1,2) 3Row 2 of the grid, cell (2,0) to cell (2,2) 4 Column 0 of the grid, cell (0,0) to cell (2,0) 5 Column 1 of the grid, cell (0,1) to cell (2,1) 6 Column 2 of the grid, cell (0,2) to cell (2,2) 7Left to right diagonal, cell (0,0) to cell (2,2) 8 Right to left diagonal, cell (2,0) to cell (0,2) Table 1 winCode values Upon completion, the function must print the following to cout on single line . The contents of the game board, printed one row after the other (i.e., the first row followed by the second row followed by the third row) . The values of the row and col parameters . The output turn value . The output Boolean variables validMove and gameOver . The winning sequence code The values printed should be separated by single spaces. The two game grid examples showin in Figure 2 can be used to demonstrate the expected output Game Grid A: X's turn Game Grid B: O's turr

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!