Question: why int this code running? Id appreciate a comment on the location of the error. #include using namespace std; class TicTacToe { public: TicTacToe (

why int this code running? Id appreciate a comment on the location of the error.
#include
using namespace std;
class TicTacToe
{
public:
TicTacToe()
{
for(int i =0; i <3; ++i)
{
for(int j =0; j <3; ++j)
{
board[i][j]=0;
}
}
sigil[0]='.';
sigil[1]='X';
sigil[2]='O';
}
void play();
private:
char sigil[3];
int board[3][3];
void drawBoard();
bool isMoveLegal(int row, int column);
int winner();
bool isGameOver();
};
void TicTacToe::drawBoard()
{
cout <<"012"<< endl;
for(int i =0; i <3; ++i)
{
cout << i <<"";
for(int j =0; j <3; ++j)
{
cout << sigil[board[i][j]]<<"";
}
cout << endl;
}
}
bool TicTacToe::isMoveLegal(int row, int column)
{
return row >=0 && row <3 && column >=0 && column <3 && board[row][column]==0;
}
int TicTacToe::winner()
{
//check rows and columns
for(int i =0; i <3; ++i)
{
if(board[i][0]== board[i][1] && board[i][1]== board[i][2] && board[i][0]!=0)
{
return board[i][0];
}
if(board[0][i]== board[1][i] && board[1][i]== board[2][i] && board[0][i]!=0)
{
return board[0][i];
}
}
return 0;
//check diagonals
if(board[0][0]== board[1][1] && board[1][1]== board[2][2] && board[0][0]!=0)
{
return board[0][0];
}
if(board[0][2]== board[1][1] && board[1][1]== board[2][0] && board[0][2]!=0)
{
return board[0][2];
}
return 0;
}
bool TicTacToe::isGameOver()
{
if(winner()!=0)
{
return true;
}
for(int i =0; i <3; ++i)
{
for(int j =0; j <3; ++j)
{
if(board[i][j]==0)
{
return false;
}
}
`}
return true;
}
void TicTacToe::play()
{
int currentPlayer =1;
while(!isGameOver())
{
drawBoard();
cout << "player "<< currentPlayer <<"("<< sigil[currentPlayer]<<"), enter your move (row and column): ";
int row =-1, column =-1;
cin >> row >> column;
while(!isMoveLegal(row, column))
{
cout << "Invalid move. Try again: ";
cin >> row >> column;
}
board[row][column]= currentPlayer;
currentPlayer =3

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 Programming Questions!