This program need to be completed in C language if possible

This program need to be completed in C++ language, if possible write down the comments. will help me in the final

In this assignment, you will implement a simple Tic-Tac-Toe game. It will be possible to play human-vs-human, human-vs-computer, or computer-vs-computer. Tic-Tac-Toe, also called X\'s and Os, 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 player to get a straight line ofthree symbols wins. If all the cells on the board are filled and neither player has a line of 3 symbols, the game is a tie. Lines may be horizontal, vertical, or diagonal You will implement a Board class to represent the 3x3 grid. This class will have 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 class to determine the players 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 will implement PlayerHuman as a subclass of Player. When this subclass is to choose a move, it should print out the current board and ask the user for the row and column to place a symbol. This class 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 have 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 program should then use dynamic memory allocation to create instances of the appropriate subclasses of Player. These must be stored with pointers of type Player The first team should place X symbols, while the second team should place O symbols. The program should then alternate asking the players for moves until one player wins, or the board is full and the game is atie. After the game is over, the program should print the winner and the board and exit.

Solution

The below program implements the TicTac game.. The code is not in the format as said above...Create classes and try to put the below functions in them and adjust a little. It should work.

If you are unable to do, please ping here will try to help you at place where you are stuck.

CODE:

// A C++ Program to play tic-tac-toe

#include<bits/stdc++.h>
using namespace std;

#define COMPUTER 1
#define HUMAN 2

#define SIDE 3 // Length of the board

// Computer will move with \'O\'
// and human with \'X\'
#define COMPUTERMOVE \'O\'
#define HUMANMOVE \'X\'


