-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEngine.java
More file actions
387 lines (336 loc) · 12.7 KB
/
Engine.java
File metadata and controls
387 lines (336 loc) · 12.7 KB
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
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
package ChessBot;
import java.util.List;
import java.util.Random;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashSet;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
public class Engine {
final static private int pawnValue = 100;
final static private int knightValue = 320;
final static private int bishopValue = 330;
final static private int rookValue = 500;
final static private int queenValue = 900;
final static private int[] materialValues = new int[] {pawnValue, bishopValue, knightValue, rookValue, queenValue, 0};
final static private Random random = new Random();
private static ZobristTable transpositionTable;
private static int transpositionCount = 0;
final static private int[] whitePawnHeatMap = new int[] {
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
};
final static private int[] whiteKnightHeatMap = new int[] {
-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,
};
final static private int[] whiteBishopHeatMap = new int[] {
-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,
};
final static private int[] whiteRookHeatMap = new int[] {
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
};
final static private int[] whiteQueenHeatMap = new int[] {
-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
};
final static private int[] whiteKingHeatMap = new int[] {
-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
};
static private int[][] whiteMaps = new int[][] {whitePawnHeatMap, whiteBishopHeatMap, whiteKnightHeatMap, whiteRookHeatMap, whiteQueenHeatMap, whiteKingHeatMap};
static private int[] blackPawnHeatMap;
static private int[] blackKnightHeatMap;
static private int[] blackBishopHeatMap;
static private int[] blackRookHeatMap;
static private int[] blackQueenHeatMap;
static private int[] blackKingHeatMap;
static private int[][] blackMaps;
static public Grid currentGrid;
private static int[] reverseArray(int[] array) {
int[] toReturn = new int[array.length];
for (int i=0; i<array.length; i++) {
toReturn[i] = array[63 - i];
}
return toReturn;
}
public static void initMaps() {
blackPawnHeatMap = reverseArray(whitePawnHeatMap);
blackBishopHeatMap = reverseArray(whiteBishopHeatMap);
blackKnightHeatMap = reverseArray(whiteKnightHeatMap);
blackRookHeatMap = reverseArray(whiteRookHeatMap);
blackQueenHeatMap = reverseArray(whiteQueenHeatMap);
blackKingHeatMap = reverseArray(whiteKingHeatMap);
blackMaps = new int[][] {blackPawnHeatMap, blackBishopHeatMap, blackKnightHeatMap, blackRookHeatMap, blackQueenHeatMap, blackKingHeatMap};
}
public static void initTable(ZobristTable table) {
transpositionTable = table;
}
private static int[] sumMaterial(int[] grid) {
int material = 0;
int whitePieceCount = 0;
int blackPieceCount = 0;
for (int i=0; i<64; i++) {
material += getValueOf(grid[i]);
}
return new int[] {material, Math.min(whitePieceCount, blackPieceCount)};
}
/*
private static int moveGenerationTest(int depth) {
if (depth == 0) {
return 1;
}
int total = 0;
List<Move> moves = Moves.getAllLegalMoves(currentGrid, true, false);
for (Move move : moves) {
AntiMove antiMove = currentGrid.makeMove(move, false);
total += moveGenerationTest(depth - 1);
currentGrid.unmakeMove(move, antiMove, false);
}
return total;
}
*/
private static int search(int depth, int alpha, int beta) {
long hash = transpositionTable.getHash(currentGrid);
/*int ttEvaluation = transpositionTable.getEvaluation(hash, depth);
if (!(ttEvaluation == ZobristTable.lookupFailed)) {
transpositionCount++;
return ttEvaluation;
}*/
if (depth == 0) {
//return quiescenceSearch(alpha, beta);
return evaluateBoard(currentGrid);
}
List<Move> moves = Moves.getAllLegalMoves(currentGrid, true, false);
if (depth > 1) {
moves = orderMoves(moves);
}
if (moves.isEmpty()) {
if (currentGrid.getIsChecked(currentGrid.getIsWhiteTurn())) {
return Integer.MIN_VALUE + 10000 + depth * 100;
}
return 0;
}
int evaluation;
AntiMove reverseMove;
for (Move move : moves) {
reverseMove = currentGrid.makeMove(move, false);
evaluation = -search(depth - 1, -beta, -alpha);
if (evaluation >= beta) {
transpositionTable.addEvaluation(transpositionTable.getHash(currentGrid), depth, evaluation);
}
currentGrid.unmakeMove(move, reverseMove, false);
if (evaluation >= beta) {
return beta;
}
if (evaluation > alpha) {
alpha = evaluation;
}
}
transpositionTable.addEvaluation(hash, depth, alpha);
return alpha;
}
/*
private static int quiescenceSearch(int alpha, int beta) {
int eval = evaluateBoard(currentGrid);
if (eval >= beta) {
return beta;
}
if (eval > alpha) {
alpha = eval;
}
List<Move> moves = Moves.getAllLegalMoves(currentGrid, true, true);
for (Move move : moves) {
AntiMove antiMove = currentGrid.makeMove(move, false);
eval = -quiescenceSearch(-beta, -alpha);
currentGrid.unmakeMove(move, antiMove, false);
if (eval >= beta) {
return beta;
}
if (eval > alpha) {
alpha = eval;
}
}
return alpha;
}*/
private static int applyHeatMaps(int[] grid) {
int totalScore = 0;
int pieceValue;
for (int i=0; i<64; i++) {
pieceValue = grid[i];
if (pieceValue > 0) {
if ((pieceValue & 8) == 0) {
totalScore += whiteMaps[pieceValue - 1][i];
} else {
totalScore -= blackMaps[pieceValue - 9][i];
}
}
}
return totalScore;
}
private static int evaluateBoard(Grid grid) {
int totalScore = 0;
final int[] materialStats = sumMaterial(grid.getGridValues());
final int material = materialStats[0];
totalScore += material;
totalScore += applyHeatMaps(grid.getGridValues());
return totalScore;
}
private static int getValueOf(int piece) {
if (piece > 8) {
return -materialValues[piece - 9];
} else if (piece > 0) {
return materialValues[piece - 1];
}
return 0;
}
private static List<Move> orderMoves(List<Move> moves) {
final int[] grid = currentGrid.getGridValues();
final boolean isTurnWhite = currentGrid.getIsWhiteTurn();
final HashSet<Integer> attackedSquares = currentGrid.getAttackedSquares(isTurnWhite);
for (int i=0; i<moves.size(); i++) {
final Move move = moves.get(i);
final int destinationSquare = move.endSquare;
final int capturePiece = grid[destinationSquare];
final int takingPiece = grid[moves.get(i).startSquare];
int score = 10 * getValueOf(capturePiece) - getValueOf(takingPiece);
if (attackedSquares.contains(destinationSquare)) {
score -= getValueOf(capturePiece);
}
move.setSortValue(score);
}
Collections.sort(moves, new Comparator<Move>() {
@Override
public int compare(Move move1, Move move2) {
return move1.getSortValue() - move2.getSortValue();
}
});
return moves;
}
public static void makeComputerBookMove(Grid grid, int depth) {
List<String[]> openings = new ArrayList<>();
BufferedReader br;
try {
br = new BufferedReader(new FileReader(GUI.pathStart + "Assets/Openings.txt"));
String line = br.readLine();
while (!(line == null)) {
openings.add(line.split(" "));
line = br.readLine();
}
br.close();
} catch (IOException e) {
System.out.println("Error reading openings file");
}
final List<String> moves = grid.getMoves();
final int halfMoves = grid.getHalfMoves();
for (int i=0; i<halfMoves; i++) {
final List<String[]> tempMoves = new ArrayList<>();
for (String[] sequence : openings) {
if (sequence[i].equals(moves.get(i))) {
tempMoves.add(sequence);
}
}
openings = new ArrayList<>();
openings.addAll(tempMoves);
}
if (openings.isEmpty()) {
System.out.println("No book move found");
makeComputerMove(grid, depth);
} else {
final String[] choseOpening = openings.get(random.nextInt(openings.size()));
System.out.println(choseOpening[halfMoves]);
final Move move = grid.codeToMove(choseOpening[halfMoves]);
grid.makeMove(move, true);
}
}
public static void makeComputerMove(Grid grid, int depth) {
System.out.println("Depth: ");
System.out.println(depth);
final long startTime = System.nanoTime();
currentGrid = grid;
List<Move> moves = Moves.getAllLegalMoves(grid, true, false);
int evaluation;
Move bestMove = null;
AntiMove reverseMove;
int alpha = Integer.MIN_VALUE + 1000;
int beta = Integer.MAX_VALUE - 1000;
if (moves.isEmpty()) {
if (currentGrid.getIsChecked(false)) {
System.out.println("White wins by checkmate");
} else {
System.out.println("Stalemate");
}
return;
}
moves = orderMoves(moves);
int i = 1;
for (Move move : moves) {
reverseMove = currentGrid.makeMove(move, false);
evaluation = -search(depth, -beta, -alpha);
currentGrid.unmakeMove(move, reverseMove, false);
if (evaluation > alpha) {
alpha = evaluation;
bestMove = move;
}
if (System.nanoTime() - startTime > 15000000000L) {
System.out.println(i);
System.out.println("/");
System.out.println(moves.size());
System.out.println("");
}
i++;
}
if (System.nanoTime() - startTime < 200) {
makeComputerMove(grid, depth + 2);
return;
}
transpositionCount = 0;
grid.makeMove(bestMove, true);
System.out.println("Evaluation: ");
System.out.println(alpha / 100);
System.out.println("");
}
public int getTranspositionCount() {
return transpositionCount;
}
}