Implement class TTTBoard which represents a Tic Tac Toe boar
Implement class TTTBoard which represents a Tic Tac Toe board. Create Member function clear() returns the board to its initial state. Function display() writes the board to the console. Function move() takes exactly one argument and places either an X or O on the board in the position input. The internal state of the TTTBoard determines whether an X or O is placed on the board. Function state() returns the state of the board, which is represented using an int (or an enum). The states are PLAYING, X_WINS, Y_WINS, TIE, and UNDEF (undefined).
Solution
#include<iostream>
#include<stdlib.h>
using namespace std;
char square[10] = {\'o\',\'1\',\'2\',\'3\',\'4\',\'5\',\'6\',\'7\',\'8\',\'9\'};
class TTTBoard {
public:
TTTBoard(){}
void clear();
void display() const;
void move();// No need of any argument to move()
int state() const;
};
void TTTBoard :: clear()
{
cout<<\"\ **** Initial state *********\ \ \";
square[1] = \'1\'; square[2] = \'2\';square[3] =\'3\';
square[4] = \'4\'; square[5] =\'5\';square[6]= \'6\';
square[7] =\'7\';square[8]=\'8\';square[9]=\'9\';
move();
}
void TTTBoard :: move(){
int player = 1,i, position;
char mark;
do
{
display();
player=(player%2)?1:2;
cout << \"Player \" << player << \", enter a number: \";
cin >> position;
mark=(player == 1) ? \'X\' : \'O\';
if ( position == 1 && square[1] == \'1\')
square[1] = mark;
else if (position == 2 && square[2] == \'2\')
square[2] = mark;
else if (position == 3 && square[3] == \'3\')
square[3] = mark;
else if (position == 4 && square[4] == \'4\')
square[4] = mark;
else if (position == 5 && square[5] == \'5\')
square[5] = mark;
else if (position == 6 && square[6] == \'6\')
square[6] = mark;
else if (position == 7 && square[7] == \'7\')
square[7] = mark;
else if (position == 8 && square[8] == \'8\')
square[8] = mark;
else if (position == 9 && square[9] == \'9\')
square[9] = mark;
else
{
cout<<\"Invalid move \";
player--;
cin.ignore();
cin.get();
}
i = state();
player++;
}while(i==-1);
display();
if(i==1)
cout<<\"==>\\aPlayer \"<<--player<<\" win \";
else
cout<<\"==>\\aGame draw\";
cin.ignore();
cin.get();
}
/*********************************************
*
* FUNCTION TO RETURN GAME STATUS
* 1 FOR GAME IS OVER WITH RESULT
* -1 FOR GAME IS IN PROGRESS
* O GAME IS OVER AND NO RESULT
***********************************************/
int TTTBoard :: state() const
{
enum status {PLAYING, X_WINS=1, Y_WINS=1, TIE=-1, UNDEF};
if (square[1] == square[2] && square[2] == square[3])
return X_WINS;
else if (square[4] == square[5] && square[5] == square[6])
return X_WINS;
else if (square[7] == square[8] && square[8] == square[9])
return X_WINS;
else if (square[1] == square[4] && square[4] == square[7])
return X_WINS;
else if (square[2] == square[5] && square[5] == square[8])
return Y_WINS;
else if (square[3] == square[6] && square[6] == square[9])
return Y_WINS;
else if (square[1] == square[5] && square[5] == square[9])
return Y_WINS;
else if (square[3] == square[5] && square[5] == square[7])
return Y_WINS;
else if (square[1] != \'1\' && square[2] != \'2\' && square[3] != \'3\'
&& square[4] != \'4\' && square[5] != \'5\' && square[6] != \'6\'
&& square[7] != \'7\' && square[8] != \'8\' && square[9] != \'9\')
return PLAYING;
else
return TIE;
}
/*******************************************************************
* FUNCTION TO DRAW BOARD OF TIC TAC TOE WITH PLAYERS MARK
*********************************************************************/
void TTTBoard :: display() const
{
cout << \"\ \ \\tTic Tac Toe\ \ \";
cout << \"Player 1 (X) - Player 2 (O)\" << endl << endl;
cout << endl;
cout << \" | | \" << endl;
cout << \" \" << square[1] << \" | \" << square[2] << \" | \" << square[3] << endl;
cout << \"_____|_____|_____\" << endl;
cout << \" | | \" << endl;
cout << \" \" << square[4] << \" | \" << square[5] << \" | \" << square[6] << endl;
cout << \"_____|_____|_____\" << endl;
cout << \" | | \" << endl;
cout << \" \" << square[7] << \" | \" << square[8] << \" | \" << square[9] << endl;
cout << \" | | \" << endl << endl;
}
int main()
{
TTTBoard T;
T.move();
char ch;
do{
cout<<\"Do you want play more (Y/y)? : \";
cin>>ch;
T.clear();
}while(ch == \'Y\' || ch == \'y\');
}


