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

PROGRAM IN C++ ONLY The game TTT is a two player game where each player trades m

ID: 3816280 • Letter: P

Question

PROGRAM IN C++ ONLY

The game TTT is a two player game where each player trades moves across a 3 x 3 game grid. One player designates their move with an X whereas the other player designates their move with an O. When one player has three of their marks placed in a line, that player has won the game. If all nine squares in the grid are filled and neither player has three symbols matching in a row, that game is considered tied. Consider the following game scenario between a red and a blue opponent.

Points will be awarded for this exercise as follows: • 10 points generate a game board with three randomly placed symbols output the game board with the placement of each symbol • 10 points + 10 extra credit points A playable game of TTT, where the player trades moves with the computer opponent (players moves can be entered by asking the user for the row and column to be placed.) • 10 points + 20 extra credit points All of the above requirements except the game should be implemented so that the player will never be able to defeat the computer opponent in a game of TTT (please note that the game can be completed to a tie.)

PROGRAM IN C++ ONLY

Explanation / Answer

Following is the code for the above game using min max AI algorithm such that computer never loses the game.

It is playable game where we ask player to enter row and column values to place move.

#include <iostream>
using namespace std;

const int board_size = 3;
const int move_size=2;
/*Function to clear the board in order to begin the game*/
void clearBoard(int board[board_size][board_size])
{
int i,j;
for(i = 0; i < board_size; i++) {
for(j = 0; j < board_size; j++){
board[i][j] = - 1;
}
  
}
}
/*Function to print the board*/
void drawBoard(int board[board_size][board_size])
{
int i, j;
for(i = 0; i < board_size; i++) {
for(j = 0; j < board_size; j++){
if(board[i][j] == 2)
   cout << "X"<<" "; //X represents player move
else if(board[i][j] == 1)
   cout << "O"<<" "; //O represents computer move
else
   cout << "_"<<" ";
}
cout << endl;
}
}
/*Function to input values of rows and column from user to play the move*/
void getPlayerMove(int player,int move[move_size])
{
int row,column;
  
cout << " Enter row and column values (between 0 and 2) to play your move ";
cin >> row;
cin >> column;
move[0]=row;
move[1]=column;
  
}
/*Function to check if there is win*/
int isaWin(int board[board_size][board_size])
{
   if (board[0][0] != -1 && (board[0][0] == board[0][1] && board[0][0] == board[0][2] ||
       board[0][0] == board[1][0] && board[0][0] == board[2][0] ||
       board[0][0] == board[1][1] && board[0][0] == board[2][2]))
  
       return board[0][0];
  
   // Check horizontal, vertical & diagonal through [1][1]
   if (board[1][1] != -1 && (board[1][1] == board[1][0] && board[1][1] == board[1][2] ||
       board[1][1] == board[0][1] && board[1][1] == board[2][1] ||
       board[1][1] == board[2][0] && board[1][1] == board[0][2]))
  
       return board[1][1];
  
   // Check horizontal, vertical & diagonal through [2][2]
   if (board[2][2] != -1 && (board[2][2] == board[0][2] && board[2][2] == board[1][2] ||
       board[2][2] == board[2][0] && board[2][2] == board[2][1]))
  
       return board[2][2];
  
   return 0;

}
/*Function used by comp(player)which recursively calls itself to check for best position to play*/
int negamax(int board[board_size][board_size],int player1,int player2) {

   int best_move_score = -9999;
   int score_for_this_move = 0;
  
   //If player 1 wins, then the score is high (good for player1)
   if (isaWin(board)==player1)
       return 1000;
  
   //If player 2 loses, then the score is low (bad for player1)
   else if (isaWin(board) ==player2)
       return -1000;

   for (int i = 0; i < 3; i++) {
       for (int j = 0; j < 3; j++) {
           if (board[i][j] == -1) {
               board[i][j] = player1; //Try test move.
               score_for_this_move = -(negamax(board, player2, player1));
               board[i][j] = -1; //Put back test move.
               if (score_for_this_move >= best_move_score) {
                   best_move_score = score_for_this_move;
               }
           }
       }
   }
  
   if (best_move_score == -9999 || best_move_score == 0)
       return 0;
  
   else if (best_move_score < 0)
       return best_move_score + 1;

   else if (best_move_score > 0)
       return best_move_score - 1; //As the game goes longer, and the recursion goes deeper, the moves near the end are less favorable than in the beginning.
  
}

/*Function which will generate nest move for comp(player) using min max algo such that comp never loses*/
void pick_best_move(int board[board_size][board_size], int move[move_size], int player1, int player2) {

   int best_move_score = -9999;
   int best_move_row = -9999;
   int best_move_col = -9999;
   int score_for_this_move = 0;

   for (int i = 0; i < board_size; i++) {
       for (int j = 0; j < board_size; j++) {
           if (board[i][j] == -1) {
               board[i][j] = 1; //Try test move.
               score_for_this_move = -(negamax(board,player2,player1));
               board[i][j] = -1; //Put back test move.
               if (score_for_this_move >= best_move_score) {
                   best_move_score = score_for_this_move;
                   best_move_row = i;
                   best_move_col = j;
               }
           }
       }
   }
   move[0]=best_move_row ;
   move[1]=best_move_col;
  
}
/*Function which plays comp move*/
void makeCompMove(int board[board_size][board_size],int player,int move[move_size])
{
cout << "Computer (player " << player << ") moving." << endl;
pick_best_move(board,move,player,2);
}
bool isMoveValid(int board[board_size][board_size], int move[])
{
if(board[move[0]][move[1]] < 0)
return true;
return false;
}

int main()
{
int board[board_size][board_size];
int turn = 0;
int move[move_size];
int plays=0;
clearBoard(board);
while(isaWin(board)==0 && plays<10){
drawBoard(board);
if(2 == turn)
turn = 1;
else
turn = 2;
do {
if(2 == turn)
   getPlayerMove(turn,move);
else
   makeCompMove(board,turn,move);
} while(!isMoveValid(board, move));
board[move[0]][move[1]] = turn;
plays++;
}
drawBoard(board);
if(plays==10 && isaWin(board)==0)
cout<<"Tie";
else if(turn == 1)
cout << "Computer wins." << endl;
else
cout<<"Player eins."<<endl;
return 0;
}

In the above program for part1 random function can be added to generate random moves as following:

Similarly for part 2nd generate moves randlomly for computer turn not using minmax algo.

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