Package com.neven.ticTacToeGame.model
Class MiniMaxAlgorithm
- java.lang.Object
-
- com.neven.ticTacToeGame.model.MiniMaxAlgorithm
-
public class MiniMaxAlgorithm extends java.lang.Object
TheMiniMaxAlgorithm
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 constructorGame
class.private char
player
Field with character to mark a player on game field.
-
Constructor Summary
Constructors Constructor Description MiniMaxAlgorithm(Game game)
Constructor for classMiniMaxAlgorithm
.
Constructor initializedgame
field by incoming paramPlayerVsAlgorithmGame
value.
-
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 thegame
by using methodgetBordFromGame()
and for each empty cell on game field will calledminiMax(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()
private java.lang.Boolean
isMovesLeft(char[][] board)
The method takes game board ang check if any ofemptyCell
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 methodevaluate(char[][])
).The method call itself, increasing deepness of the "tree" with all possible game combination, till game will be finished.
-
-
-
Field Detail
-
game
private final Game game
Field with an injected in constructorGame
class. This field used for take from gameGame.cells
and evaluate game field cost for making best move currently for thisGame
.
-
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
-
-
Constructor Detail
-
MiniMaxAlgorithm
public MiniMaxAlgorithm(Game game)
Constructor for classMiniMaxAlgorithm
.
Constructor initializedgame
field by incoming paramPlayerVsAlgorithmGame
value.- Parameters:
game
-PlayerVsAlgorithmGame
in what should be made best algorithm move.
-
-
Method Detail
-
findBestMove
public int findBestMove()
This method takes game board from thegame
by using methodgetBordFromGame()
and for each empty cell on game field will calledminiMax(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 thegame
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 byplayer
,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 methodevaluate(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 methodgetBordFromGame()
.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 ofemptyCell
left on game field.- Parameters:
board
-char[][]
board which can be received by using a methodgetBordFromGame()
.- Returns:
Boolean
value is anyemptyCell
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 returned0
(zero).- Parameters:
board
-char[][]
board which can be received by using a methodgetBordFromGame()
.- Returns:
- evaluated cost of the game board, acceptable values
-10
,+10
,0
(zero)
-
-