Question: Tic-Tac-Toe program to have the computer play against the user in C++. I need to modify a Tic Tac Toe C+ code (that I already

Tic-Tac-Toe program to have the computer play against the user in C++.

I need to modify a Tic Tac Toe C+ code (that I already have) to instead of having two players playing against each other, this time it has to be the computer against the human player.

The computer will always start as the first move. The computer should never lose the game, no matter what the user enters. So that the computer can win or draw the game.

Tic-Tac-Toe against computer

Write a program that allows a player to play against a computer (player X and player O) to play a game of tic-tac-toe. Use a two- dimensional char array with three rows and three columns as the game board. Each element of the array should be initialized with an asterisk (*). The players take turns making moves and the program keeps track of whose turn it is. Player X moves first. The program should run a loop that:

Displays the contents of the board array (see prompts and output, below).

Prompts and allows the player to select a location on the board for an X in the case of player X The program should ask the player to enter the row and column number. Valid row or column numbers are 1, 2, or 3. Then the computer make its move

The loop terminates when the computer won, or a tie has occurred. If the computer has won, the program should declare the computer the winner and end. If a tie has occurred, the program should say so and end.

This is the Code that I created, but I need now to change it to make the computer one of the player

#include

using namespace std;

void displayPrompt(char OXO [3][3], char what)

{

int rowNum, colNum;

cout<< what <<"'s player at bat."<

cout<<" Make your move! Where do you want the "<< what <<" positioned?"<

//The program ask to enter the specific move by writing the number of the the row and the number of the column

cout<<"Type the row number and column number separated by a space and press ENTER."<

cin>>rowNum>>colNum;

cout<<"You have typed row #"<

cout<<" and typed column #"<

if(rowNum >= 0 && rowNum < 3 && colNum >= 0 && colNum < 3)

{

//operators

if(OXO [rowNum][colNum] != '*')

{

cout<<"The space is already taken."<

cout<<"Make another choice."<

displayPrompt(OXO, what);

}

else

{

cout<<"Wow, THAT IS A MOVE! "<

OXO [rowNum][colNum] = what;

}

}

else

{

cout<<"The entry is invalid: Please try again!"<

cout<<" The columns and row numbers must be either 0, 1, 2, to be valid."<

displayPrompt(OXO, what);

}

}

//creating the table-board. displayBoard shows the current tic-tac-toe board along with proper spacing

void displayBoard(char OXO [3][3])

{

int i, j;

cout<<"****************"<

cout<<"| 0 | 1 | 2 | 3 |"<

cout<<"****************"<

for(i = 0; i < 3; i++)

{

cout<<"| "<

for(j = 0; j < 3; j++)

if(OXO [i][j] == '*')

cout<<" | ";

else

cout<< OXO [i][j]<<" | ";

cout<

}

}

bool gameComplete(char OXO [3][3])

{

bool won = false;

int i = 1;

if(OXO [i][i] == OXO [i][i+1] && OXO [i][i] == OXO [i][i-1] && OXO [i][i] != '*')

won = true;

if(OXO [i][i] == OXO [i+1][i] && OXO [i][i] == OXO [i-1][i] && OXO [i][i] != '*')

won = true;

if(OXO [i][i] == OXO [i-1][i-1] && OXO [i][i] == OXO [i+1][i+1] && OXO [i][i] != '*')

won = true;

if(OXO [i][i] == OXO [i-1][i+1] && OXO [i][i] == OXO [i+1][i-1] && OXO [i][i] != '*')

won = true;

if(OXO [i-1][i-1] == OXO [i-1][i] && OXO [i-1][i] == OXO [i-1][i+1] && OXO [i-1][i] != '*')

won = true;

if(OXO [i-1][i-1] == OXO [i][i-1] && OXO [i][i-1] == OXO [i+1][i-1] && OXO [i][i-1] != '*')

won = true;

if(OXO [i+1][i-1] == OXO [i+1][i] && OXO [i+1][i] == OXO [i+1][i+1] && OXO [i+1][i] != '*')

won = true;

if(OXO [i-1][i+1] == OXO [i][i+1] && OXO [i][i+1] == OXO [i+1][i+1] && OXO [i][i+1] != '*')

won = true;

return won;

}

bool boardFilled(char OXO [3][3])

{

bool space = false;

int i, j;

for(i = 0; i < 3; i++)

for(j = 0; j < 3; j++)

if(OXO [i][j] == '*')

space = true;

return !space;

}

int main()

{

char OXO [3][3], turn, again;

int i, j;

while(1)

{

for(i = 0; i < 3; i++)

for(j = 0; j < 3; j++)

OXO [i][j] = '*';

cout<<" Lets Play a New Game: X goes first. PLAY BALL!"<

turn = 'X';

//The program runs a loop

while(!gameComplete(OXO) && !boardFilled(OXO))

{

// The moves are displayed on the board (table) array

displayBoard(OXO);

displayPrompt(OXO, turn);

if(turn == 'X')

turn = 'O';

else

turn = 'X';

}

// We controls whether a player has won, or a draw has occurred.

if(boardFilled(OXO))

{

// If a draw has occurred, the program should say so and end.

cout<<"It is a DRAW! THE CAT WINS, Look for the Dog to play next game!"<

}

else

{

if(turn == 'X')

cout<<" The Os WIN!"<

else

cout<<" The Xs WIN!"<

}

//Reset board if user wants to play again

displayBoard(OXO);

cout<<" Want to play again? Type Y or y to play again."<

cin>>again;

if(again != 'Y' && again != 'y')

{

cout<<"IT WAS A GREAT GAME!"<

exit(0);

}

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!