Answer:
#include <iostream>
#include <ctime>
#include <string> // for string methods
using namespace std;
class Board
{
private:
char squares[3][3]; //two-dimensional arrays
public:
Board() {}
void PrintBoard();
void BeginGame();
void playerTurn (char num, char Player);
bool CheckWin (char Player, bool gameOver);
bool CheckDraw(bool gameOver);
};
//Organize board for the game
void Board::BeginGame()
{
int n = 1;
int i = 0;
int j = 0;
for ( i = 0; i < 3; i++ )
{
for ( j = 0; j < 3; j++ )
{
squares[i][j] = '0' + n; //Casting n value to a character
n++;
}
}
} //End SetGameBoard
//printing board
void Board::PrintBoard()
{
int i = 0;
int j = 0;
for ( i = 0; i < 3; i++ )
{
for ( j = 0; j < 3; j++ )
{
if ( j < 2 )
{
cout << squares[i][j] << " | ";
}
else
{
cout << squares[i][j] << endl;
}
}
}
} //End
void Board:: playerTurn (char num, char Player)
{
int i = 0;
int j = 0;
bool WrongMove = true; //If the value is true then the player has made a wrong move
for( i = 0; i < 3; i++ )
{
for( j = 0; j < 3; j++ )
{
if(squares[i][j] == num)
{
//Assigns the space with the X or O,
squares[i][j] = Player;
WrongMove = false;
}
}
}
if(WrongMove == true) { cout << "You can only mark the open sets!\n"; }
} //End Player Move
//checking for the winner. If not, cotinue or a draw
bool Board:: CheckWin (char Player, bool GameOver)
{
for(int i = 0; i < 3; i++) //To check rows
{
if(squares[i][0] == squares[i][1] && squares[i][1] ==
squares[i][2]) GameOver = true;
}
for(int i = 0; i < 3; i++) //To check columns
{
if(squares[0][i] == squares[1][i] && squares[1][i] ==
squares[2][i]) GameOver = true;
}
if(squares[0][0] == squares[1][1] && squares[1][1] == squares[2][2]) //To check the Diagonals
{
GameOver = true;
}
if(squares[0][2] == squares[1][1] && squares[1][1] == squares[2][0])
{
GameOver = true;
}
if(GameOver == true)
{
cout << "Player " << Player << " wins!\n\n";
cout << "-----------------------" << endl;
cout << "| CONGRATULATIONS " << Player << " |\n";
cout << "-----------------------" << endl << endl;
}
return GameOver;
}
bool Board:: CheckDraw(bool GameOver)
{
int n = 1;
int i = 0;
int j = 0;
int counter = 0;
for( i = 0; i < 3; i++ )
{
for( j = 0; j < 3; j++ )
{
//Check to see if the board if full
if(squares[i][j] == '0' + n)
{
counter++;
}
n++;
}
}
if( counter < 1 )
{
cout << "Game Draw\n\n";
GameOver = true;
}
return GameOver;
}
// Main method starts here
int main()
{
bool finish = false, GameOver = false,isValid;
char Player = 'O', num;
int number;
srand(time(NULL)); //seed random from time
number = rand()%2; //Randomly choose a player to start game
if(number == 0)
Player = 'X';
else
Player = 'O';
cout << "" << endl;
cout << "-=Tic-Tac-Toe=-\n";
cout << "-------------"<< endl;
Board TicTac;
TicTac.BeginGame();
TicTac.PrintBoard(); //Initialize and output board
do
{
if( Player == 'X' )
{
Player = 'O';
}
else
{
Player = 'X';
}
TicTac.PrintBoard();
cout << "\nPlayer \"" << Player << "\", it's your turn: ";
cin >> num;
cout << "\n";
TicTac.playerTurn (num, Player);
GameOver = TicTac.CheckWin (Player, GameOver);
GameOver = TicTac.CheckDraw(GameOver);
if(GameOver == true)
{
cout << "Thank you for playing!";
finish = true;
}
} while(!finish);
return 0;
}