CH 12 PP 2 A software company that develops games has just hired you! Before wor
ID: 664973 • Letter: C
Question
CH 12 PP 2
A software company that develops games has just hired you! Before working on the next version of Medieval Menace they have given you the task of implementing the tic-tac-toe game in C++. Tic-tac-toe consists of a 3 X 3 game board of squares where each square is either empty, has an X mark, or has a O marker. Two players represented by an X or and O play the game. The objective is for one player to get three Xs or three Os in a row first. Design, implement, and test classes that represent a tic-tac-toe game board and X and O markers. Your classes should provide suitable observer and mutator classes to create a game that prompts for player X and player O to place markers at specified locations on the game board. After each move your program should display the current game board to the console. Your program should also check after each move if there is a winning configuration of the game board. If so, the game should complete indicating which player won.
Explanation / Answer
//Game of Tic Tac Toe
#include <iostream.h>
#define X 'X'
#define O 'O'
#define yes 1
#define no 0
int verify (char[][3], int, int);
void reTaliateForce (char x[][3], char choice);
void statusSoFar (char x[][3]);
int successDefeat(char [][3], int );
int continuePlaying(int);
void main()
{
char x[3][3];
int count;
int boardRow,column,correct=yes,choice,game=yes,counter=0,r,c,d;
for (boardRow=0; boardRow<3; boardRow++)
{
for (column=0; column<3; column++)
{
x[boardRow][column]=' ';
}
}
cout << "Welcome to the game of Tic Tac Toe " <<
cout << "Can you select Row of the board and then the column of the board please? ";
statusSoFar(x);
count = 1;
do
{
do
{
choice=X;
cout << "boardRow: ";
cin >> boardRow;
cout << "column: ";
cin >> column;
boardRow--;
column--;
x[boardRow][column];
correct=verify (x,boardRow,column);
// if (!correct)
count ++;
} while (count < 10);
x[boardRow][column]=X;
counter++;
game=successDefeat(x, game);
choice=O;
if (!game)
{
statusSoFar(x);
cout << " You win!!! " << endl;
cin >> game;
game=continuePlaying(game);
if (!game)
break;
}
if (game)
{
reTaliateForce (x, choice);
game=successDefeat(x,game);
}
if (game==2)
{
cout << " Sorry You had lost this time " << endl;
cout << "Like to Play again? " << "1.yes " << "0.no " << endl;
cin >> game;
game=continuePlaying(game);
if(!game)
break;
}
if (counter==5 && game)
{
cout << " It's a tie!! " << endl;
cin >> game;
game=continuePlaying(game);
if(!game)
break;
}
}while (game);
}
int continuePlaying (int game)
{
if (game) main();
return no;
}
int verify (char x[3][3], int boardRow, int column)
{
return no;
}
int successDefeat (char x[][3], int game)
{
int boardRow, column, r, c, d, ro, co, dO;
for (boardRow=0; boardRow<3; boardRow++)
{
r=0;
c=0;
d=0;
ro=0;
co=0;
dO=0;
for (column=0; column<3; column++)
{
if(x[boardRow][column]==X)
r++;
if(x[boardRow][column]==O)
ro++;
if(x[column][boardRow]==X)
c++;
if(x[column][boardRow]==O)
co++;
if(x[column][column]==X)
d++;
if(x[column][column]==O)
dO++;
if (ro==3 || co==3 || dO==3)
game=2;
}
}
if (x[0][2]==X && x[1][1]==X && x[2][0]==X)
d=3;
if (x[0][2]==O && x[1][1]==O && x[2][0]==O)
game=2;
if (r==3 || c==3 || d==3)
game=no;
return game;
}
void reTaliateForce (char x[][3], char choice)
{
int a,b,game;
for (a=0; a<3; a++)
for (b=0; b<3; b++)
if (x[a][b]==' ' && choice==O)
{
x[a][b]=O;
choice=X;
}
statusSoFar(x);
}
void statusSoFar (char x[][3])
{
cout << " _________________ ";
cout << "| | | | ";
cout << "| " << x[0][0] << " | " << x[0][1] << " | " << x[0][2] <<
" | ";
cout << "|_____|_____|_____| ";
cout << "| | | | ";
cout << "| " << x[1][0] << " | " << x[1][1] << " | " << x[1][2] <<
" | ";
cout << "|_____|_____|_____| ";
cout << "| | | | ";
cout << "| " << x[2][0] << " | " << x[2][1] << " | " << x[2][2] <<
" | ";
cout << "|_____|_____|_____| ";
}
Since it sayd answer cannot be more than 65,000 characters, cannot paste the output screen
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.