Question & Answer: 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……

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. Playerl move: 1 1 10 0 Player2 move: 2 2 10 0 0 2 0 Playerl move: 3 1 10 0 0 2 0 10 0 Player2 move: 1 1 1 1 is used. Please choose another move: 3 3 10 0 0 2 0 1 0 2 Playerl move 21 10 0 12 0 10 2 Playerl wins!

C++please

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. 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 Player1 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 Player1 wins!

Expert Answer

 

Source code:

—————————————–
#include <iostream>
#define player1 1
#define player2 2
#define player1MOVE 1
#define player2MOVE 2
using namespace std;
void show(int array[3][3]);
bool check(int array[3][3]);
int main()
{
int array[3][3],i,j,move=1;
int moveIndex = 0, row, column;
int moves[9];
cout<<“————————————–“<<endl;
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
array[i][j]=0;
}
}
cout<<“t ***** Tic-Tac-Toe ******nn”<<endl;
printf(“Choose a cell numbered from 1 to 9 as below and playnn”);
for(i=0;i<3;i++)
{
for(j=0;j<3;j++)
{
cout<<“t”<<array[i][j];
}
cout<<endl;
}
while (check(array) == false && moveIndex != 9)
{
if (move == player1)
{
cout<<“Player1 Move: “;
cin>>row;
cin>>column;
array[row][column] = player1MOVE;
show(array);
moveIndex ++;
move = player2;
}
else if (move == player2)
{
cout<<“Player2 Move: “;
cin>>row;
cin>>column;
array[row][column] = player2MOVE;
show(array);
moveIndex ++;
move = player1;
}
}
if (check(array) == false && moveIndex == 9)
printf(“It’s a drawn”);
else
{
if (move == player1)
cout<<” player2 has wonn”<<endl;
else if (move == player2)
cout<<” player1 has wonn”<<endl;
}
return 0;
}
void show(int array[3][3])
{
for(int i=0;i<3;i++)
{
for(int j=0;j<3;j++)
{
cout<<“t”<<array[i][j];
}
cout<<endl;
}
return;
}
bool check_row(int array[3][3])
{
for (int i=0; i<3; i++)
{
if (array[i][0] == array[i][1] &&array[i][1] == array[i][2] &&
array[i][0] != 0)
{
return true;
}
}
return false;
}
bool check_column(int array[3][3])
{
for (int i=0; i<3; i++)
{
if (array[0][i] == array[1][i] && array[1][i] == array[2][i]
&& array[0][i] != 0)
{
return true;
}
}
return false;
}
bool check_diagonal(int array[3][3])
{
if (array[0][0] == array[1][1] && array[1][1] == array[2][2] &&
array[0][0] != 0)
{
return true;
}
if (array[0][2] == array[1][1] && array[1][1] == array[2][0] &&
array[0][2] != 0)
{
return true;
}
return false;
}
bool check(int array[3][3])
{
return(check_row(array) || check_column(array) || check_diagonal(array) );
}

Still stressed from student homework?
Get quality assistance from academic writers!