FOR a tic tac toe do the following please in c a Implement a

FOR a tic tac toe, do the following please in c++:

a) Implement a Board class with the following functionality:

Default constructor, copy constructor, destructor, assignment operator.

Determine the symbol at the specified grid position.

Change the symbol at the specified grid position.

Determine the winner of the game, if any. You must distinguish between the case where there is no winner because the game is not over yet, and the case where there is no winner because the game ended in a tie.

o Hint: Create four constants for the four game states and return one of them.

Hint: Find a way to pass the position in the grid as a single value. This can be done by creating a record (struct) to store the row and column, or by representing both values using a single integer. This will make Parts B, C, and D easier and your program easier to read.

Print the board to standard output.

b) Implement an abstract Player class with the following functionality:

Default constructor, copy constructor, destructor, assignment operator.

o Remember: This class will be inherited, so the destructor must be declared with the virtual keyword.

A way to set the symbol placed by this Player.

o This can be done as either a function or as an additional constructor with a parameter.

Determine the symbol placed by this Player.

Determine the next move for this Player. This function should take a const reference to a Board as a parameter and must be a pure virtual function.

Hint: Add a class invariant that ensures that a Player always has a valid symbol.

c) Implement a PlayerHuman class the following functionality:

Default constructor, copy constructor, destructor, assignment operator.

Determine the next move. This must be a virtual function and override the corresponding function in Player.

Detect and reject invalid user input.

d) Implement a PlayerRandom class with the following functionality:

Default constructor, copy constructor, destructor, assignment operator.

Determine the next move. This must be a virtual function and override the corresponding function in Player.

e) Implement a program to simulate the Tic Tac Toe game. This program should behave as follows:

Ask the user whether each player is human or computer

o Detect and reject invalid input

Dynamically create appropriate Player subclass instances for each team.

Repeatedly ask the players for moves in alternating order.

When the game is over, print who won, print the board, and exit the program.

in c++ please

the game should be played by (human vs human), (human vs computer ) or ( computer vs omputer )

Solution

Answer:

#include <iostream>

using namespace std;

char place[10] = {\'o\',\'1\',\'2\',\'3\',\'4\',\'5\',\'6\',\'7\',\'8\',\'9\'};

int checkwinner();
void displayboard();

int main()
{
int player = 1,i,choice;

char indicate;
do
{
displayboard();
player=(player%2)?1:2;

cout << \"Player \" << player << \", enter a number: \";
cin >> choice;

indicate=(player == 1) ? \'X\' : \'O\';

if (choice == 1 && place[1] == \'1\')

place[1] = indicate;
else if (choice == 2 && place[2] == \'2\')

place[2] = indicate;
else if (choice == 3 && place[3] == \'3\')

place[3] = indicate;
else if (choice == 4 && place[4] == \'4\')

place[4] = indicate;
else if (choice == 5 && place[5] == \'5\')

place[5] = indicate;
else if (choice == 6 && place[6] == \'6\')

place[6] = indicate;
else if (choice == 7 && place[7] == \'7\')

place[7] = indicate;
else if (choice == 8 && place[8] == \'8\')

place[8] = indicate;
else if (choice == 9 && place[9] == \'9\')

place[9] = indicate;
else
{
cout<<\"Not a valid move \";

player--;
cin.ignore();
cin.get();
}
i=checkwinner();

player++;
}while(i==-1);
displayboard();
if(i==1)

cout<<\"==>\\aPlayer \"<<--player<<\" win \";
else
cout<<\"==>\\aGame is draw\";

cin.ignore();
cin.get();
return 0;
}


int checkwinner()
{
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 displayboard()
{
system(\"cls\");
cout << \"\ \ \\t Game Tic Tac Toe\ \ \";

cout << \"Player 1 (X) - Player 2 (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;
}

FOR a tic tac toe, do the following please in c++: a) Implement a Board class with the following functionality: Default constructor, copy constructor, destructo
FOR a tic tac toe, do the following please in c++: a) Implement a Board class with the following functionality: Default constructor, copy constructor, destructo
FOR a tic tac toe, do the following please in c++: a) Implement a Board class with the following functionality: Default constructor, copy constructor, destructo
FOR a tic tac toe, do the following please in c++: a) Implement a Board class with the following functionality: Default constructor, copy constructor, destructo

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site