// To show the current board status
void showBoard(char board[][SIDE])
{
printf(\"\ \ \");

printf(\"\\t\\t\\t--------------\ \");
printf(\"\\t\\t\\t %c | %c | %c \ \", board[0][0],
board[0][1], board[0][2]);
printf(\"\\t\\t\\t--------------\ \");
printf(\"\\t\\t\\t %c | %c | %c \ \", board[1][0],
board[1][1], board[1][2]);
printf(\"\\t\\t\\t--------------\ \");
printf(\"\\t\\t\\t %c | %c | %c \ \", board[2][0],
board[2][1], board[2][2]);
printf(\"\\t\\t\\t--------------\ \ \");

return;
}

// To show the instructions
void showInstruction()
{
printf(\"\\t\\t\\t Tic-Tac-Toe\ \ \");
printf(\"Choose a cell numbered from 1 to 9 as below\"
\" and play\ \ \");

printf(\"\\t\\t\\t--------------\ \");
printf(\"\\t\\t\\t 1 | 2 | 3 \ \");
printf(\"\\t\\t\\t--------------\ \");
printf(\"\\t\\t\\t 4 | 5 | 6 \ \");
printf(\"\\t\\t\\t--------------\ \");
printf(\"\\t\\t\\t 7 | 8 | 9 \ \");
printf(\"\\t\\t\\t--------------\ \ \");

printf(\"-\\t-\\t-\\t-\\t-\\t-\\t-\\t-\\t-\\t-\ \ \");

return;
}


// o initialiseGame the game
void initialiseGame(char board[][SIDE], int moves[])
{
// Initiate the random number generator so that
// the same configuration doesn\'t arises
srand(time(NULL));

// Initially the board is empty
for (int i=0; i<SIDE; i++)
{
for (int j=0; j<SIDE; j++)
board[i][j] = \' \';
}

// Fill the moves with numbers
for (int i=0; i<SIDE*SIDE; i++)
moves[i] = i;

// randomise the moves
random_shuffle(moves, moves + SIDE*SIDE);

return;
}

// to declare the winner of the game
void findWinner(int whoseTurn)
{
if (whoseTurn == COMPUTER)
printf(\"COMPUTER has won\ \");
else
printf(\"HUMAN has won\ \");
return;
}

// returns true if any of the row is crossed with the same player\'s move
bool rowCross(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);
}

// returns true if any of the column is crossed with the same player\'s move
bool columnCross(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);
}

// returns true if any of the diagonal is crossed with the same player\'s move
bool diagonalCross(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);
}

// returns true if the game is over else it returns a false
bool gameOver(char board[][SIDE])
{
return(rowCross(board) || columnCross(board)
|| diagonalCross(board) );
}

// to play Tic-Tac-Toe
void playGame(int whoseTurn)
{
// A 3*3 Tic-Tac-Toe board for playing
char board[SIDE][SIDE];

int moves[SIDE*SIDE];

// initialiseGame the game
initialiseGame(board, moves);

// Show the instructions before playing
showInstruction();

int moveIndex = 0, x, y;

// Keep playing till the game is over or it is a draw
while (gameOver(board) == false &&
moveIndex != SIDE*SIDE)
{
if (whoseTurn == COMPUTER)
{
x = moves[moveIndex] / SIDE;
y = moves[moveIndex] % SIDE;
board[x][y] = COMPUTERMOVE;
printf(\"COMPUTER has put a %c in cell %d\ \",
COMPUTERMOVE, moves[moveIndex]+1);
showBoard(board);
moveIndex ++;
whoseTurn = HUMAN;
}

else if (whoseTurn == HUMAN)
{
x = moves[moveIndex] / SIDE;
y = moves[moveIndex] % SIDE;
board[x][y] = HUMANMOVE;
printf (\"HUMAN has put a %c in cell %d\ \",
HUMANMOVE, moves[moveIndex]+1);
showBoard(board);
moveIndex ++;
whoseTurn = COMPUTER;
}
}

// If the game has drawn
if (gameOver(board) == false &&
moveIndex == SIDE * SIDE)
printf(\"It\'s a draw\ \");
else
{
// Toggling the user to declare the actual winner
if (whoseTurn == COMPUTER)
whoseTurn = HUMAN;
else if (whoseTurn == HUMAN)
whoseTurn = COMPUTER;

// Declare the winner
findWinner(whoseTurn);
}
return;
}

// Driver program
int main()
{
// Let us play the game with COMPUTER starting first
playGame(COMPUTER);

return (0);
}

OUTPUT:

           Tic-Tac-Toe

Choose a cell numbered from 1 to 9 as below and play

           --------------
           1 | 2 | 3
           --------------
           4 | 5 | 6
           --------------
           7 | 8 | 9
           --------------

-   -   -   -   -   -   -   -   -   -

COMPUTER has put a O in cell 8


           --------------
           | |
           --------------
           | |
           --------------
           | O |
           --------------

HUMAN has put a X in cell 2


           --------------
           | X |
           --------------
           | |
           --------------
           | O |
           --------------

COMPUTER has put a O in cell 3


           --------------
           | X | O
           --------------
           | |
           --------------
           | O |
           --------------

HUMAN has put a X in cell 7


           --------------
           | X | O
           --------------
           | |
           --------------
           X | O |
           --------------

COMPUTER has put a O in cell 4


           --------------
           | X | O
           --------------
           O | |
           --------------
           X | O |
           --------------

HUMAN has put a X in cell 6


           --------------
           | X | O
           --------------
           O | | X
           --------------
           X | O |
           --------------

COMPUTER has put a O in cell 1


           --------------
           O | X | O
           --------------
           O | | X
           --------------
           X | O |
           --------------

HUMAN has put a X in cell 9


           --------------
           O | X | O
           --------------
           O | | X
           --------------
           X | O | X
           --------------

COMPUTER has put a O in cell 5


           --------------
           O | X | O
           --------------
           O | O | X
           --------------
           X | O | X
           --------------

It\'s a draw

This program need to be completed in C++ language, if possible write down the comments. will help me in the final In this assignment, you will implement a simpl
This program need to be completed in C++ language, if possible write down the comments. will help me in the final In this assignment, you will implement a simpl
This program need to be completed in C++ language, if possible write down the comments. will help me in the final In this assignment, you will implement a simpl
This program need to be completed in C++ language, if possible write down the comments. will help me in the final In this assignment, you will implement a simpl
This program need to be completed in C++ language, if possible write down the comments. will help me in the final In this assignment, you will implement a simpl
This program need to be completed in C++ language, if possible write down the comments. will help me in the final In this assignment, you will implement a simpl

Get Help Now

Submit a Take Down Notice

Tutor
Tutor: Dr Jack
Most rated tutor on our site