Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Using C++ ..... You should start by downloading the tictactoe_final.cpp program: ******************************************************************************* //TIC-TAC-TOE [game] #include #include #include #include #include using namespace std; const int SIZE

Using C++ .....

You should start by downloading the tictactoe_final.cpp program:

*******************************************************************************

//TIC-TAC-TOE [game] #include  #include  #include  #include  #include  using namespace std; const int SIZE = 3; void displayBoard(string b[][SIZE]); bool userFirst(); bool currentPlayerWon(string b[][SIZE], string symbol); int main() { //construct board string board[SIZE][SIZE]; int position_id = 1; for (int i=0; i< SIZE; i++) { for (int j=0; j < SIZE; j++) { board[i][j] = to_string(position_id); /* stringstream ss; ss << position_id; board[i][j] = ss.str(); */position_id++; } } //Initial game output cout << "Welcome to Tic-Tac-Toe" << endl; bool userTurn = userFirst(); if (userTurn == true) { cout << "Player goes first!" << endl; } else { cout << "Computer goes first!" << endl; } //Loop for turn taking in game int positionsRemaining = SIZE * SIZE; bool userWon = false; bool computerWon = false; while ((positionsRemaining > 0) && (!userWon) && (!computerWon)) { displayBoard(board); //User's turn if (userTurn) { bool validMove = false; while (!validMove) { int position_id; cout << "Enter a position: "; cin >> position_id; if ((position_id <= (SIZE*SIZE)) && (position_id > 0)) { int row = (position_id-1)/SIZE; int col = (position_id-1)%SIZE; //cout << "row = " << row << " col = " << col << endl; if ((board[row][col] != "X") && (board[row][col] != "O")) { board[row][col] = "X"; validMove = true; } else { cout << "Position already used. Try again." << endl; } } else { cout << "Position invalid. Try again." << endl; } } positionsRemaining--; userWon = currentPlayerWon(board, "X"); userTurn = false; } //Computer's turn else { bool validMove = false; while (!validMove) { int row = rand()%SIZE; int col = rand()%SIZE; if ((board[row][col] != "X") && (board[row][col] != "O")) { board[row][col] = "O"; validMove = true; } } positionsRemaining--; computerWon = currentPlayerWon(board, "O"); userTurn = true; } } //Display game result displayBoard(board); if (userWon) { cout << "Congratulations! You have won!" << endl; } else if (computerWon) { cout << "The computer has won. Try again." << endl; } else { cout << "Out of moves. Try again." << endl; } return 0; } void displayBoard(string b[][SIZE]) { cout << "Tic-tac-toe board:" << endl << endl; for (int i=0; i< SIZE; i++) { for (int j=0; j < SIZE; j++) { cout << b[i][j] << "\t"; } cout << endl; } cout << endl; } bool userFirst() { //set seed for random number generation srand(time(NULL)); //generate a random number //0 for computer //1 for user int num = rand()%2; if (num == 0) { return false; } return true; } //Return true if player/computer with symbol (X or O) has won bool currentPlayerWon(string b[][SIZE], string symbol) { //Horizontal case //Loop through each row for (int i=0; i < SIZE; i++) { bool rowWinDetected = true; //Check all positions in row and see if they are the same symbol for (int j = 0; j < SIZE; j++) { if (b[i][j] != symbol) { rowWinDetected = false; } } if (rowWinDetected) { return true; } } //Vertical case //Loop through each column for (int i=0; i < SIZE; i++) { bool colWinDetected = true; //Check all positions in column and see if they are the same symbol for (int j = 0; j < SIZE; j++) { if (b[j][i] != symbol) { colWinDetected = false; } } if (colWinDetected) { return true; } } //Diagonal case #1 bool diagonal1WinDetected = true; for (int i=0; i < SIZE; i++) { if (b[i][i] != symbol) { diagonal1WinDetected = false; } } if (diagonal1WinDetected) { return true; } //Diagonal case #2 bool diagonal2WinDetected = true; for (int i=0; i < SIZE; i++) { if (b[(SIZE-1)-i][i] != symbol) { diagonal2WinDetected = false; } } if (diagonal2WinDetected) { return true; } //otherwise win not diagonal2WinDetected return false; } 

*******************************************************************************

This program is finished but the computer player is not very strategic and relies only on random guesses to determine the next move:

 //Computer's turn else {  
 bool validMove = false; while (!validMove) {  
 int row = rand()%SIZE; int col = rand()%SIZE; if ((board[row][col] != "X")  
 && (board[row][col] != "O")) { board[row][col] = "O"; validMove = true;  

} }

 positionsRemaining--; computerWon = currentPlayerWon(board, "O"); userTurn = true;  

}

First, rewrite the above code so that the determination of the next computer move is done in a separate function. After the rewrite the above code should look like:

 //Computer's turn else {  
 //The row and col are both passed as //call-by-reference int row, col;  
 generateComputerMove(board, row, col); board[row][col] = "O";  
 positionsRemaining--; computerWon = currentPlayerWon(board, "O"); 
 userTurn = true; }  

Next, make the determination of a move in the new generateComputerMove() function more strategic (Hint: you can add rules instead of always just determining a random move).

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 Management Systems

Authors: Mark L. Gillenson

2nd Edition

0470624701, 978-0470624708

More Books

Students also viewed these Databases questions

Question

Prepare a short profile of Henry words worth Longfellow?

Answered: 1 week ago

Question

What is RAM as far as telecommunication is concerned?

Answered: 1 week ago

Question

Question 1: What is reproductive system? Question 2: What is Semen?

Answered: 1 week ago

Question

Describe the sources of long term financing.

Answered: 1 week ago

Question

8. Praise the trainees for their success in learning the task.

Answered: 1 week ago