-
Notifications
You must be signed in to change notification settings - Fork 0
/
MiniMax.java
162 lines (145 loc) · 5.15 KB
/
MiniMax.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
package ai;
import game.Board;
import static game.Mark.*;
/**
* The MiniMax algorithm in its most basic form.
*
* @author DavidHurst
*/
public class MiniMax {
private static final int MAX_DEPTH = 6;
private MiniMax() {
}
/**
* Play moves on the board alternating between playing as X and O analysing
* the board each time to return the value of the highest value move for the
* X player. Return the highest value move when a terminal node or the
* maximum search depth is reached.
* @param board Board to play on and evaluate
* @param depth The maximum depth of the game tree to search to
* @param isMax Maximising or minimising player
* @return Value of the board
*/
public static int miniMax(Board board, int depth, boolean isMax) {
int boardVal = evaluateBoard(board);
// Terminal node (win/lose/draw) or max depth reached.
if (Math.abs(boardVal) == 10 || depth == 0
|| !board.anyMovesAvailable()) {
return boardVal;
}
// Maximising player, find the maximum attainable value.
if (isMax) {
int highestVal = Integer.MIN_VALUE;
for (int row = 0; row < board.getWidth(); row++) {
for (int col = 0; col < board.getWidth(); col++) {
if (!board.isTileMarked(row, col)) {
board.setMarkAt(row, col, X);
highestVal = Math.max(highestVal, miniMax(board,
depth - 1, false));
board.setMarkAt(row, col, BLANK);
}
}
}
return highestVal;
// Minimising player, find the minimum attainable value;
} else {
int lowestVal = Integer.MAX_VALUE;
for (int row = 0; row < board.getWidth(); row++) {
for (int col = 0; col < board.getWidth(); col++) {
if (!board.isTileMarked(row, col)) {
board.setMarkAt(row, col, O);
lowestVal = Math.min(lowestVal, miniMax(board,
depth - 1, true));
board.setMarkAt(row, col, BLANK);
}
}
}
return lowestVal;
}
}
/**
* Evaluate every legal move on the board and return the best one.
* @param board Board to evaluate
* @return Coordinates of best move
*/
public static int[] getBestMove(Board board) {
int[] bestMove = new int[]{-1, -1};
int bestValue = Integer.MIN_VALUE;
for (int row = 0; row < board.getWidth(); row++) {
for (int col = 0; col < board.getWidth(); col++) {
if (!board.isTileMarked(row, col)) {
board.setMarkAt(row, col, X);
int moveValue = miniMax(board, MAX_DEPTH, false);
board.setMarkAt(row, col, BLANK);
if (moveValue > bestValue) {
bestMove[0] = row;
bestMove[1] = col;
bestValue = moveValue;
}
}
}
}
return bestMove;
}
/**
* Evaluate the given board from the perspective of the X player, return
* 10 if a winning board configuration is found, -10 for a losing one and 0
* for a draw.
* @param board Board to evaluate
* @return value of the board
*/
private static int evaluateBoard(Board board) {
int rowSum = 0;
int bWidth = board.getWidth();
int Xwin = X.getMark() * bWidth;
int Owin = O.getMark() * bWidth;
// Check rows for winner.
for (int row = 0; row < bWidth; row++) {
for (int col = 0; col < bWidth; col++) {
rowSum += board.getMarkAt(row, col).getMark();
}
if (rowSum == Xwin) {
return 10;
} else if (rowSum == Owin) {
return -10;
}
rowSum = 0;
}
// Check columns for winner.
rowSum = 0;
for (int col = 0; col < bWidth; col++) {
for (int row = 0; row < bWidth; row++) {
rowSum += board.getMarkAt(row, col).getMark();
}
if (rowSum == Xwin) {
return 10;
} else if (rowSum == Owin) {
return -10;
}
rowSum = 0;
}
// Check diagonals for winner.
// Top-left to bottom-right diagonal.
rowSum = 0;
for (int i = 0; i < bWidth; i++) {
rowSum += board.getMarkAt(i, i).getMark();
}
if (rowSum == Xwin) {
return 10;
} else if (rowSum == Owin) {
return -10;
}
// Top-right to bottom-left diagonal.
rowSum = 0;
int indexMax = bWidth - 1;
for (int i = 0; i <= indexMax; i++) {
rowSum += board.getMarkAt(i, indexMax - i).getMark();
}
if (rowSum == Xwin) {
return 10;
} else if (rowSum == Owin) {
return -10;
}
return 0;
}
}