Class MiniMaxAlgorithm


  • public class MiniMaxAlgorithm
    extends java.lang.Object
    The MiniMaxAlgorithm class is a class which contain The Algorithm MiniMax and methods that allow to The Algorithm MiniMax make an analysis of the game field, evaluate any possible future moves of algorithm and user and make a chose what move will be less harmful for the result of the game. Best result of the game will be The Algorithm MiniMax winning, so The Algorithm MiniMax always will try to win and less harmful for the result of the game wil be the draw.
    As base for this class was this article and code from examples. Code was refactored for current application needs.
    Author:
    Arterm Koliushko, https://www.linkedin.com/in/artem-koliushko/
    See Also:
    MiniMax on Wikipedia, Introduction to Minimax Algorithm with a Java Implementation, Mini-Max Algorithm in Artificial Intelligence
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private char algorithm
      Field with character to mark an algorithm on game field.
      private char emptyCell
      Field with character to mark empty a cell on game field.
      private Game game
      Field with an injected in constructor Game class.
      private char player
      Field with character to mark a player on game field.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      private int evaluate​(char[][] board)
      The method receive game board and check is any of winning lines was finished.
      int findBestMove()
      This method takes game board from the game by using method getBordFromGame() and for each empty cell on game field will called miniMax(char[][] gameField, int depth, Boolean isMax) method to figure out cost of this move if algorithm choose this cell for the best move.
      private char[][] getBordFromGame()
      This method takes game board from the game and refactor it from the line view to a classic view:
      Line view:
      (1, 2, 3, 4, 5, 6, 7, 8, 9)
      Classic view:
      1|2|3
      4|5|6
      7|8|9
      All moves on game field also marked on the board by player, algorithm, emptyCell) characters.
      private java.lang.Boolean isMovesLeft​(char[][] board)
      The method takes game board ang check if any of emptyCell left on game field.
      private int miniMax​(char[][] board, int depth, java.lang.Boolean isMax)
      This method is a recursive method which evaluate all possible results of the game and return value of game board (using method evaluate(char[][])).The method call itself, increasing deepness of the "tree" with all possible game combination, till game will be finished.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • game

        private final Game game
        Field with an injected in constructor Game class. This field used for take from game Game.cells and evaluate game field cost for making best move currently for this Game.
      • player

        private final char player
        Field with character to mark a player on game field.
        See Also:
        Constant Field Values
      • algorithm

        private final char algorithm
        Field with character to mark an algorithm on game field.
        See Also:
        Constant Field Values
      • emptyCell

        private final char emptyCell
        Field with character to mark empty a cell on game field.
        See Also:
        Constant Field Values
    • Method Detail

      • findBestMove

        public int findBestMove()
        This method takes game board from the game by using method getBordFromGame() and for each empty cell on game field will called miniMax(char[][] gameField, int depth, Boolean isMax) method to figure out cost of this move if algorithm choose this cell for the best move. First empty cell which will has more points will be returned as best move.
        Returns:
        Integer with a number of cell where an Algorithm MiniMax will made a move
      • getBordFromGame

        private char[][] getBordFromGame()
        This method takes game board from the game and refactor it from the line view to a classic view:
        Line view:
        (1, 2, 3, 4, 5, 6, 7, 8, 9)
        Classic view:
        1|2|3
        4|5|6
        7|8|9
        All moves on game field also marked on the board by player, algorithm, emptyCell) characters.
        Returns:
        char[3][3] with refactored game board and all moves made in the game.
      • miniMax

        private int miniMax​(char[][] board,
                            int depth,
                            java.lang.Boolean isMax)
        This method is a recursive method which evaluate all possible results of the game and return value of game board (using method evaluate(char[][])).The method call itself, increasing deepness of the "tree" with all possible game combination, till game will be finished.
        Parameters:
        board - char[][] board which can be received by using a method getBordFromGame().
        depth - deepness of the "tree" with all possible game combination.
        isMax - Boolean value is the MiniMax Algorithm move turn or an user
        (true - algorithm, false - user).
        Also called as Maximizer and Minimizer.
        Returns:
        evaluated cost of the game board, acceptable values -10, +10, 0(zero)
        See Also:
        Mini-Max Algorithm in Artificial Intelligence
      • isMovesLeft

        private java.lang.Boolean isMovesLeft​(char[][] board)
        The method takes game board ang check if any of emptyCell left on game field.
        Parameters:
        board - char[][] board which can be received by using a method getBordFromGame().
        Returns:
        Boolean value is any emptyCell left on game field.
      • evaluate

        private int evaluate​(char[][] board)
        The method receive game board and check is any of winning lines was finished.
        According to the amount of the game field cells and rules of the game it can be only 8 lines on which a player or the algorithm can make a winning combination from 3 moves in a row.
        According needs of the algorithm this method evaluate cost of the game field. If an user make a winning line will be returned negative value(-10), if an algorithm make a winning line will be returned positive value(+10), if no one makes a winning line will be returned 0(zero).
        Parameters:
        board - char[][] board which can be received by using a method getBordFromGame().
        Returns:
        evaluated cost of the game board, acceptable values -10, +10, 0(zero)