In this assignment, you will implement a simple Tic-Tac-Toe game. It will be pos
ID: 3589200 • Letter: I
Question
In this assignment, you will implement a simple Tic-Tac-Toe game. It will be possibk to pay human-vs-human, human-vs-computer, or computer-vs-computer Tic-Tac-Toe, also called X's and O's, Noughts and Crosses, and X and 0 is a simple game played on a 3x3 grid, referred to as the board. The grid starts empty, and the two players take turns placing their respective symbols, X and O, on an empty grid cell, referred to as making a move. The first payer to get a straight line of three symbols wins. Ifall the cells on the board are filled and ne ither player has a line of3 symbols, the game is a tie. Lines may be horizontal, vertical, or diagonal. You will implement a Board cass to represent the 3x3 grid. This class will ha ve functions to determine which symbol if any, is in a cell, to place a symbol in a cell, to determine the winner, if any so far, and to print the board to standard output. The board should appear as below: You will implement an abstract Player cass to determine the player's moves. The Player class should store which symbol it will place and contain a pure virtual function to determine the Player's next move. This function will be overridden by the subclasses of Player. You wil implement PlayerHuman as a subcass of Player. When this subclass is to choose a move, it shoukd print out the curre nt board and ask the user for the row and co lumn to place a symbol This cass should detect if the user enters an invalid location, either because it is not in the grid or it already has a symbol, and if the location is invalid, ask the user again. You will implement PlayerRandom as a subclass of Player. When this subclass is to choose a move, it should return a random position in the grid that does not yet ha ve a symbol. You will implement a program to play the Tic Tac Toe game. The program should begin by asking the user if each team should be controlled by a human or by the computer. The p should then use dynamic memory allocation to create instances ofthe appropriate s Player. These must be stored with pointers of type Player*. The first team should place X symbols, while the second team should pace O symbols. The program shoukd then alternate asking the players for moves until one player wins, or the board is full and the game is a tie. After the game is over, the program should print the winner and the board and exit. rogram subclasses ofExplanation / Answer
1. C++ code for Human vs Computer Tic Tac Toe game:
#include <iostream>
#include <iomanip>
using namespace std;
bool gameWon (char ch, char board[] [3]);
bool drawGame(char board[][3]);
void gameBoard(char board[] [3]);
bool gameWon(char ch, char board[] [3])
{
for (int x = 0; x < 3; x++)
if (ch == board[x] [0] && ch == board[x] [1] && ch == board[x] [2]) return true;
for (int y = 0; y < 3; y++)
if (ch == board[0] [y] && ch == board[1] [y] && ch == board[2] [y]) return true;
if (ch == board[0] [0] && ch == board[1] [1] && ch == board[2] [2]) return true;
if (ch == board[0] [2] && ch == board[1] [1] && ch == board[2] [0]) return true;
return false;
}
bool drawGame(char board[][3])
{
for (int x = 0; x < 3; x++)
for (int y = 0; y < 3; y++)
if (board[x] [y] == ' ') return false;
return true;
}
void gameBoard(char board[] [3])
{
for (int x = 0; x < 3; x++)
{
cout << "| ";
for (int y = 0; y < 3; y++)
cout << board[x] [y] << " | ";
cout << " -------------" << endl;
}
}
int main()
{
int row;
int column;
char board[3][3] = {{' ', ' ', ' '}, {' ', ' ', ' '}, {' ', ' ', ' '}}; //Displays the empty initial game board
gameBoard(board);
while (true)
{
int firstTurn;
cout << "TIC TAC TOE ";
cout << "Enter who will take the first turn. Choose 0 for user, 1 for computer. ";
cin >> firstTurn;
if (firstTurn == 0)
{
cout << "User will take first turn." << endl;
cout << "Enter a row (0, 1, 2) for X player: ";
cin >> row;
cout << "Enter a column (0, 1, 2) for X player: ";
cin >> column;
return gameBoard(board);
}
else if (firstTurn == 1)
{
cout << "Computer will take first turn." << endl;
row = (rand()%2);
column = (rand()%2);
}
else
cout << "Please choose 0 for user or 1 for computer to take first turn." << endl;
board[row] [column] = 'x';
gameBoard(board);
if (gameWon('x', board))
{
cout << "X player won" << endl;
exit(0);
}
else if (drawGame(board))
{
cout << "No winner" << endl;
exit(0);
}
cout << "Enter a row (0, 1, 2) for O player: ";
cin >> row;
cout << "Enter a column (0, 1, 2) for O player: ";
cin >> column;
board[row] [column] = 'O';
gameBoard(board);
if (gameWon('O', board))
{
cout << "O player won" << endl;
exit(0);
}
else if (drawGame(board))
{
cout << "No winner" << endl;
exit(0);
}
}
return 0;
}
2. C++ code for Computer vs Computer Tic Tac Toe game:
#include<bits/stdc++.h>
using namespace std;
#define COMPUTER1 1
#define COMPUTER2 2
#define SIDE 3
#define COMPUTER1MOVE 'O'
#define COMPUTER2MOVE 'X'
void showBoard(char board[][SIDE])
{
printf(" ");
printf(" %c | %c | %c ", board[0][0],
board[0][1], board[0][2]);
printf(" -------------- ");
printf(" %c | %c | %c ", board[1][0],
board[1][1], board[1][2]);
printf(" -------------- ");
printf(" %c | %c | %c ", board[2][0],
board[2][1], board[2][2]);
return;
}
void showInstructions()
{
printf(" Tic-Tac-Toe ");
printf("Choose a cell numbered from 1 to 9 as below"
" and play ");
printf(" 1 | 2 | 3 ");
printf(" -------------- ");
printf(" 4 | 5 | 6 ");
printf(" -------------- ");
printf(" 7 | 8 | 9 ");
printf("- - - - - - - - - - ");
return;
}
void initialise(char board[][SIDE], int moves[])
{
srand(time(NULL));
for (int i=0; i<SIDE; i++)
{
for (int j=0; j<SIDE; j++)
board[i][j] = ' ';
}
for (int i=0; i<SIDE*SIDE; i++)
moves[i] = i;
random_shuffle(moves, moves + SIDE*SIDE);
return;
}
void declareWinner(int whoseTurn)
{
if (whoseTurn == COMPUTER1)
printf("COMPUTER1 has won ");
else
printf("COMPUTER2 has won ");
return;
}
bool rowCrossed(char board[][SIDE])
{
for (int i=0; i<SIDE; i++)
{
if (board[i][0] == board[i][1] &&
board[i][1] == board[i][2] &&
board[i][0] != ' ')
return (true);
}
return(false);
}
bool columnCrossed(char board[][SIDE])
{
for (int i=0; i<SIDE; i++)
{
if (board[0][i] == board[1][i] &&
board[1][i] == board[2][i] &&
board[0][i] != ' ')
return (true);
}
return(false);
}
bool diagonalCrossed(char board[][SIDE])
{
if (board[0][0] == board[1][1] &&
board[1][1] == board[2][2] &&
board[0][0] != ' ')
return(true);
if (board[0][2] == board[1][1] &&
board[1][1] == board[2][0] &&
board[0][2] != ' ')
return(true);
return(false);
}
bool gameOver(char board[][SIDE])
{
return(rowCrossed(board) || columnCrossed(board)
|| diagonalCrossed(board) );
}
void playTicTacToe(int whoseTurn)
{
char board[SIDE][SIDE];
int moves[SIDE*SIDE];
initialise(board, moves);
showInstructions();
int moveIndex = 0, x, y;
while (gameOver(board) == false &&
moveIndex != SIDE*SIDE)
{
if (whoseTurn == COMPUTER1)
{
x = moves[moveIndex] / SIDE;
y = moves[moveIndex] % SIDE;
board[x][y] = COMPUTER1MOVE;
printf("COMPUTER1 has put a %c in cell %d ",
COMPUTER1MOVE, moves[moveIndex]+1);
showBoard(board);
moveIndex ++;
whoseTurn = COMPUTER2;
}
else if (whoseTurn == COMPUTER2)
{
x = moves[moveIndex] / SIDE;
y = moves[moveIndex] % SIDE;
board[x][y] = COMPUTER2MOVE;
printf ("COMPUTER2 has put a %c in cell %d ",
COMPUTER2MOVE, moves[moveIndex]+1);
showBoard(board);
moveIndex ++;
whoseTurn = COMPUTER1;
}
}
if (gameOver(board) == false &&
moveIndex == SIDE * SIDE)
printf("It's a draw ");
else
{
if (whoseTurn == COMPUTER1)
whoseTurn = COMPUTER2;
else if (whoseTurn == COMPUTER2)
whoseTurn = COMPUTER1;
declareWinner(whoseTurn);
}
return;
}
int main()
{
playTicTacToe(COMPUTER1);
return (0);
}
OUTPUT:
COMPUTER1 has put a O in cell 6
| |
--------------
| | O
--------------
| |
COMPUTER2 has put a X in cell 7
| |
--------------
| | O
--------------
X | |
COMPUTER1 has put a O in cell 5
| |
--------------
| O | O
--------------
X | |
COMPUTER2 has put a X in cell 1
X | |
--------------
| O | O
--------------
X | |
COMPUTER1 has put a O in cell 9
X | |
--------------
| O | O
--------------
X | | O
COMPUTER2 has put a X in cell 8
X | |
--------------
| O | O
--------------
X | X | O
COMPUTER1 has put a O in cell 4
X | |
--------------
O | O | O
--------------
X | X | O
COMPUTER1 has won