Question

In: Computer Science

Using Java please implement a 4x4 Tic-Tac-Toe with Minimax with alpha-beta pruning. Please comment code heavily...

Using Java please implement a 4x4 Tic-Tac-Toe with Minimax with alpha-beta pruning. Please comment code heavily so I can follow along and understand the steps. Also, inlcude a screenshot of the final output. Do not include code for GUI, a GUI is not needed. Thank you

Solutions

Expert Solution

The recursive algorithm for "minimax with alpha-beta pruning" is as follows:

minimax(level, player, alpha, beta)  // player may be "computer" or "opponent"
if (gameover || level == 0)
   return score
children = all valid moves for this "player"
if (player is computer, i.e., max's turn)
   // Find max and store in alpha
   for each child
      score = minimax(level - 1, opponent, alpha, beta)
      if (score > alpha) alpha = score
      if (alpha >= beta) break;  // beta cut-off
   return alpha
else (player is opponent, i.e., min's turn)
   // Find min and store in beta
   for each child
      score = minimax(level - 1, computer, alpha, beta)
      if (score < beta) beta = score
      if (alpha >= beta) break;  // alpha cut-off
   return beta

// Initial call with alpha=-inf and beta=inf
minimax(2, computer, -inf, +inf)

The relevant changes (over the AIPlayerMinimax.java) are:

   /** Get next best move for computer. Return int[2] of {row, col} */
   @Override
   int[] move() {
      int[] result = minimax(2, mySeed, Integer.MIN_VALUE, Integer.MAX_VALUE);
         // depth, max-turn, alpha, beta
      return new int[] {result[1], result[2]};   // row, col
   }
 
   /** Minimax (recursive) at level of depth for maximizing or minimizing player
       with alpha-beta cut-off. Return int[3] of {score, row, col}  */
   private int[] minimax(int depth, Seed player, int alpha, int beta) {
      // Generate possible next moves in a list of int[2] of {row, col}.
      List<int[]> nextMoves = generateMoves();
 
      // mySeed is maximizing; while oppSeed is minimizing
      int score;
      int bestRow = -1;
      int bestCol = -1;
 
      if (nextMoves.isEmpty() || depth == 0) {
         // Gameover or depth reached, evaluate score
         score = evaluate();
         return new int[] {score, bestRow, bestCol};
      } else {
         for (int[] move : nextMoves) {
            // try this move for the current "player"
            cells[move[0]][move[1]].content = player;
            if (player == mySeed) {  // mySeed (computer) is maximizing player
               score = minimax(depth - 1, oppSeed, alpha, beta)[0];
               if (score > alpha) {
                  alpha = score;
                  bestRow = move[0];
                  bestCol = move[1];
               }
            } else {  // oppSeed is minimizing player
               score = minimax(depth - 1, mySeed, alpha, beta)[0];
               if (score < beta) {
                  beta = score;
                  bestRow = move[0];
                  bestCol = move[1];
               }
            }
            // undo move
            cells[move[0]][move[1]].content = Seed.EMPTY;
            // cut-off
            if (alpha >= beta) break;
         }
         return new int[] {(player == mySeed) ? alpha : beta, bestRow, bestCol};
      }
   }

Related Solutions

Using Java please implement a 4x4 Tic-Tac-Toe with Minimax with alpha-beta pruning. PLease comment code heavily...
Using Java please implement a 4x4 Tic-Tac-Toe with Minimax with alpha-beta pruning. PLease comment code heavily so I can follow along and understand the steps. Thank you
For this assignment, you will implement the minimax algorithm with alpha-beta pruning in order to find...
For this assignment, you will implement the minimax algorithm with alpha-beta pruning in order to find the optimal move for a game of generalized tic-tac-toe. In this version of the game, the players can choose different board sizes, for example 4x4 or 5x5, instead of the normal 3x3. The game proceeds with the usual rules for tic-tac-toe (see https://en.wikipedia.org/wiki/Tic-tac-toe). Requirements You are to modify the mp2basecode (below) program to implement the alpha-beta search for making the computer’s move. This will...
make a 4x4 tic tac toe in javascript (X is the user) (O is the computer)
make a 4x4 tic tac toe in javascript (X is the user) (O is the computer)
If anyone can please write a code for a 5x5 tic tac toe game in matlab...
If anyone can please write a code for a 5x5 tic tac toe game in matlab I would greatly appreciate it. Its extra credit. PLEASE HELP ME :(
Provided is code for checking horizontal and vertical win in a tic tac toe game. Please...
Provided is code for checking horizontal and vertical win in a tic tac toe game. Please provide code for checking diagonal win. function checkWinVertical(player, row, col) { var counter = 0; //each button var btn = document.getElementsByTagName("button"); for (counterC = 0; counterC < col; counterC++){ for (countR = 0; countR < row; countR++){ if (player != btn[counter].innerHTML){ counter += 1; break; } else if (countR + 1 == col) { for (countW = 0; countW < col; countW++){ btn[counter -...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The...
The objective of this assignment is to implement the tic-tac-toe game with a C program. The game is played by two players on a board defined as a 5x5 grid (array). Each board position can contain one of two possible markers, either ‘X’ or ‘O’. The first player plays with ‘X’ while the second player plays with ‘O’. Players place their markers in an empty position of the board in turns. The objective is to place 5 consecutive markers of...
Write a LISP program to play the game Tic-Tac-Toe on a size 4x4 game board. Your...
Write a LISP program to play the game Tic-Tac-Toe on a size 4x4 game board. Your program must use min-max search and should be invoked by the function call: > (Tic-Tac-Toe) The game is single player, human vs the computer AI.
**MATLAB Code only Please create a MATLAB script that is a TIC-TAC-TOE game. Please keep it...
**MATLAB Code only Please create a MATLAB script that is a TIC-TAC-TOE game. Please keep it simple, but there is no other rules. Thank you.
please create a tic tac toe game with python using only graphics.py library
please create a tic tac toe game with python using only graphics.py library
Using C language Problem!! <3 x 3 tic tac toe> 2 players are doing tic tac...
Using C language Problem!! <3 x 3 tic tac toe> 2 players are doing tic tac toe alternatively. Anyone could win when they first make a bingo for any line(horizontal, vertical, diagonal) [Constraints] 1. Use 2dimensional Array(3 X 3) 2. Each elements are 1 or 2 so that can show their player1, player2's mark. 3. Inputs are 1 through 9 as you can see 1 2 3 4 5 6 7 8 9 4. No inputs are duplicated 5. if...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT