Question: #include #include #include #include #include using namespace std; vector > board = { { 0 , 0 , 0 } , { 0 , 0

#include
#include
#include
#include
#include
using namespace std;
vector> board ={{0,0,0},
{0,0,0},
{0,0,0}};
unordered_set usedNumbers;
void displayBoard(){
for (const auto& row : board){
for (int value : row){
cout << value <<"";
}
cout << endl;
}
}
bool checkWin(){
// Check rows and columns
for (int i =0; i <3; ++i){
int rowSum =0;
int colSum =0;
for (int j =0; j <3; ++j){
rowSum += board[i][j];
colSum += board[j][i];
}
if (rowSum ==15|| colSum ==15)
return true;
}
// Check diagonals
int diag1= board[0][0]+ board[1][1]+ board[2][2];
int diag2= board[0][2]+ board[1][1]+ board[2][0];
if (diag1==15|| diag2==15)
return true;
return false;
}
bool boardFull(){
for (const auto& row : board){
for (int value : row){
if (value ==0)
return false;
}
}
return true;
}
void userMove(bool playEven){
char rowLetter;
int value;
bool validInput = false;
while (!validInput){
cout << "Your turn (Enter the position (a-i) and value): ";
cin >> rowLetter >> value;
if (usedNumbers.count(value)>0|| rowLetter <'a'|| rowLetter >'i'|| value <1|| value >9||(playEven && value %2==0)||(!playEven && value %2!=0)){
cout << "Invalid move. Please try again." << endl;
} else {
int row =(rowLetter -'a')/3;
int col =(rowLetter -'a')%3;
if (board[row][col]!=0){
cout << "Spot already occupied. Please try again." << endl;
} else {
board[row][col]= value;
usedNumbers.insert(value);
validInput = true;
}
}
}
}
void computerMove(bool playEven){
int value;
bool validMove = false;
while (!validMove){
value = rand()%9+1; // Random number between 1 and 9
if (usedNumbers.count(value)==0 && ((playEven && value %2==0)||(!playEven && value %2!=0))){
int row, col;
do {
row = rand()%3;
col = rand()%3;
} while (board[row][col]!=0);
board[row][col]= value;
usedNumbers.insert(value);
validMove = true;
}
}
}
int main(){
srand(time(0));
cout << "Welcome to the Sum-Up-15 Game!" << endl;
char playerChoice;
cout <<"Do you want to play with even numbers? (y/n): ";
cin >> playerChoice;
bool playEven =(playerChoice =='y'|| playerChoice =='Y');
bool userTurn = true;
while (true){
// Player's turn
if (userTurn){
displayBoard();
userMove(!playEven); // If playEven is true, user should play odd numbers, and vice versa
if (checkWin()){
cout << "Congratulations! You win!" << endl;
break;
}
} else {// Computer's turn
computerMove(!playEven); // If playEven is true, computer should play odd numbers, and vice versa
if (checkWin()){
cout << "Computer wins! Better luck next time." << endl;
break;
}
}
if (boardFull()){
cout << "It's a draw!" << endl;
break;
}
userTurn =!userTurn;
}
displayBoard();
return 0;
}
modify this code by adding an AI algorith so that the code plays the best move rather than a random move. you can use stuufs like minimax algorithm. C++

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!