The MiniMax Algorithm

The MiniMax algorithm is a generic artificial intelligence algorithm which is meant to process the best next move for the computer. MiniMax can work with plenty of board games and logic games.

The following code is written in Java, and can be applied for many other games.

MiniMax builds a game's moves tree like in the following picture, where every possible game move is being processed until the maximum tree depth that was defined by programmer.

Please be noted that the higher the tree depth is, the more processing power it takes to calculate the next best move, the more time it takes for the computer to make its move. So, you will need to find out by yourself what is the best tree depth to define in your game.

The function input is:

* @param state - An object of the "State" Class that represents the current state of the game.

* @param depth - A number that represents the game moves' tree depth that the MiniMax algorithm will use. In order to go to the lowest depth in the tree, the input will be -1.

* @param maxRunningTime A number that represents the maximum time in milliseconds that the MiniMax algorithm will run for. For unlimited running time, please send an input of "null".

* @param isMax - A Boolean variable that represents who the current player is.

"True" means that the current player is the computer and , "False" means that the current player is the human player.

* @return - The function returns an object of the "State" class that represents a game state after the computer made his best move.

The following things need to be added to your code in order for the algorithm to work properly:

  • A "State" class that represents a game state.

In the same way we as humans see the current game state with our eyes, the computer needs to see it with variables, arrays and objects.

The class must have the following functions:

void setLastMove(Move move) - This function sets the last move that was done at the current game state.

Move getLastMove() - This function returns the last move that was done at the current game state.

void setScore(int score) - This function sets how good the current game state is for the computer.

int getScore() - This function returns how good the current game state is for the computer.

  • A "Move" class that represents a move that can be done in a given game state.
  • The ArryaList<States> getAllLegalStates(State state,Boolean isMax) function gets the current game state and a boolean value.

If the "isMax" variable is true, the function returns an array list of the possible next game moves for the computer,

and if "isMax" variable is false, the function returns an array list of the possibles next game moves for the human player.

  • The int eval(State state) function that gets a game state and returns an int value that tells how good the game state is for the computer. For example, a value of 100 is the best for the computer, and a value of -100 is the worst for the computer.
  • The Boolean isGameOver(State state) function that gets a game state and returns "true" if the game is over in this state, and otherwise it returns "false".