In C++ Implement a class to play the game of tic-tac-toe with two players. The c
ID: 3856003 • Letter: I
Question
In C++ 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.
000 000 000
Player1 move: 1 1
100
000
000
Player2 move: 2 2
100
020
000
Player1 move: 3 1
100
020
100
Player2 move: 1 1
1 1 is used. Please choose another move: 3 3
100
020
102
Player1 move: 2 1
100
120
102
The header, implementation and driver programs should be respectively named: TicTacToe.h, TicTacToe.cpp and TestTicTacToe.cpp
Can anyone help me please
Explanation / Answer
Answer:
#include <iostream>
using namespace std;
char place[10] = {'0','1','2','3','4','5','6','7','8','9'};
int examinewinner();
void showboard();
int main()
{
int player = 1,i,select;
char locate;
do
{
showboard();
player=(player%2)?1:2;
cout << "Player " << player << "Enter a number: ";
cin >> select;
locate=(player == 1) ? 'X' : 'O';
if (select == 1 && place[1] == '1')
place[1] = locate;
else if (select == 2 && place[2] == '2')
place[2] = locate;
else if (select == 3 && place[3] == '3')
place[3] = locate;
else if (select == 4 && place[4] == '4')
place[4] = locate;
else if (select == 5 && place[5] == '5')
place[5] = locate;
else if (select == 6 && place[6] == '6')
place[6] = locate;
else if (select == 7 && place[7] == '7')
place[7] = locate;
else if (select == 8 && place[8] == '8')
place[8] = locate;
else if (select == 9 && place[9] == '9')
place[9] = locate;
else
{
cout<<"Please choose another move: ";
player--;
cin.ignore();
cin.get();
}
i=examinewinner();
player++;
}while(i==-1);
showboard();
if(i==1)
cout<<"==>Player "<<--player<<" wins! ";
else
cout<<"==>Game is standoff";
cin.ignore();
cin.get();
return 0;
}
int examinewinner()
{
if (place[1] == place[2] && place[2] == place[3])
return 1;
else if (place[4] == place[5] && place[5] == place[6])
return 1;
else if (place[7] == place[8] && place[8] == place[9])
return 1;
else if (place[1] == place[4] && place[4] == place[7])
return 1;
else if (place[2] == place[5] && place[5] == place[8])
return 1;
else if (place[3] == place[6] && place[6] == place[9])
return 1;
else if (place[1] == place[5] && place[5] == place[9])
return 1;
else if (place[3] == place[5] && place[5] == place[7])
return 1;
else if (place[1] != '1' && place[2] != '2' && place[3] != '3'
&& place[4] != '4' && place[5] != '5' && place[6] != '6'
&& place[7] != '7' && place[8] != '8' && place[9] != '9')
return 0;
else
return -1;
}
void showboard()
{
system("cls");
cout << " !!!!!!!!!!Tic Tac Toe Mind Game Ready To Play !!!!!!!!!! ";
cout << "Player 1 picks X and Player 2 picks O" << endl << endl;
cout << endl;
cout << " | | " << endl;
cout << " " << place[1] << " | " << place[2] << " | " << place[3] << endl;
cout << "_____|_____|_____" << endl;
cout << " | | " << endl;
cout << " " << place[4] << " | " << place[5] << " | " << place[6] << endl;
cout << "_____|_____|_____" << endl; cout << " | | " << endl;
cout << " " << place[7] << " | " << place[8] << " | " << place[9] << endl;
cout << " | | " << endl << endl;
}