Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Using C++ ..... You should start by downloading the tictactoe_final.cpp program:

ID: 3606374 • Letter: U

Question

Using C++ .....

You should start by downloading the tictactoe_final.cpp program:

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

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

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

} }

}

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:

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).

Explanation / Answer

here is the simple code to do that -

#include <iostream>
#include<cstdlib>
using namespace std;

int checkwin( char[]);
void board( char[]);

int main()
{
char square[10] = {'o','1','2','3','4','5','6','7','8','9'};
int player = 1,i,choice;

char mark;
do
{
board(square);

if(player%2==1)
player=1;
else
player=2;


// player 2
if(player==2)
{
cout << "Players " << player<<endl;
cout<<"Press Enter for CPU"<<endl;
choice=rand()%9;
mark='O';
int turn=1;
int placed=0;
while(placed==0)
{

if (choice == 1 && square[1] == '1'){
square[1] = mark;
placed=1;
}
else if (choice == 2 && square[2] == '2'){

square[2] = mark;
placed=1;
}
else if (choice == 3 && square[3] == '3'){

square[3] = mark;
placed=1;
}
else if (choice == 4 && square[4] == '4'){

placed=1;
square[4] = mark;
}
else if (choice == 5 && square[5] == '5'){

square[5] = mark;
placed=1;
}
else if (choice == 6 && square[6] == '6'){

square[6] = mark;
placed=1;
}
else if (choice == 7 && square[7] == '7'){

square[7] = mark;
placed=1;
}
else if (choice == 8 && square[8] == '8'){

square[8] = mark;
placed=1;
}
else if (choice == 9 && square[9] == '9'){

square[9] = mark;
placed=1;
}

else
{
choice=rand()%9;
}
}
i=checkwin(square);
player++;

board(square);
}

// player 1
else if(player==1)
{
cout << "Players " << player << ", enter a number: ";
cin >> choice;
mark='X';

if (choice == 1 && square[1] == '1')

square[1] = mark;
else if (choice == 2 && square[2] == '2')

square[2] = mark;
else if (choice == 3 && square[3] == '3')

square[3] = mark;
else if (choice == 4 && square[4] == '4')

square[4] = mark;
else if (choice == 5 && square[5] == '5')

square[5] = mark;
else if (choice == 6 && square[6] == '6')

square[6] = mark;
else if (choice == 7 && square[7] == '7')

square[7] = mark;
else if (choice == 8 && square[8] == '8')

square[8] = mark;
else if (choice == 9 && square[9] == '9')

square[9] = mark;
else
{
cout<<"Invalid move ";

player--;

}
i=checkwin(square);

player++;
}
}while(i==-1);
board(square);
if(i==1)

cout<<"Congratulation! Player "<<--player<<" win ";
else
cout<<" OOps! Game draw";

return 0;
}

void board(char square[])
{
system("cls");
cout << " Tic Tac Toe ";

cout << "Player 1 (X) - Player 2 (O)" << endl << endl;
cout << endl;

cout << " | | " << endl;
cout << " " << square[1] << " | " << square[2] << " | " << square[3] << endl;

cout << "_____|_____|_____" << endl;
cout << " | | " << endl;

cout << " " << square[4] << " | " << square[5] << " | " << square[6] << endl;

cout << "_____|_____|_____" << endl;
cout << " | | " << endl;

cout << " " << square[7] << " | " << square[8] << " | " << square[9] << endl;

cout << " | | " << endl << endl;
}


int checkwin(char square[])
{
if (square[1] == square[2] && square[2] == square[3])

return 1;
else if (square[4] == square[5] && square[5] == square[6])

return 1;
else if (square[7] == square[8] && square[8] == square[9])

return 1;
else if (square[1] == square[4] && square[4] == square[7])

return 1;
else if (square[2] == square[5] && square[5] == square[8])

return 1;
else if (square[3] == square[6] && square[6] == square[9])

return 1;
else if (square[1] == square[5] && square[5] == square[9])

return 1;
else if (square[3] == square[5] && square[5] == square[7])

return 1;
else if (square[1] != '1' && square[2] != '2' && square[3] != '3'
&& square[4] != '4' && square[5] != '5' && square[6] != '6'
&& square[7] != '7' && square[8] != '8' && square[9] != '9')

return 0;
else
return -1;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote