-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFangEngine.cpp
More file actions
567 lines (502 loc) · 13.3 KB
/
FangEngine.cpp
File metadata and controls
567 lines (502 loc) · 13.3 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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
#include "FangEngine.h"
#include <algorithm>
#include <bitset>
#define INF INT_MAX
FangEngine::FangEngine()
{
playing = true;
}
FangEngine::FangEngine(Game* g)
{
playing = true;
game = g;
// set up openings for white
// play king's pawn
openings["rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"] = "e4";
// play Vienna
openings["rnbqkbnr/pppp1ppp/8/4p3/4P3/8/PPPP1PPP/RNBQKBNR w KQkq e6 0 2"] = "Nc3";
// play open Sicilian
openings["rnbqkbnr/pp1ppppp/8/2p5/4P3/8/PPPP1PPP/RNBQKBNR w KQkq c6 0 2"] = "Nf3";
// set up openings for black
// play king's pawn
openings["rnbqkbnr/pppppppp/8/8/4P3/8/PPPP1PPP/RNBQKBNR b KQkq e3 0 1"] = "e5";
// play Indian defense
openings["rnbqkbnr/pppppppp/8/8/3P4/8/PPP1PPPP/RNBQKBNR b KQkq d3 0 1"] = "Nf6";
openings["rnbqkb1r/pppppppp/5n2/8/2PP4/8/PP2PPPP/RNBQKBNR b KQkq c3 0 2"] = "g6";
}
FangEngine::~FangEngine()
{
;
}
// calls Minimax algorithm
Move* FangEngine::search(int depth)
{
Move* move = new Move;
// check for book move
std::string currFEN = game->getFEN();
std::cout << currFEN << '\n';
if (openings.find(currFEN) != openings.end())
{
Move* tmp = game->strToMove(openings[currFEN]);
if (tmp)
{
std::cout << "Using book move!\n";
move = tmp->cloneMove();
return move;
}
else std::cout << "Book move failed! Check strToMove()\n";
}
// if we are in an endgame, boost searching
if (isEndGame())
{
// count number of pieces left
uint64_t boardWithoutKings = game->pieceBoards[WP_INDEX] | game->pieceBoards[WN_INDEX] |
game->pieceBoards[WB_INDEX] | game->pieceBoards[WR_INDEX] | game->pieceBoards[WQ_INDEX] |
game->pieceBoards[BP_INDEX] | game->pieceBoards[BN_INDEX] | game->pieceBoards[BB_INDEX] |
game->pieceBoards[BR_INDEX] | game->pieceBoards[BQ_INDEX];
int popcnt = (int)(std::bitset<64>(boardWithoutKings).count());
int oldDepth = depth;
if (popcnt < 7) depth += 4;
else if (popcnt < 10) depth += 3;
else if (popcnt < 13) depth += 2;
else if (popcnt < 17) depth++;
std::cout << "Endgame detected! Depth increased by " << depth - oldDepth << '\n';
}
double evaluation = minimax(depth, depth, (game->turn % 2 == 0), -INF, INF, &move);
std::cout << "Evaluation: " << evaluation << '\n';
return move;
}
// uses alpha-beta pruning
double FangEngine::minimax(int depth, int trueDepth, bool maxer, double alpha, double beta, Move** move)
{
std::vector<Move*> moves;
game->generateLegalMoves(moves);
std::sort(moves.begin(), moves.end());
if (depth == 0 || moves.size() == 0)
{
for (auto p : moves)
{
if (p != *move) delete p;
}
int end = game->isCheckmate(game->turn);
if (end == -1) return eval();
else if (end == 0 || end == DRAW_BY_FIFTY || end == DRAW_BY_MATERIAL || end == DRAW_BY_REPETITION) return 0;
else
{
if (maxer) return -10000 + (trueDepth - depth); // white is checkmated, return based on mate quickness
else return 10000 - (trueDepth - depth); // black is checkmated, return based on mate quickness
}
}
if (maxer) // white maximizes
{
double maxEval, eval;
maxEval = -INF; // worst possible eval for white
for (auto m : moves)
{
game->makeMove(m);
eval = minimax(depth - 1, false, false, alpha, beta, move);
if (eval > maxEval)
{
maxEval = eval;
if (depth == trueDepth) *move = m;
}
alpha = std::max(alpha, eval); // change lower bound if necessary
game->unmakeMove(m);
if (beta <= alpha) break; // black will never go down this branch; has better option
}
for (auto p : moves)
{
if (p != *move) delete p;
}
return maxEval;
}
else // black minimizes
{
double minEval, eval;
minEval = INF; // worst possible eval for black
for (auto m : moves)
{
game->makeMove(m);
eval = minimax(depth - 1, false, true, alpha, beta, move);
if (eval < minEval)
{
minEval = eval;
if (depth == trueDepth) *move = m;
}
beta = std::min(beta, eval); // change upper bound if necessary
game->unmakeMove(m);
if (beta <= alpha) break; // black will never go down this branch; has better option
}
for (auto p : moves)
{
if (p != *move) delete p;
}
return minEval;
}
}
bool FangEngine::isEndGame()
{
uint64_t tmp;
int whiteMaterial = 0, blackMaterial = 0;
// loop over white pawns
tmp = game->pieceBoards[WP_INDEX];
while (tmp)
{
unsigned long index;
char ch = _BitScanForward64(&index, tmp);
if (ch)
{
whiteMaterial += PAWN_VALUE;
}
else std::cout << "Something wrong with white pawns in eval()!\n";
tmp &= tmp - 1;
}
// loop over white knights
tmp = game->pieceBoards[WN_INDEX];
while (tmp)
{
unsigned long index;
char ch = _BitScanForward64(&index, tmp);
if (ch)
{
whiteMaterial += KNIGHT_VALUE;
}
else std::cout << "Something wrong with white knights in eval()!\n";
tmp &= tmp - 1;
}
// loop over white bishop
tmp = game->pieceBoards[WB_INDEX];
while (tmp)
{
unsigned long index;
char ch = _BitScanForward64(&index, tmp);
if (ch)
{
whiteMaterial += BISHOP_VALUE;
}
else std::cout << "Something wrong with white bishops in eval()!\n";
tmp &= tmp - 1;
}
// loop over white rooks
tmp = game->pieceBoards[WR_INDEX];
while (tmp)
{
unsigned long index;
char ch = _BitScanForward64(&index, tmp);
if (ch)
{
whiteMaterial += ROOK_VALUE;
}
else std::cout << "Something wrong with white rooks in eval()!\n";
tmp &= tmp - 1;
}
// loop over white queens
tmp = game->pieceBoards[WQ_INDEX];
while (tmp)
{
unsigned long index;
char ch = _BitScanForward64(&index, tmp);
if (ch)
{
whiteMaterial += QUEEN_VALUE;
}
else std::cout << "Something wrong with white queens in eval()!\n";
tmp &= tmp - 1;
}
// loop over black pawns
tmp = game->pieceBoards[BP_INDEX];
while (tmp)
{
unsigned long index;
char ch = _BitScanForward64(&index, tmp);
if (ch)
{
blackMaterial += PAWN_VALUE;
}
else std::cout << "Something wrong with black pawns in eval()!\n";
tmp &= tmp - 1;
}
// loop over black knights
tmp = game->pieceBoards[BN_INDEX];
while (tmp)
{
unsigned long index;
char ch = _BitScanForward64(&index, tmp);
if (ch)
{
blackMaterial += KNIGHT_VALUE;
}
else std::cout << "Something wrong with black knights in eval()!\n";
tmp &= tmp - 1;
}
// loop over black bishop
tmp = game->pieceBoards[BB_INDEX];
while (tmp)
{
unsigned long index;
char ch = _BitScanForward64(&index, tmp);
if (ch)
{
blackMaterial += BISHOP_VALUE;
}
else std::cout << "Something wrong with black bishops in eval()!\n";
tmp &= tmp - 1;
}
// loop over black rooks
tmp = game->pieceBoards[BR_INDEX];
while (tmp)
{
unsigned long index;
char ch = _BitScanForward64(&index, tmp);
if (ch)
{
blackMaterial += ROOK_VALUE;
}
else std::cout << "Something wrong with black rooks in eval()!\n";
tmp &= tmp - 1;
}
// loop over black queens
tmp = game->pieceBoards[BQ_INDEX];
while (tmp)
{
unsigned long index;
char ch = _BitScanForward64(&index, tmp);
if (ch)
{
blackMaterial += QUEEN_VALUE;
}
else std::cout << "Something wrong with black queens in eval()!\n";
tmp &= tmp - 1;
}
if (whiteMaterial <= 1600 && blackMaterial <= 1600) return true;
else return false;
}
double FangEngine::eval()
{
int whiteKingX, whiteKingY, blackKingX, blackKingY;
bool whiteHasQueen = false, blackHasQueen = false;
int whiteMinors = 0, blackMinors = 0;
double eval = 0; // evaluation in centipawns
uint64_t tmp;
#ifdef USING_BITS
bool isEndgame = isEndGame();
// loop over white pawns
tmp = game->pieceBoards[WP_INDEX];
while (tmp)
{
unsigned long index;
char ch = _BitScanForward64(&index, tmp);
if (ch)
{
if (isEndgame) eval += PAWN_VALUE + whitePawnPlacementE[7 - (index / 8)][index % 8];
else eval += PAWN_VALUE + whitePawnPlacement[7 - (index / 8)][index % 8];
}
else std::cout << "Something wrong with white pawns in eval()!\n";
tmp &= tmp - 1;
}
// loop over white knights
tmp = game->pieceBoards[WN_INDEX];
while (tmp)
{
unsigned long index;
char ch = _BitScanForward64(&index, tmp);
if (ch)
{
eval += KNIGHT_VALUE + whiteKnightPlacement[7 - (index / 8)][index % 8];
whiteMinors += KNIGHT_VALUE;
}
else std::cout << "Something wrong with white knights in eval()!\n";
tmp &= tmp - 1;
}
// loop over white bishop
tmp = game->pieceBoards[WB_INDEX];
while (tmp)
{
unsigned long index;
char ch = _BitScanForward64(&index, tmp);
if (ch)
{
eval += BISHOP_VALUE + whiteBishopPlacement[7 - (index / 8)][index % 8];
whiteMinors += BISHOP_VALUE;
}
else std::cout << "Something wrong with white bishops in eval()!\n";
tmp &= tmp - 1;
}
// loop over white rooks
tmp = game->pieceBoards[WR_INDEX];
while (tmp)
{
unsigned long index;
char ch = _BitScanForward64(&index, tmp);
if (ch)
{
eval += ROOK_VALUE + whiteRookPlacement[7 - (index / 8)][index % 8];
}
else std::cout << "Something wrong with white rooks in eval()!\n";
tmp &= tmp - 1;
}
// loop over white queens
tmp = game->pieceBoards[WQ_INDEX];
while (tmp)
{
unsigned long index;
char ch = _BitScanForward64(&index, tmp);
if (ch)
{
eval += QUEEN_VALUE + whiteQueenPlacement[7 - (index / 8)][index % 8];
whiteHasQueen = true;
}
else std::cout << "Something wrong with white queens in eval()!\n";
tmp &= tmp - 1;
}
// get white king index
tmp = game->pieceBoards[WK_INDEX];
if (!tmp) std::cout << "CANNOT FIND WHITE KING IN eval()!\n";
int whiteKingIndex = game->bitToIndex(tmp);
whiteKingX = whiteKingIndex % 8;
whiteKingY = 7 - (whiteKingIndex / 8);
// loop over black pawns
tmp = game->pieceBoards[BP_INDEX];
while (tmp)
{
unsigned long index;
char ch = _BitScanForward64(&index, tmp);
if (ch)
{
if (isEndgame) eval += -PAWN_VALUE + blackPawnPlacementE[7 - (index / 8)][index % 8];
else eval += -PAWN_VALUE + blackPawnPlacement[7 - (index / 8)][index % 8];
}
else std::cout << "Something wrong with black pawns in eval()!\n";
tmp &= tmp - 1;
}
// loop over black knights
tmp = game->pieceBoards[BN_INDEX];
while (tmp)
{
unsigned long index;
char ch = _BitScanForward64(&index, tmp);
if (ch)
{
eval += -KNIGHT_VALUE + blackKnightPlacement[7 - (index / 8)][index % 8];
blackMinors += KNIGHT_VALUE;
}
else std::cout << "Something wrong with black knights in eval()!\n";
tmp &= tmp - 1;
}
// loop over black bishop
tmp = game->pieceBoards[BB_INDEX];
while (tmp)
{
unsigned long index;
char ch = _BitScanForward64(&index, tmp);
if (ch)
{
eval += -BISHOP_VALUE + blackBishopPlacement[7 - (index / 8)][index % 8];
blackMinors += BISHOP_VALUE;
}
else std::cout << "Something wrong with black bishops in eval()!\n";
tmp &= tmp - 1;
}
// loop over black rooks
tmp = game->pieceBoards[BR_INDEX];
while (tmp)
{
unsigned long index;
char ch = _BitScanForward64(&index, tmp);
if (ch)
{
eval += -ROOK_VALUE + blackRookPlacement[7 - (index / 8)][index % 8];
}
else std::cout << "Something wrong with black rooks in eval()!\n";
tmp &= tmp - 1;
}
// loop over black queens
tmp = game->pieceBoards[BQ_INDEX];
while (tmp)
{
unsigned long index;
char ch = _BitScanForward64(&index, tmp);
if (ch)
{
eval += -QUEEN_VALUE + blackQueenPlacement[7 - (index / 8)][index % 8];
blackHasQueen = true;
}
else std::cout << "Something wrong with black queens in eval()!\n";
tmp &= tmp - 1;
}
// get black king index
tmp = game->pieceBoards[BK_INDEX];
if (!tmp) std::cout << "CANNOT FIND BLACK KING IN eval()!\n";
int blackKingIndex = game->bitToIndex(tmp);
blackKingX = blackKingIndex % 8;
blackKingY = 7 - (blackKingIndex / 8);
#else
for (int y = 0; y < 8; y++)
{
for (int x = 0; x < 8; x++)
{
switch (game->board[y][x])
{
case WHITE_PAWN:
eval += PAWN_VALUE + whitePawnPlacement[y][x];
break;
case BLACK_PAWN:
eval += -PAWN_VALUE + blackPawnPlacement[y][x];
break;
case WHITE_KNIGHT:
eval += KNIGHT_VALUE + whiteKnightPlacement[y][x];
whiteMinors += KNIGHT_VALUE;
break;
case BLACK_KNIGHT:
eval += -KNIGHT_VALUE + blackKnightPlacement[y][x];
blackMinors += KNIGHT_VALUE;
break;
case WHITE_BISHOP:
eval += BISHOP_VALUE + whiteBishopPlacement[y][x];
whiteMinors += BISHOP_VALUE;
break;
case BLACK_BISHOP:
eval += -BISHOP_VALUE + blackBishopPlacement[y][x];
blackMinors += BISHOP_VALUE;
break;
case WHITE_ROOK:
eval += ROOK_VALUE + whiteRookPlacement[y][x];
break;
case BLACK_ROOK:
eval += -ROOK_VALUE + blackRookPlacement[y][x];
break;
case WHITE_QUEEN:
whiteHasQueen = true;
eval += QUEEN_VALUE + whiteQueenPlacement[y][x];
break;
case BLACK_QUEEN:
blackHasQueen = true;
eval += -QUEEN_VALUE + blackQueenPlacement[y][x];
break;
case WHITE_KING:
whiteKingX = x; whiteKingY = y;
break;
case BLACK_KING:
blackKingX = x; blackKingY = y;
break;
}
}
}
#endif
// check for endgame
if (isEndgame)
{
eval += KING_VALUE + whiteKingPlacementE[whiteKingY][whiteKingX];
eval += -KING_VALUE + blackKingPlacementE[blackKingY][blackKingX];
}
// kings are in middlegame
else
{
eval += KING_VALUE + whiteKingPlacementM[whiteKingY][whiteKingX];
eval += -KING_VALUE + blackKingPlacementM[blackKingY][blackKingX];
}
// bonuses for castling rights in middlegame
if (!isEndgame && game->whiteKingCanCastle) eval += 25;
if (!isEndgame && game->blackKingCanCastle) eval -= 25;
return eval / 100.0; // return eval in pawns
}