In C++ Implement a class to play the game of tic-tac-toe with two players. The c
ID: 3856159 • 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:
/* Header file TicTacToe.h */
#include<iostream>
#include <string>
using std::cout;
using std::cin;
using std::endl;
using std::string;
class TicTacToe
{
private:
int tickTactoBoard[3][3]; //represent the board. we are hiding this data memeber
int nextPlayer;
public:
void displayBoard(); //prototype
bool getPlayerMove(int & x, int & y);
bool checkIfMoveValid(int x, int y);
int whoisTheWinner();
bool IsTheWinner(int c);
bool IsItDraw();
TicTacToe(); //constructor
};
/* Implementation file TicTacToe.cpp */
#include "TicTacToe.h"
void TicTacToe::displayBoard()
{
cout << endl;
for(int i=0; i<3; ++i)
{
cout << tickTactoBoard[i][0] << " " << tickTactoBoard[i][1] << " " << tickTactoBoard[i][2] << " " << endl;
}
}
bool TicTacToe::getPlayerMove(int & x, int & y)
{
cout << "Player "<<nextPlayer<<" move: ";
cin >> x;
cin >> y;
while( !checkIfMoveValid(x,y) )
{
cout << x <<" "<< y <<"is used. Please choose another move: ";
cin >> x;
cin >> y;
}
tickTactoBoard[x][y] = nextPlayer;
if (nextPlayer == 1 )
nextPlayer = 2;
else
nextPlayer = 1;
return true;
}
bool TicTacToe::checkIfMoveValid(int x, int y)
{
if (((x >=0 && x <= 2) && (y >=0 && y <= 2) && (tickTactoBoard[x][y] == 0) ))
return true;
else
return false;
}
bool TicTacToe::IsItDraw()
{
for (int i=0; i<3; i++)
for (int j=0; j<3; j++)
if (tickTactoBoard[i][j] == 0)
return false;
return true;
}
int TicTacToe::whoisTheWinner()
{
int c = 5; //1, 2, 5, or 0 for 1 or 2 is the winner or Draw or more moves are available.
if (IsTheWinner(1))
return 1;
if (IsTheWinner(2))
return 2;
if (IsItDraw())
return 5;
return 0; //more moves are possible
}
bool TicTacToe::IsTheWinner(int c)
{
//Check columns and rows
for (int i=0; i<3; i++)
{
if (tickTactoBoard[i][0] == c && tickTactoBoard[i][1] == c && tickTactoBoard[i][2] == c)
return true;
if (tickTactoBoard[0][i] == c && tickTactoBoard[1][i] == c && tickTactoBoard[2][i] == c)
return true;
}
//check diagonals
if (tickTactoBoard[0][0] == c && tickTactoBoard[1][1] == c && tickTactoBoard[2][2] == c)
return true;
if (tickTactoBoard[2][0] == c && tickTactoBoard[1][1] == c && tickTactoBoard[0][2] == c)
return true;
return false;
}
TicTacToe::TicTacToe()
{
//Initialize the board array
for (int i=0; i<3; ++i)
for (int j=0; j< 3; ++j)
tickTactoBoard[i][j]= 0;
nextPlayer = 1;
}
/* main method TestTicTacToe.cpp */
#include "TicTacToe.h"
int main()
{
int x, y;
int c = 0;
TicTacToe myTicTacToe;
myTicTacToe.displayBoard();
do
{
myTicTacToe.getPlayerMove(x, y);
myTicTacToe.displayBoard();
c= myTicTacToe.whoisTheWinner();
if (c != 0)
{
if (c == 5)
cout << "It is a draw!" << endl;
else
cout << "Player " << c << " win! " << endl;
break; //break out of the for loop
}
} while(true);
return 0;
}
Working fine. Let me knpw any concerns. Thank you