forked from LichessBot-Coders/Lichess-Coded-Bot
-
Notifications
You must be signed in to change notification settings - Fork 12
/
MinimaxAI.java
319 lines (267 loc) · 9.67 KB
/
MinimaxAI.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
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
package chai;
import java.util.Random;
import java.util.Arrays;
import java.util.List;
import java.util.Collections;
import java.util.ArrayList;
import chesspresso.position.Position;
import chesspresso.move.IllegalMoveException;
import chesspresso.Chess;
public class MinimaxAI implements ChessAI {
static int winVal = 1000000;
static int loseVal = -1000000;
private final Object mutex = new Object();
int maxDepth;
int tempDepth;
int IDdepth;
int player;
public MinimaxAI(int d, int whiteOrBlack) {
this.player = whiteOrBlack;
this.maxDepth = d;
}
public short getMove(Position position) {
return minimaxDecision(position);
}
// Spot Precedence for White Pawns
protected static int [] pawnTable = {
0, 0, 0, 0, 0, 0, 0, 0,
50, 50, 50, 50, 50, 50, 50, 50,
10, 10, 20, 30, 30, 20, 10, 10,
5, 5, 10, 25, 25, 10, 5, 5,
0, 0, 0, 20, 20, 0, 0, 0,
5, -5,-10, 0, 0,-10, -5, 5,
5, 10, 10,-20,-20, 10, 10, 5,
0, 0, 0, 0, 0, 0, 0, 0 };
// Placement Precedence for all Knights
protected static int [] knightTable = {
-50,-40,-30,-30,-30,-30,-40,-50,
-40,-20, 0, 0, 0, 0,-20,-40,
-30, 0, 10, 15, 15, 10, 0,-30,
-30, 5, 15, 20, 20, 15, 5,-30,
-30, 0, 15, 20, 20, 15, 0,-30,
-30, 5, 10, 15, 15, 10, 5,-30,
-40,-20, 0, 5, 5, 0,-20,-40,
-50,-40,-30,-30,-30,-30,-40,-50 };
protected static int [] bishopPrecedence = {
-20,-10,-10,-10,-10,-10,-10,-20,
-10, 0, 0, 0, 0, 0, 0,-10,
-10, 0, 5, 10, 10, 5, 0,-10,
-10, 5, 5, 10, 10, 5, 5,-10,
-10, 0, 10, 10, 10, 10, 0,-10,
-10, 10, 10, 10, 10, 10, 10,-10,
-10, 5, 0, 0, 0, 0, 5,-10,
-20,-10,-10,-10,-10,-10,-10,-20 };
protected static int [] rookTable = {
0, 0, 0, 0, 0, 0, 0, 0,
5, 10, 10, 10, 10, 10, 10, 5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
0, 0, 0, 5, 5, 0, 0, 0 };
protected static int [] queenTable = {
-20,-10,-10, -5, -5,-10,-10,-20,
-10, 0, 0, 0, 0, 0, 0,-10,
-10, 0, 5, 5, 5, 5, 0,-10,
-5, 0, 5, 5, 5, 5, 0, -5,
0, 0, 5, 5, 5, 5, 0, -5,
-10, 5, 5, 5, 5, 5, 0,-10,
-10, 0, 5, 0, 0, 0, 0,-10,
-20,-10,-10, -5, -5,-10,-10,-20 };
protected static int [] kingTable = {
-30,-40,-40,-50,-50,-40,-40,-30,
-30,-40,-40,-50,-50,-40,-40,-30,
-30,-40,-40,-50,-50,-40,-40,-30,
-30,-40,-40,-50,-50,-40,-40,-30,
-20,-30,-30,-40,-40,-30,-30,-20,
-10,-20,-20,-20,-20,-20,-20,-10,
20, 20, 0, 0, 0, 0, 20, 20,
20, 30, 10, 0, 0, 10, 30, 20 };
/*
* Returns the maximum of the minimum possibilities
*/
private synchronized short minimaxDecision(Position position) {
for (IDdepth = 1; IDdepth < maxDepth; IDdepth++) {
short [] moves = position.getAllMoves();
int currUtil = loseVal;
int bestUtil = loseVal;
int bestIdx = 0;
short currMove;
this.tempDepth = this.IDdepth;
for (int i = 0; i < moves.length; i++) {
try {
currMove = moves[i];
position.doMove(currMove);
currUtil = minValue(position, 1);
position.undoMove();
// Use the last (and thus shortest) path to victory!
if (bestUtil <= currUtil) {
bestUtil = currUtil;
bestIdx = i;
}
} catch (Exception e) {
System.out.println(e);
System.out.println("Invalid Move at start state...");
}
}
if (bestUtil == winVal)
return(moves[bestIdx]);
if ((IDdepth+1) == maxDepth)
return(moves[bestIdx]);
}
// Return the move with the best utility at the highest depth
return -1;
}
private synchronized int maxValue(Position position, int currDepth) {
if (cutOffTest(position, currDepth)) {
return utility(position, currDepth);
}
int util = loseVal;
int curr;
short [] moves = position.getAllMoves();
for (int i = 0; i < moves.length; i++) {
try {
position.doMove(moves[i]);
curr = minValue(position, currDepth + 1);
position.undoMove();
if (curr > util)
util = curr;
} catch(Exception e) {
System.out.println("Illegal move found...");
}
}
return util;
}
private synchronized int minValue(Position position, int currDepth) {
if (cutOffTest(position, currDepth)) {
return utility(position, currDepth);
}
int util = winVal;
int curr;
short [] moves = position.getAllMoves();
for (int i = 0; i < moves.length; i++) {
try {
position.doMove(moves[i]);
curr = maxValue(position, currDepth);
position.undoMove();
if (curr < util)
util = curr;
} catch(Exception e) {
System.out.println("Illegal move found...");
}
}
return util;
}
private boolean cutOffTest(Position position, int currDepth) {
return (position.isTerminal() || (currDepth > this.tempDepth));
}
/*
* Returns winVal or loseVal on a mate, 0 on a draw
* otherwise, returns a random value in the range [loseVal, winVal]
*/
private int utility(Position position, int d) {
if (position.isTerminal()) {
//System.out.println("Terminal position found at depth " + d);
// Case of a mate
if (position.isMate()) {
if (position.getToPlay() == this.player) {
//System.out.println("Found a loss, returning " + loseVal);
return loseVal;
} else {
if (d <= this.tempDepth) {
//System.out.println("Replacing tempDepth with " + d);
this.tempDepth = d;
return winVal;
} else {
return loseVal;
}
//System.out.println("Found a win, returning " + winVal + " DepthLimit = " + this.tempDepth);
}
} else {
//System.out.println("Found a draw, returning 0");
return 0;
}
} else { // Non-terminal position
return getMaterials(position);
}
}
public int getMaterials(Position p) {
int currStone = Chess.NO_STONE, row, col;
int val = 0;
int wPawns = 0, wRooks = 0, wBishops = 0, wQueens = 0, wKnights = 0, wKings = 0;
int bPawns = 0, bRooks = 0, bBishops = 0, bQueens = 0, bKnights = 0, bKings = 0;
for (int i = 0; i < Chess.NUM_OF_SQUARES; i++) {
currStone = p.getStone(i);
row = i / 8; // EG B3 = 17, 17%8 = 1, 1 = B
col = i % 8; // With integer division => 17/8 = 2 (counting from zero)
switch(currStone) {
case Chess.WHITE_PAWN:
wPawns++;
val += (100 + (pawnTable[8*(7 - row) + col]));
break;
case Chess.BLACK_PAWN:
bPawns++;
val -= (100 + (pawnTable[8 + (8* row) - (1 +col)]));
break;
case Chess.WHITE_ROOK:
wRooks++;
val += (500 + (rookTable[8*(7 - row) + col]));
break;
case Chess.BLACK_ROOK:
bRooks++;
val -= (500 + (rookTable[8 + (8* row) - (1 +col)]));
break;
case Chess.WHITE_BISHOP:
wBishops++;
val += (330 + (rookTable[8*(7 - row) + col]));
break;
case Chess.BLACK_BISHOP:
bBishops++;
val -= (330 + (rookTable[8 + (8* row) - (1 +col)]));
break;
case Chess.WHITE_KNIGHT:
wKnights++;
val += (320 + (knightTable[8*(7 - row) + col]));
break;
case Chess.BLACK_KNIGHT:
bKnights++;
val -= (320 + (knightTable[8 + (8* row) - (1 +col)]));
break;
case Chess.WHITE_QUEEN:
wQueens++;
val += (900 + (queenTable[8*(7 - row) + col]));
break;
case Chess.BLACK_QUEEN:
bQueens++;
val -= (900 + (queenTable[8 + (8* row) - (1 +col)]));
break;
case Chess.WHITE_KING:
wKings++;
val += (20000 + (kingTable[8*(7 - row) + col]));
break;
case Chess.BLACK_KING:
bKings++;
val -= (20000 + (kingTable[8 + (8* row) - (1 +col)]));
break;
}
}
/* Customizations: */
// Penalty for no-pawns
if (wPawns == 0) {
val -= 20;
}
if (bPawns == 0) {
val += 20;
}
// Bonus for bishop pair
if (wBishops == 2) {
val += 40;
}
if (bBishops == 2) {
val -= 40;
}
// Everything Calculated with respect to white
return (this.player == Chess.WHITE ? val : -1 * val);
}
}