Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Extend the feature of your Tic-Tac-Toe program to have the computer play against the user. The computer will always start as the first move. The

Extend the feature of your Tic-Tac-Toe program to have the computer play against the user.

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

I'm using C++. Here is my code

#include

using namespace std;

//print tic tac toe board

void printGameBoard(char board[3][3]);

//check the winning condition

char hasWinner(char board[3][3]);

int main()

{

int count=0;

int row,col; //declare the row and column variables

string player = "X"; //show which player is playing

char winner; //declare the char variable of winner

char square[3][3] = {{'*','*','*'},{'*','*','*'},{'*','*','*'}};

printGameBoard(square); // print the matrix of tic tac toe board

while (count != 9)

{

cout<<"Player "<

cin>> row>>col; // user input

if(row<0 || row> 3 || col<0 || col> 3) //Input Validation

cout<<"invalid input, please enter number between 1 to 3 ";

else if (square[row-1][col-1] != '*')

cout<<"repeat input! ";

else if (player == "X") { // player X is playing

square[row-1][col-1] = 'X';

player = "O";

count= count+1; // if correct entry, next player turn

printGameBoard(square);

}

else{ //switch to player 2

square[row-1][col-1] = 'O'; // player O is playing

player = "X";

count= count+1; //if correct entry, next player turn

printGameBoard(square);

}

winner = hasWinner(square);

if (winner == 'X') { //check for winner

cout<<"Player X win! ";

return 0;

}

else if (winner == 'O') {

cout<<"Player O win! ";

return 0;

}

if (count==9) //game over

cout<<"tie!"<<" ";

}

}

// The hasWinner function calculates if the current game board is in a winning board

char hasWinner(char board[3][3])

{

char hasWinner;

// Check winner of horizontal

if (board[0][0] == board[0][1] && board[0][0] == board[0][2] && board[0][0] != '*')

hasWinner = board[0][0];

else if (board[1][0] == board[1][1] && board[1][0] == board[1][2] && board[1][0] != '*')

hasWinner = board[1][0];

else if (board[2][0] == board[2][1] && board[2][0] == board[2][2] && board[2][0] != '*')

hasWinner = board[2][2];

// Check winner of vertical

else if (board[0][0] == board[1][0] && board[0][0] == board[2][0] && board[2][0] != '*')

hasWinner = board[0][0];

else if (board[0][1] == board[1][1] && board[0][1] == board[2][1] && board[0][1] != '*')

hasWinner = board[0][1];

else if (board[0][2] == board[1][2] && board[0][2] == board[2][2] && board[0][2] != '*')

hasWinner = board[0][2];

//Check winner of diagonal

else if (board[0][0] == board[1][1] && board[0][0] == board[2][2] && board[0][0] != '*')

hasWinner = board[0][0];

else if (board[0][2] == board[1][1] && board[0][2] == board[2][0] && board[1][1] != '*')

hasWinner = board[0][2];

else

hasWinner = '*';

return hasWinner;

}

void printGameBoard(char board[3][3])

{

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

{

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

{

cout<

}

cout<<" ";

}

}

A sample of your programs input/output is listed below

...

...

...

Computers turn

...

.X.

...

Users turn, enter row and column: 1 1

O..

.X.

...

Computers turn

O.X

.X.

...

Users turn, enter row and column: 3 1

O.X

.X.

O..

Computers turn

O.X

XX.

O..

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Fundamentals Of Database Systems

Authors: Ramez Elmasri, Sham Navathe

4th Edition

0321122267, 978-0321122261

More Books

Students also viewed these Databases questions