Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#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;
}
bool 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;
}
}
}
return checkWin();
}
void computerMove(bool playEven){
int bestScore =-1000;
int bestMoveRow =-1;
int bestMoveCol =-1;
for (int i =0; i <3; ++i){
for (int j =0; j <3; ++j){
if (board[i][j]==0){
board[i][j]= playEven ?1 : 2; // Assuming even numbers represent user and odd numbers represent computer
int score = minimax(false);
board[i][j]=0;
if (score > bestScore){
bestScore = score;
bestMoveRow = i;
bestMoveCol = j;
}
}
}
}
board[bestMoveRow][bestMoveCol]= playEven ?1 : 2;
usedNumbers.insert(bestMoveRow *3+ bestMoveCol +1);
}
int evaluate(){
if (checkWin()){
return boardFull()?0 : 1;
}
return -1;
}
int minimax(bool maximizingPlayer){
int score = evaluate();
if (score !=-1) return score;
if (maximizingPlayer){
int bestScore =-1000;
for (int i =0; i <3; ++i){
for (int j =0; j <3; ++j){
if (board[i][j]==0){
board[i][j]=2; // Computer's move
int currentScore = minimax(false);
board[i][j]=0;
bestScore = max(bestScore, currentScore);
}
}
}
return bestScore;
} else {
int bestScore =1000;
for (int i =0; i <3; ++i){
for (int j =0; j <3; ++j){
if (board[i][j]==0){
board[i][j]=1; // User's move
int currentScore = minimax(true);
board[i][j]=0;
bestScore = min(bestScore, currentScore);
}
}
}
return bestScore;
}
}
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();
if (userMove(!playEven)){
cout << "Congratulations! You win!" << endl;
break;
}
} else {
// Computer's turn
computerMove(!playEven);
displayBoard();
if (checkWin()){
cout << "Computer wins! Better luck next time." << endl;
break;
}
}
if (boardFull()){
cout << "It's a draw!" << endl;
break;
}
userTurn =!userTurn;
}
displayBoard();
Welcome to the Sum-Up-15 Game!
Do you want to play with even numbers? (y/n): y
000
000
000
Your turn (Enter the position (a-i) and value): a6
Process finished with exit code 139(interrupted by signal 11: SIGSEGV)
when i run this code i get that outcome, where when i type anything it just stops the code and says exit code 139. could you fix it please

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

More Books

Students also viewed these Databases questions