Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Implement a class to play the game of tic-tac-toe with two players. The class co

ID: 3867875 • Letter: I

Question

Implement a class to play the game of tic-tac-toe with two players. The class contains as private data member a 3 by 3 array of integers. The constructor should initialize the empty board to zeros. When the first player moves, place 1 in the specified square: place 2 when the second player moves. Each move must be done in an empty square. After each move, determine if the game has been won or if the game is a draw. A sample run follows. 0 0 0 0 0 0 0 0 0 Player1 move: 1 1 1 0 0 0 0 0 0 0 0 Player2 move: 2 2 1 0 0 0 2 0 0 0 0 Player 1 move: 3 1 1 0 1 0 2 0 0 0 0 Player2 move: 1 1 1 1 is used. Please choose another move: 3 3 1 0 1 0 2 0 0 0 2 Player1 move: 2 1 1 1 1 0 2 0 0 0 2 Player l wins!

Explanation / Answer

#include <iostream>

using namespace std;

bool check(int matrix[][3],int i,int j)

{

if(i==1&&j==1)

{

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

{

return true;

}

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

{

return true;

}

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

{

return true;

}

else return false;

}

if(i==1 && j==2)

{

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

{

return true;

}

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

return true;

else

return false;

}

if(i==1&&j==3)

{

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

{

return true;

}

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

return true;

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

return true;

else

return false;

}

if(i==2 && j==1)

{

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

{

return true;

}

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

{

return true;

}

}

if(i==2 && j==2)

{

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

{

return true;

}

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

return true;

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

return true;

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

{

return true;

}

else

return false;

}

if(i==2 && j==3)

{

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

{

return true;

}

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

return true;

else

return false;

}

if(i==3 && j==1)

{

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

{

return true;

}

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

return true;

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

return true;

else

return false;

}

if(i==3 && j==2)

{

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

return true;

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

return true;

else

return false;

}

if(i==3 && j==3)

{

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

{

return true;

}

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

return true;

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

return true;

else

return false;

}

}

void display(int matrix[][3])

{

cout<<endl;

int i,j;

for(i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

cout<<matrix[i][j]<<" ";

}

cout<<endl;

}

}

int main(){

int matrix[3][3];

bool status=false;

int i,j,k;

for(int i=0;i<3;i++)

{

for(j=0;j<3;j++)

{

matrix[i][j]=0;

}

}

for(k=0;k<9;k++)

{

display(matrix);

cout<<"Enter player"<<(k%2)+1<<"::";

cin>>i;

cin>>j;

if(matrix[i-1][j-1]==0)

{

matrix[i-1][j-1]=(k%2)+1;

status=check(matrix,i,j);

if(status)

{

cout<<"player "<<(k%2)+1<<" won ";

break;

}

}

else{

cout<<i<<" "<<j<<" is used. Please choose another move:";

cin>>i;

cin>>j;

matrix[i-1][j-1]=(k%2)+1;

status=check(matrix,i,j);

if(status)

{

cout<<"player "<<(k%2)+1<<" won ";

break;

}

}

}

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote