Game 5 - Tic Tac Toe class - Write a fifth game program that can play Tic Tac To
ID: 3765075 • Letter: G
Question
Game 5 - Tic Tac Toe class - Write a fifth game program that can play Tic Tac Toe. This game displays the lines of the Tic Tac Toe game and prompts the player to choose a move. The move is recorded on the screen and the computer picks his move from those that are left. The player then picks his next move. The program should allow only legal moves. The program should indicate when there is a winner and tell who won. The player is then asked if he would like to play again. A sample output is shown in the attached file. Tic_tac_toe game This game is played between you and the computer. You move first and the computer will then move. Play continues until you or the computer wins or it is a "cat's" game.
1 2 3 A ___|___|___ B ___|___|___ C | | Please enter your move. a1 1 2 3 A _X_|___|___ B ___|___|___ C | | Computer makes a move. 1 2 3 A _X_|_O_|___ B ___|___|___ C | | Please enter your move.b2 1 2 3 A _X_|_O_|___ B ___|_X_|___ C | | Computer makes a move. 1 2 3 A _X_|_O_|___ B ___|_X_|___ C | | O Please enter your move. b1 1 2 3 A _X_|_O_|___ B _X_|_X_|___ C | | o Computer makes a move. 1 2 3 A _X_|_O_|___ B _X_|_X_|___ C o | | O Please enter your move. b3 Congratulations you won!! 1 2 3 A _X_|_O_|___ B _X_|_X_|_X_ C o | | O Would you like to play Tic-Tac-Toe again? (yes/no)
Explanation / Answer
#include <iostream.h>
#define X 'X'
#define O 'O'
#define yes 1
#define no 0
int check (char[][3], int, int);
void counteratk (char x[][3], char turn);
void progress (char x[][3]);
void main()
{
char x[3][3];
int rows,cols,correct=yes,turn,game=yes,counter=0,r,c,d;
for (rows=0; rows<3; rows++)
{
for (cols=0; cols<3; cols++)
{
x[row][cols]=' ';
}
}
cout << "You, are about to witness the program of the century. " <<
"COMMENCE TIC TAC TOE!!!! ";
cout << "Ladies first, please pick the row then the column. ";
progress(x);
do
{
do
{
turn=X;
cout << "Row: ";
cin >> rows;
cout << "Column: ";
cin >> cols;
rows--;
cols--;
x[rows][cols];
correct=check (x,rows,cols);
if (!correct)
}while (!correct);
x[rows][cols]=X;
counter++;
game=winlose(x, game);
turn=O;
if (!game)
{
progress(x);
cout << " You win!!! " << endl;
cin >> game;
game=go_on(game);
if (!game)
break;
}
if (game)
{
counteratk (x, turn);
game=winlose(x,game);
}
if (game==2)
{
cout << " You lost...how lame >.> " << endl;
cout << "Play again " << "1.yes " << "0.no " << endl;
cin >> game;
game=go_on(game);
if(!game)
break;
}
if (counter==5 && game)
{
cout << " It's a tie!! " << endl;
cin >> game;
game=go_on(game);
if(!game)
break;
}
}while (game);
}
int go_on (int game)
{
if (game)
{
main();
{
return no;
int check (char x[][3], int rows, int cols)
return no;
}
int winlose (char x[][3], int game)
{
int rows, cols, r, c, d, ro, co, dO;
for (rows=0; rows<3; rows++)
{
r=0;
c=0;
d=0;
ro=0;
co=0;
dO=0;
for (cols=0; cols<3; cols++)
{
if(x[rows][cols]==X)
r++;
if(x[rows][cols]==O)
ro++;
if(x[cols][rows]==X)
c++;
if(x[cols][rows]==O)
co++;
if(x[cols][cols]==X)
d++;
if(x[cols][cols]==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 counteratk (char x[][3], char turn)
{
int a,b,game;
for (a=0; a<3; a++)
for (b=0; b<3; b++)
if (x[a][b]==' ' && turn==O)
{
x[a][b]=O;
turn=X;
}
progress(x);
}
void progress (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 << "|_____|_____|_____| ";
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.