-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
1848 lines (1531 loc) · 61.7 KB
/
main.cpp
File metadata and controls
1848 lines (1531 loc) · 61.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
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
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <bitset>
#include <iostream>
#include <string>
#include <map>
#include "uci.h"
#include <vector>
#include <limits>
#include "Board.h"
#include <algorithm>
#include <chrono>
#include "zobrist.h"
#include "tt.h"
#include "move.h"
#include <intrin.h>
#include "movelist.h"
long long nodes = 0;
int gameHistory[2][64][64];
std::vector<uint64_t> repHistory;
enum Square : int
{
SQ_A1 = 0, SQ_B1, SQ_C1, SQ_D1, SQ_E1, SQ_F1, SQ_G1, SQ_H1,
SQ_A2, SQ_B2, SQ_C2, SQ_D2, SQ_E2, SQ_F2, SQ_G2, SQ_H2,
SQ_A3, SQ_B3, SQ_C3, SQ_D3, SQ_E3, SQ_F3, SQ_G3, SQ_H3,
SQ_A4, SQ_B4, SQ_C4, SQ_D4, SQ_E4, SQ_F4, SQ_G4, SQ_H4,
SQ_A5, SQ_B5, SQ_C5, SQ_D5, SQ_E5, SQ_F5, SQ_G5, SQ_H5,
SQ_A6, SQ_B6, SQ_C6, SQ_D6, SQ_E6, SQ_F6, SQ_G6, SQ_H6,
SQ_A7, SQ_B7, SQ_C7, SQ_D7, SQ_E7, SQ_F7, SQ_G7, SQ_H7,
SQ_A8, SQ_B8, SQ_C8, SQ_D8, SQ_E8, SQ_F8, SQ_G8, SQ_H8,
SQ_NONE = 64
};
std::bitset<64> generateKnightMoves(std::bitset<64> knights)
{
std::bitset<64> notAFile = ~std::bitset<64>(0x0101010101010101ULL);
std::bitset<64> notABFile = ~std::bitset<64>(0x0303030303030303ULL);
std::bitset<64> notHFile = ~std::bitset<64>(0x8080808080808080ULL);
std::bitset<64> notGHFile = ~std::bitset<64>(0xC0C0C0C0C0C0C0C0ULL);
std::bitset<64> result;
result |= (knights & notAFile) << 15;
result |= (knights & notHFile) << 17;
result |= (knights & notABFile) << 6;
result |= (knights & notGHFile) << 10;
result |= (knights & notAFile) >> 17;
result |= (knights & notHFile) >> 15;
result |= (knights & notABFile) >> 10;
result |= (knights & notGHFile) >> 6;
return result;
}
std::bitset<64> generateWhitePawnCaptures(std::bitset<64> whitePawns, std::bitset<64> blackPieces)
{
std::bitset<64> notAFile = ~std::bitset<64>(0x0101010101010101ULL);
std::bitset<64> notHFile = ~std::bitset<64>(0x8080808080808080ULL);
std::bitset<64> capturesLeft = (whitePawns & notAFile) << 7;
std::bitset<64> capturesRight = (whitePawns & notHFile) << 9;
return (capturesLeft | capturesRight) & blackPieces;
}
std::bitset<64> generateBlackPawnCaptures(std::bitset<64> blackPawns, std::bitset<64> whitePieces)
{
std::bitset<64> notAFile = ~std::bitset<64>(0x0101010101010101ULL);
std::bitset<64> notHFile = ~std::bitset<64>(0x8080808080808080ULL);
std::bitset<64> capturesLeft = (blackPawns & notHFile) >> 7;
std::bitset<64> capturesRight = (blackPawns & notAFile) >> 9;
return (capturesLeft | capturesRight) & whitePieces;
}
std::bitset<64> generateWhitePawnMoves(std::bitset<64> whitePawns, std::bitset<64> blackPieces, std::bitset<64> allPieces)
{
std::bitset<64> rank2 = (0x000000000000FF00ULL);
std::bitset<64> oneStep = (whitePawns << 8) & ~allPieces;
std::bitset<64> twoStep = ((whitePawns & rank2) << 8) & ~allPieces;
twoStep = (twoStep << 8) & ~allPieces;
std::bitset<64> captures = generateWhitePawnCaptures(whitePawns, blackPieces);
return oneStep | twoStep | captures;
}
std::bitset<64> generateBlackPawnMoves(std::bitset<64> blackPawns, std::bitset<64> whitePieces, std::bitset<64> allPieces)
{
std::bitset<64> rank7 = (0x00FF000000000000ULL);
std::bitset<64> oneStep = (blackPawns >> 8) & ~allPieces;
std::bitset<64> twoStep = ((blackPawns & rank7) >> 8) & ~allPieces;
twoStep = (twoStep >> 8) & ~allPieces;
std::bitset<64> captures = generateBlackPawnCaptures(blackPawns, whitePieces);
return oneStep | twoStep | captures;
}
std::bitset<64> constWhitePawnCaptures(std::bitset<64> whitePawns)
{
std::bitset<64> notAFile = ~std::bitset<64>(0x0101010101010101ULL);
std::bitset<64> notHFile = ~std::bitset<64>(0x8080808080808080ULL);
std::bitset<64> capturesLeft = (whitePawns & notAFile) << 7;
std::bitset<64> capturesRight = (whitePawns & notHFile) << 9;
return (capturesLeft | capturesRight);
}
std::bitset<64> constBlackPawnCaptures(std::bitset<64> blackPawns)
{
std::bitset<64> notAFile = ~std::bitset<64>(0x0101010101010101ULL);
std::bitset<64> notHFile = ~std::bitset<64>(0x8080808080808080ULL);
std::bitset<64> capturesLeft = (blackPawns & notHFile) >> 7;
std::bitset<64> capturesRight = (blackPawns & notAFile) >> 9;
return (capturesLeft | capturesRight);
}
std::bitset<64> generateRookMoves(int square, std::bitset<64> ownPieces, std::bitset<64> allPieces)
{
std::bitset<64> moves;
int directions[4] = {8, -8, 1, -1};
for (int d = 0; d < 4; ++d)
{
int sq = square;
while (true)
{
int nextSq = sq + directions[d];
if (directions[d] == 1 && nextSq % 8 == 0) break;
if (directions[d] == -1 && sq % 8 == 0) break;
if (nextSq < 0 || nextSq >= 64) break;
if (ownPieces[nextSq]) break;
moves.set(nextSq);
if (allPieces[nextSq]) break;
sq = nextSq;
}
}
return moves;
}
std::bitset<64> generateBishopMoves(int square, std::bitset<64> ownPieces, std::bitset<64> allPieces)
{
std::bitset<64> moves;
int directions[4] = {9, 7, -9, -7};
for (int d = 0; d < 4; ++d)
{
int sq = square;
while (true)
{
int nextSq = sq + directions[d];
if ((directions[d] == 9 || directions[d] == -7) && nextSq % 8 == 0) break;
if ((directions[d] == -9 || directions[d] == 7) && sq % 8 == 0) break;
if (nextSq < 0 || nextSq >= 64) break;
if (ownPieces[nextSq]) break;
moves.set(nextSq);
if (allPieces[nextSq]) break;
sq = nextSq;
}
}
return moves;
}
std::bitset<64> generateQueenMoves(int square, std::bitset<64> ownPieces, std::bitset<64> allPieces)
{
return (generateRookMoves(square, ownPieces, allPieces) | generateBishopMoves(square, ownPieces, allPieces));
}
std::bitset<64> generateKingMoves(int square, std::bitset<64> ownPieces, std::bitset<64> allPieces)
{
std::bitset<64> moves;
int directions[8] = {1, -1, 8, -8, 9, -9, 7, -7};
for (int d = 0; d < 8; ++d)
{
int nextSq = square + directions[d];
if (nextSq < 0 || nextSq >= 64) continue;
int fromFile = square % 8;
int toFile = nextSq % 8;
if (std::abs(fromFile - toFile) > 1) continue;
if (ownPieces[nextSq]) continue;
moves.set(nextSq);
}
return moves;
}
int notationToIndex(const std::string& notation)
{
if (notation.length() != 2) return -1;
char file = notation[0];
char rank = notation[1];
if (file < 'a' || file > 'h' || rank < '1' || rank > '8') return -1;
int fileIndex = file - 'a';
int rankIndex = rank - '1';
return rankIndex * 8 + fileIndex;
}
std::string indexToNotation(int index)
{
if (index < 0 || index >= 64) return "";
char file = 'a' + (index % 8);
char rank = '1' + (index / 8);
return std::string (1, file) + rank;
}
std::bitset<64> generateWhitePawnAttacks(const Board& board)
{
std::bitset<64> notAFile = ~std::bitset<64>(0x0101010101010101ULL);
std::bitset<64> notHFile = ~std::bitset<64>(0x8080808080808080ULL);
std::bitset<64> capturesLeft = (board.whitePawns & notAFile) << 7;
std::bitset<64> capturesRight = (board.whitePawns & notHFile) << 9;
return (capturesLeft | capturesRight);
}
std::bitset<64> generateBlackPawnAttacks(const Board& board)
{
std::bitset<64> notAFile = ~std::bitset<64>(0x0101010101010101ULL);
std::bitset<64> notHFile = ~std::bitset<64>(0x8080808080808080ULL);
std::bitset<64> capturesLeft = (board.blackPawns & notAFile) >> 7;
std::bitset<64> capturesRight = (board.blackPawns & notHFile) >> 9;
return (capturesLeft | capturesRight);
}
std::bitset<64> generateWhiteAttacks(const Board& board)
{
std::bitset<64> attacks;
for (int sq = 0; sq < 64; ++sq)
{
if (board.whiteKnights[sq])
{
attacks |= generateKnightMoves(std::bitset<64> (1ULL << sq));
}
}
attacks |= generateWhitePawnAttacks(board);
for (int sq = 0; sq < 64; ++sq)
{
if (board.whiteBishops[sq])
attacks |= generateBishopMoves(sq, board.getOwnPieces(true), board.getAllPieces());
else if (board.whiteRooks[sq])
attacks |= generateRookMoves(sq, board.getOwnPieces(true), board.getAllPieces());
else if (board.whiteQueen[sq])
attacks |= generateQueenMoves(sq, board.getOwnPieces(true), board.getAllPieces());
else if (board.whiteKing[sq])
attacks |= generateKingMoves(sq, board.getOwnPieces(true), board.getAllPieces());
}
return attacks;
}
std::bitset<64> generateBlackAttacks(const Board& board)
{
std::bitset<64> attacks;
for (int sq = 0; sq < 64; ++sq)
{
if (board.blackKnights[sq])
{
attacks |= generateKnightMoves(std::bitset<64> (1ULL << sq));
}
}
attacks |= generateBlackPawnAttacks(board);
for (int sq = 0; sq < 64; ++sq)
{
if (board.blackBishops[sq])
attacks |= generateBishopMoves(sq, board.getOwnPieces(false), board.getAllPieces());
else if (board.blackRooks[sq])
attacks |= generateRookMoves(sq, board.getOwnPieces(false), board.getAllPieces());
else if (board.blackQueen[sq])
attacks |= generateQueenMoves(sq, board.getOwnPieces(false), board.getAllPieces());
else if (board.blackKing[sq])
attacks |= generateKingMoves(sq, board.getOwnPieces(false), board.getAllPieces());
}
return attacks;
}
void getPinnedPieces(const Board& board, std::bitset<64>* pinnedLines)
{
for (int i = 0; i < 64; ++i)
{
pinnedLines[i].reset();
}
bool white = board.whiteToMove;
int kingSquare = (white ? board.whiteKing : board.blackKing)._Find_first();
if (kingSquare >= 64) {
std::cerr << "Error: King not found on board!" << std::endl;
return;
}
std::bitset<64> enemyBishops = white ? board.blackBishops : board.whiteBishops;
std::bitset<64> enemyRooks = white ? board.blackRooks : board.whiteRooks;
std::bitset<64> enemyQueens = white ? board.blackQueen : board.whiteQueen;
std::bitset<64> ownPieces = board.getOwnPieces(white);
std::bitset<64> allPieces = board.getAllPieces();
int directions[8] = {1, -1, 8, -8, 7, -7, 9, -9};
for (int d = 0; d < 8; ++d)
{
int dir = directions[d];
int sq = kingSquare;
int potentialPinned = -1;
std::bitset<64> potentialLine;
while (true)
{
sq += dir;
if (sq < 0 || sq >= 64) break;
int prevFile = (sq - dir) % 8;
int currFile = sq % 8;
if (abs(currFile - prevFile) > 1) break;
potentialLine.set(sq);
if (ownPieces[sq])
{
if (potentialPinned == -1)
{
potentialPinned = sq;
}
else break;
}
else if (allPieces[sq])
{
if (enemyQueens[sq] ||
(enemyBishops[sq] && (d >= 4)) ||
(enemyRooks[sq] && (d < 4)))
{
if (potentialPinned != -1) {
pinnedLines[potentialPinned] = potentialLine;
}
}
break;
}
}
}
}
bool isKingInCheck(const Board& board)
{
int kingIndex = board.whiteToMove ? board.whiteKing._Find_first() : board.blackKing._Find_first();
std::bitset<64> attackMap = board.whiteToMove ?
generateBlackAttacks(board)
:
generateWhiteAttacks(board);
return attackMap[kingIndex];
}
void removePieceAt(Board& b, int sq)
{
if (!b.getAllPieces()[sq]) return;
b.whitePawns.reset(sq);
b.whiteKnights.reset(sq);
b.whiteBishops.reset(sq);
b.whiteRooks.reset(sq);
b.whiteQueen.reset(sq);
b.whiteKing.reset(sq);
b.blackPawns.reset(sq);
b.blackKnights.reset(sq);
b.blackBishops.reset(sq);
b.blackRooks.reset(sq);
b.blackQueen.reset(sq);
b.blackKing.reset(sq);
}
int getPieceIndex(const Board& b, int sq)
{
if (b.whitePawns[sq]) return 0;
if (b.whiteKnights[sq]) return 1;
if (b.whiteBishops[sq]) return 2;
if (b.whiteRooks[sq]) return 3;
if (b.whiteQueen[sq]) return 4;
if (b.whiteKing[sq]) return 5;
if (b.blackPawns[sq]) return 6;
if (b.blackKnights[sq]) return 7;
if (b.blackBishops[sq]) return 8;
if (b.blackRooks[sq]) return 9;
if (b.blackQueen[sq]) return 10;
if (b.blackKing[sq]) return 11;
return -1;
}
bool makeMove(Move move, Board& board)
{
int from = MoveEncoder::getFrom(move);
int to = MoveEncoder::getTo(move);
int flag = MoveEncoder::getFlag(move);
if (from == -1 || to == -1) return false;
// ------------------------Clear hash-----------------------------
board.hash ^= Zobrist::sideKey;
if (board.en_passant != -1) {
board.hash ^= Zobrist::en_passantKeys[board.en_passant % 8];
}
int oldCastlingRights = 0;
if (board.whiteCanCastleKingside) oldCastlingRights |= 1 << 0;
if (board.whiteCanCastleQueenside) oldCastlingRights |= 1 << 1;
if (board.blackCanCastleKingside) oldCastlingRights |= 1 << 2;
if (board.blackCanCastleQueenside) oldCastlingRights |= 1 << 3;
board.hash ^= Zobrist::castlingKeys[oldCastlingRights];
// ----------------------------------------------------------------
if (board.getAllPieces()[to]) {
int capturedType = getPieceIndex(board, to);
if (capturedType != -1) {
board.hash ^= Zobrist::pieceKeys[capturedType][to];
}
removePieceAt(board, to);
}
bool isPromotion = (flag >= MoveEncoder::PROMO_KNIGHT) && (flag <= MoveEncoder::PROMO_QUEEN);
// Remove taken piece
if (board.getAllPieces()[to]) {
removePieceAt(board, to);
}
// Move pawn
if (board.whiteToMove) {
if (board.whitePawns[from]) {
board.whitePawns.reset(from);
board.hash ^= Zobrist::pieceKeys[0][from];
if (to == board.en_passant && (to / 8 == 5)) {
board.blackPawns.reset(to - 8);
board.hash ^= Zobrist::pieceKeys[6][to - 8];
}
if (isPromotion) {
switch (flag) {
case MoveEncoder::PROMO_QUEEN: board.whiteQueen.set(to); board.hash ^= Zobrist::pieceKeys[4][to]; break;
case MoveEncoder::PROMO_ROOK: board.whiteRooks.set(to); board.hash ^= Zobrist::pieceKeys[3][to]; break;
case MoveEncoder::PROMO_BISHOP: board.whiteBishops.set(to); board.hash ^= Zobrist::pieceKeys[2][to]; break;
case MoveEncoder::PROMO_KNIGHT: board.whiteKnights.set(to); board.hash ^= Zobrist::pieceKeys[1][to]; break;
}
} else {
board.whitePawns.set(to);
board.hash ^= Zobrist::pieceKeys[0][to];
}
if (from / 8 == 1 && to / 8 == 3) board.en_passant = from + 8;
else board.en_passant = -1;
}
// Move knight
else if (board.whiteKnights[from]) {
board.whiteKnights.reset(from); board.whiteKnights.set(to);
board.hash ^= Zobrist::pieceKeys[1][from]; board.hash ^= Zobrist::pieceKeys[1][to];
board.en_passant = -1;
}
// Move bishop
else if (board.whiteBishops[from]) {
board.whiteBishops.reset(from); board.whiteBishops.set(to);
board.hash ^= Zobrist::pieceKeys[2][from]; board.hash ^= Zobrist::pieceKeys[2][to];
board.en_passant = -1;
}
// Move rook
else if (board.whiteRooks[from]) {
board.whiteRooks.reset(from); board.whiteRooks.set(to);
board.hash ^= Zobrist::pieceKeys[3][from]; board.hash ^= Zobrist::pieceKeys[3][to];
board.en_passant = -1;
if (from == SQ_A1) board.whiteCanCastleQueenside = false;
else if (from == SQ_H1) board.whiteCanCastleKingside = false;
}
// Move queen
else if (board.whiteQueen[from]) {
board.whiteQueen.reset(from); board.whiteQueen.set(to);
board.hash ^= Zobrist::pieceKeys[4][from]; board.hash ^= Zobrist::pieceKeys[4][to];
board.en_passant = -1;
}
// Move king
else if (board.whiteKing[from]) {
board.whiteKing.reset(from); board.whiteKing.set(to);
board.hash ^= Zobrist::pieceKeys[5][from]; board.hash ^= Zobrist::pieceKeys[5][to];
board.en_passant = -1;
if (from == SQ_E1) {
if (to == SQ_G1) {
board.whiteRooks.reset(SQ_H1); board.whiteRooks.set(SQ_F1);
board.hash ^= Zobrist::pieceKeys[3][SQ_H1]; board.hash ^= Zobrist::pieceKeys[3][SQ_F1];
board.whiteCastled = true;
} else if (to == SQ_C1) {
board.whiteRooks.reset(SQ_A1); board.whiteRooks.set(SQ_D1);
board.hash ^= Zobrist::pieceKeys[3][SQ_A1]; board.hash ^= Zobrist::pieceKeys[3][SQ_D1];
board.whiteCastled = true;
}
}
board.whiteCanCastleKingside = false;
board.whiteCanCastleQueenside = false;
}
}
else {
// Move pawn
if (board.blackPawns[from]) {
board.blackPawns.reset(from);
board.hash ^= Zobrist::pieceKeys[6][from];
if (to == board.en_passant && (to / 8 == 2)) {
board.whitePawns.reset(to + 8);
board.hash ^= Zobrist::pieceKeys[0][to + 8];
}
if (isPromotion) {
switch (flag) {
case MoveEncoder::PROMO_QUEEN: board.blackQueen.set(to); board.hash ^= Zobrist::pieceKeys[10][to]; break;
case MoveEncoder::PROMO_ROOK: board.blackRooks.set(to); board.hash ^= Zobrist::pieceKeys[9][to]; break;
case MoveEncoder::PROMO_BISHOP: board.blackBishops.set(to); board.hash ^= Zobrist::pieceKeys[8][to]; break;
case MoveEncoder::PROMO_KNIGHT: board.blackKnights.set(to); board.hash ^= Zobrist::pieceKeys[7][to]; break;
}
} else {
board.blackPawns.set(to);
board.hash ^= Zobrist::pieceKeys[6][to];
}
if (from / 8 == 6 && to / 8 == 4) board.en_passant = from - 8;
else board.en_passant = -1;
}
// Move knight
else if (board.blackKnights[from]) {
board.blackKnights.reset(from); board.blackKnights.set(to);
board.hash ^= Zobrist::pieceKeys[7][from]; board.hash ^= Zobrist::pieceKeys[7][to];
board.en_passant = -1;
}
// Move bishop
else if (board.blackBishops[from]) {
board.blackBishops.reset(from); board.blackBishops.set(to);
board.hash ^= Zobrist::pieceKeys[8][from]; board.hash ^= Zobrist::pieceKeys[8][to];
board.en_passant = -1;
}
// Move rook
else if (board.blackRooks[from]) {
board.blackRooks.reset(from); board.blackRooks.set(to);
board.hash ^= Zobrist::pieceKeys[9][from]; board.hash ^= Zobrist::pieceKeys[9][to];
board.en_passant = -1;
if (from == SQ_A8) board.blackCanCastleQueenside = false;
else if (from == SQ_H8) board.blackCanCastleKingside = false;
}
// Move queen
else if (board.blackQueen[from]) {
board.blackQueen.reset(from); board.blackQueen.set(to);
board.hash ^= Zobrist::pieceKeys[10][from]; board.hash ^= Zobrist::pieceKeys[10][to];
board.en_passant = -1;
}
// Move king
else if (board.blackKing[from]) {
board.blackKing.reset(from); board.blackKing.set(to);
board.hash ^= Zobrist::pieceKeys[11][from]; board.hash ^= Zobrist::pieceKeys[11][to];
board.en_passant = -1;
if (from == SQ_E8) {
if (to == SQ_G8) {
board.blackRooks.reset(SQ_H8); board.blackRooks.set(SQ_F8);
board.hash ^= Zobrist::pieceKeys[9][SQ_H8]; board.hash ^= Zobrist::pieceKeys[9][SQ_F8];
board.blackCastled = true;
} else if (to == SQ_C8) {
board.blackRooks.reset(SQ_A8); board.blackRooks.set(SQ_D8);
board.hash ^= Zobrist::pieceKeys[9][SQ_A8]; board.hash ^= Zobrist::pieceKeys[9][SQ_D8];
board.blackCastled = true;
}
}
board.blackCanCastleKingside = false;
board.blackCanCastleQueenside = false;
}
}
// Update castling hash
int newCastlingRights = 0;
if (board.whiteCanCastleKingside) newCastlingRights |= 1 << 0;
if (board.whiteCanCastleQueenside) newCastlingRights |= 1 << 1;
if (board.blackCanCastleKingside) newCastlingRights |= 1 << 2;
if (board.blackCanCastleQueenside) newCastlingRights |= 1 << 3;
board.hash ^= Zobrist::castlingKeys[newCastlingRights];
// Update en passant hash
if (board.en_passant != -1) {
board.hash ^= Zobrist::en_passantKeys[board.en_passant % 8];
}
board.whiteToMove = !board.whiteToMove;
return true;
}
int mirrorVertical(int sq)
{
return sq ^ 56;
}
int getPieceValue(char piece)
{
switch (piece)
{
case 'P': case 'p': return 100;
case 'N': case 'n': return 320;
case 'B': case 'b': return 330;
case 'R': case 'r': return 500;
case 'Q': case 'q': return 900;
case 'K': case 'k': return 20000;
default: return 0;
}
}
bool isCheck(Board board, const Move move)
{
makeMove(move, board);
return isKingInCheck(board);
}
constexpr int MAX_DEPTH = 64;
Move killerMoves[MAX_DEPTH][2];
int scoreMove(const Move& move, const Board& board, int depth, Move ttBestMove)
{
int from = MoveEncoder::getFrom(move);
int to = MoveEncoder::getTo(move);
char attacker = board.getPieceAt(from);
char victim = board.getPieceAt(to);
int score = 0;
if (move == ttBestMove) return 30000;
if (victim != '.')
{
score += 10000 + getPieceValue(victim) - getPieceValue(attacker);
}
if (MoveEncoder::getFlag(move) == MoveEncoder::PROMO_QUEEN)
{
score += 8000;
}
if (victim == '.')
{
if (killerMoves[depth][0] == move) return 9000;
else if (killerMoves[depth][1] == move) return 8000;
return gameHistory[board.whiteToMove][from][to];
}
return score;
}
int scoreCapture(const Move& move, const Board& board)
{
int from = MoveEncoder::getFrom(move);
int to = MoveEncoder::getTo(move);
int flag = MoveEncoder::getFlag(move);
char attacker = board.getPieceAt(from);
char victim = board.getPieceAt(to);
if (to == board.en_passant)
{
return 10000;
}
if (victim != '.')
{
return 10000 + getPieceValue(victim) - getPieceValue(attacker);
}
if (flag == MoveEncoder::PROMO_QUEEN) return 9000;
if (flag == MoveEncoder::PROMO_ROOK) return 5000;
if (flag == MoveEncoder::PROMO_BISHOP) return 3000;
if (flag == MoveEncoder::PROMO_KNIGHT) return 3000;
return 0;
}
std::bitset<64> getRankInFront(const Board & board, int sq)
{
int rank = sq / 8;
std::bitset<64> rankInFront;
if (rank < 4) {
if (rank == 0) return std::bitset<64>(0x000000000000FF00ULL);
if (rank == 1) return std::bitset<64>(0x0000000000FF0000ULL);
return std::bitset<64>(0);
}
else {
if (rank == 7) return std::bitset<64>(0x00FF000000000000ULL);
if (rank == 6) return std::bitset<64>(0x0000FF0000000000ULL);
return std::bitset<64>(0);
}
return rankInFront;
}
int MAX_PHASE = 24;
int gamePhase(const Board& board)
{
int phase = 0;
phase += board.whiteQueen.count() * 4;
phase += board.whiteRooks.count() * 2;
phase += board.whiteBishops.count() * 1;
phase += board.whiteKnights.count() * 1;
phase += board.blackQueen.count() * 4;
phase += board.blackRooks.count() * 2;
phase += board.blackBishops.count() * 1;
phase += board.blackKnights.count() * 1;
return phase;
}
std::bitset<64> getSecondRing(int kingSq, const Board& board, bool isWhiteKing)
{
if (kingSq >= 64) return std::bitset<64>(0);
std::bitset<64> firstRing = generateKingMoves(kingSq, board.getOwnPieces(isWhiteKing), board.getAllPieces());
std::bitset<64> secondRing;
for (int sq = 0; sq < 64; ++sq)
{
if (firstRing[sq])
{
secondRing |= generateKingMoves(sq, board.getOwnPieces(isWhiteKing), board.getAllPieces());
}
}
secondRing.reset(kingSq);
secondRing &= ~firstRing;
return secondRing;
}
bool isPassedPawn(int sq, bool white, const Board& board)
{
std::bitset<64> enemyPawns = white ? board.blackPawns : board.whitePawns;
std::bitset<64> mask = 0ULL;
int file = sq % 8;
int rank = sq / 8;
for (int df = -1; df <= 1; ++df)
{
int f = file + df;
if (f < 0 || f > 7) continue;
if (white)
{
for (int r = rank + 1; r < 8; ++r)
{
int targetSq = r * 8 + f;
mask |= (1ULL << targetSq);
}
}
else
{
for (int r = rank - 1; r > 0; --r)
{
int targetSq = r * 8 + f;
mask |= (1ULL << targetSq);
}
}
}
return (enemyPawns & mask) == 0;
}
int pawnProtections(int sq, bool white, const Board& board)
{
std::bitset<64> protSq = white ? constBlackPawnCaptures(sq) : constWhitePawnCaptures(sq);
std::bitset<64> ownPawns = white ? board.whitePawns : board.blackPawns;
return (protSq & ownPawns).count();
}
std::bitset<64> fileMask(int file)
{
std::bitset<64> mask;
for (int rank = 1; rank < 7; ++rank)
{
mask.set(rank * 8 + file);
}
return mask;
}
bool isIsolatedPawn(int sq, bool white, const Board& board)
{
int file = sq % 8;
std::bitset<64> ownPawns = white ? board.whitePawns : board.blackPawns;
bool leftHasPawn = false;
bool rightHasPawn = false;
if (file > 0)
{
leftHasPawn = (ownPawns & fileMask(file - 1)).any();
}
if (file < 7)
{
rightHasPawn = (ownPawns & fileMask(file + 1)).any();
}
return !(leftHasPawn || rightHasPawn);
}
int doubledPawnPenalty(bool white, const Board& board)
{
std::bitset<64> pawns = white ? board.whitePawns : board.blackPawns;
int penalty = 0;
for (int file = 0; file < 8; ++file)
{
int count = 0;
for (int rank = 1; rank < 7; ++rank)
{
int sq = rank * 8 + file;
if (pawns[sq]) count++;
}
if (count > 1)
{
penalty += (count - 1) * 5;
}
}
return penalty;
}
int distance(int sq1, int sq2)
{
int file1 = sq1 % 8;
int rank1 = sq1 / 8;
int file2 = sq2 % 8;
int rank2 = sq2 / 8;
return std::max(std::abs(file1 - file2), std::abs(rank1 - rank2));
}
int centerManhattanDistance(int sq)
{
int file = sq % 8;
int rank = sq / 8;
int distFile = std::abs(2*file-7);
int distRank = std::abs(2*rank-7);
return distFile + distRank;
}
int evaluatePosition(const Board& board)
{
int whiteKingSq = board.whiteKing._Find_first();
int blackKingSq = board.blackKing._Find_first();
if (whiteKingSq >= 64) return -100000;
if (blackKingSq >= 64) return 100000;
int score = 0;
int phase = gamePhase(board);
double blend = static_cast<double>(phase) / MAX_PHASE; // opening = 1 endgame = 0
score += board.whitePawns.count() * 100;
score -= board.blackPawns.count() * 100;
score += board.whiteKnights.count() * 320;
score -= board.blackKnights.count() * 320;
score += board.whiteBishops.count() * 330;
score -= board.blackBishops.count() * 330;
score += board.whiteRooks.count() * 500;
score -= board.blackRooks.count() * 500;
score += board.whiteQueen.count() * 900;
score -= board.blackQueen.count() * 900;
const int PawnOpeningTable[64] = {
0, 0, 0, 0, 0, 0, 0, 0,
5, 10, 10, -20, -20, 10, 10, 5,
5, -5, -10, 0, 0, -10, -5, 5,
0, 0, 0, 20, 20, 0, 0, 0,
5, 5, 10, 25, 25, 10, 5, 5,
10, 10, 20, 30, 30, 20, 10, 10,
50, 50, 50, 50, 50, 50, 50, 50,
0, 0, 0, 0, 0, 0, 0, 0
};
const int KnightOpeningTable[64] = {
-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
};
const int BishopOpeningTable[64] = {
-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
};
const int RookOpeningTable[64] = {
0, 0, 0, 5, 5, 0, 0, 0,
-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,
5, 10, 10, 10, 10, 10, 10, 5,
0, 0, 0, 0, 0, 0, 0, 0
};
const int QueenOpeningTable[64] = {
-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
};
const int KingOpeningTable[64] = {
-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, 32, 12, 0, 0, 10, 35, 20
};
const int PawnEndgameTable[64] = {
0, 0, 0, 0, 0, 0, 0, 0,
178, 173, 158, 134, 147, 132, 165, 187,
94, 100, 85, 67, 56, 53, 82, 84,
32, 24, 13, 5, -2, 4, 17, 17,
13, 9, -3, -7, -7, -8, 3, -1,
4, 7, -6, 1, 0, -5, -1, -8,
13, 8, 8, 10, 13, 0, 2, -7,
0, 0, 0, 0, 0, 0, 0, 0
};
const int KnightEndgameTable[64] = {
-58, -38, -13, -28, -31, -27, -63, -99,
-25, -8, -25, -2, -9, -25, -24, -52,
-24, -20, 10, 9, -1, -9, -19, -41,
-17, 3, 22, 22, 22, 11, 8, -18,
-18, -6, 16, 25, 16, 17, 4, -18,
-23, -3, 15, 24, 15, 16, 0, -23,