Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help Implementing Adversarial Search Using MiniMax for this TicTacToe game. I cant change any of the code except where the comments say TODO.

I need help Implementing Adversarial Search Using MiniMax for this TicTacToe game. I cant change any of the code except where the comments say TODO.

******************************Minimax.Java*************************************

import java.util.ArrayList;

public class Minimax { private static String AI_LETTER = "O"; private static String PLAYER_LETTER = "X"; /** * * This will recursively call Minimax depending on the current player, if the * current player is O, the algorithm will find the MAX available board and if * the current player is X, the algorithm will find the MIN avaiLable board. * * We assume that the human player is X and that the AI is O * * The terminal state check is done at the start before recursively calling * Minimax, the terminal checks are checkWinner for player X(Human) and O(AI) and * if the board state is full, if either of the conditions gets satisfied then * it will return the value as decided if winner is AI(O), assign +1, if * winner is User(X) assign -1 and if the state is draw assign 0 and return * * @param state * board for which the Minimax will be called recursively * @param player * player for whom the game state should be generated * @return boolean true/false **/ public static int miniMax(GameState state, String player) { GameAI.setTotalCount(GameAI.getTotalCount() + 1); // TODO: Implement the minimax function /* HINTS: * * Use Double.NEGATIVE_INFINITY and Double.POSITIVE_INFINITY * for the initialization values * * Use the checkWinner method in GameState to check leaf nodes * * Use boardFullCheck method in GameState to check for tied games * * Use the printBoardStateMax to produce debug output * */ // DEBUG OUTPUT CODE // Log.debug("Inside maxValue " + " " + value); // Log.debug("Inside minValue " + " " + best); return 0;

} }

******************************GameAI.Java*************************************

import java.util.ArrayList; import java.util.Random;

public class GameAI { private static String AI_LETTER = "O"; private static String PLAYER_LETTER = "X"; public static int BOARD_SIZE = 3; static int drawset = 0; private static int totalCount;

String[] boardState;

GameAI() { boardState = new String[BOARD_SIZE * BOARD_SIZE]; } public void updateState(String[] state) { boardState = state; Log.debug("Printing the board state after player plays"); showBoardState(boardState); } public String[] getNextMove() { GameState state = new GameState(BOARD_SIZE, boardState);

// Calling Minimax algorithm Log.debug("Starting minimax..."); findBestBoardMinimax(state); return boardState; } /* * This method shows the state of the game board. * * The buttons are numbered from zero to 8, hence * the layout is as follows: * * [0] [1] [2] * [3] [4] [5] * [6] [7] [8] */ private static void showBoardState(String[] state) { StringBuilder sb = new StringBuilder(); sb.append("Board State:"); for (int row = 0; row < BOARD_SIZE; row++) { sb.append(" \t"); for (int col = 0; col < BOARD_SIZE; col++) { sb.append(String.format("[%s] ", state[(row*BOARD_SIZE)+col])); } } Log.debug(sb.toString()); } /** * * Updates the boardState with the play the AI thinks is most suitable according * to the algorithm, here the algorithm is simple Minimax, which will generate * the whole tree and assign the utility value at the leaf nodes. The values * assigned are +1000 for winner of AI, -1000 for winner of user and 0 if draw * The values are then backed up to the original list of available * generatedBoards and the best of the board is selected. * * @param state * Board state of the current boardState * @return void * **/ public void findBestBoardMinimax(GameState state) { // TODO: Replace this code with your own implementation /* * This code must do the following: * i. Take the current game state and generate the game tree, * i.e., the successors. A method has been provided to do * most of the necessary work. See the generateSuccessors * method in the GameState class. * * ii. Keep track of the state with the highest utility * * iii. Invoke miniMax on all the successors, i.e., the tree nodes * (see the Minimax class) * */ // DEBUG OUTPUT CODE // Log.debug("Selected State Value tmpwinnerVal " + tmpwinnerVal); // // Log.debug("Selected State Value " + winnerVal); // This code chooses a tile at random to simulate the AI's move Random random = new Random(); /* * Continue generating random tile numbers * until a empty tile is found */ while (true) { int tileIndex = random.nextInt(9); if (boardState[tileIndex] == "-") { boardState[tileIndex] = AI_LETTER; break; } } showBoardState(boardState); }

static void showTotalCount() { Log.debug("Total states generated/explored " + getTotalCount()); }

public static int getTotalCount() { return totalCount; }

public static void setTotalCount(int totalCount) { GameAI.totalCount = totalCount; } }

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

Question

How flying airoplane?

Answered: 1 week ago