-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathboard.cpp
1529 lines (1301 loc) · 53 KB
/
board.cpp
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
/*
Laser, a UCI chess engine written in C++11.
Copyright 2015-2018 Jeffrey An and Michael An
Laser is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Laser is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Laser. If not, see <http://www.gnu.org/licenses/>.
*/
#include <algorithm>
#include <cstring>
#include <random>
#include <string>
#include "board.h"
#include "bbinit.h"
#include "eval.h"
#include "uci.h"
constexpr uint64_t WHITE_KSIDE_PASSTHROUGH_SQS = indexToBit(5) | indexToBit(6);
constexpr uint64_t WHITE_QSIDE_PASSTHROUGH_SQS = indexToBit(1) | indexToBit(2) | indexToBit(3);
constexpr uint64_t BLACK_KSIDE_PASSTHROUGH_SQS = indexToBit(61) | indexToBit(62);
constexpr uint64_t BLACK_QSIDE_PASSTHROUGH_SQS = indexToBit(57) | indexToBit(58) | indexToBit(59);
// Zobrist hashing table and the start position key, both initialized at startup
uint64_t zobristTable[794];
static uint64_t startPosZobristKey = 0;
void initZobristTable() {
std::mt19937_64 rng (61280152908);
for (int i = 0; i < 794; i++)
zobristTable[i] = rng();
Board b;
int *mailbox = b.getMailbox();
b.initZobristKey(mailbox);
startPosZobristKey = b.getZobristKey();
delete[] mailbox;
}
// Magic tables, initialized in bbinit.cpp
extern uint64_t *attackTable;
extern MagicInfo magicBishops[64];
extern MagicInfo magicRooks[64];
// Precalculated bitboard tables
extern uint64_t inBetweenSqs[64][64];
//------------------------------------------------------------------------------
//--------------------------------Constructors----------------------------------
//------------------------------------------------------------------------------
// Create a board object initialized to the start position.
Board::Board() {
allPieces[WHITE] = 0x000000000000FFFF;
allPieces[BLACK] = 0xFFFF000000000000;
pieces[WHITE][PAWNS] = 0x000000000000FF00; // white pawns
pieces[WHITE][KNIGHTS] = 0x0000000000000042; // white knights
pieces[WHITE][BISHOPS] = 0x0000000000000024; // white bishops
pieces[WHITE][ROOKS] = 0x0000000000000081; // white rooks
pieces[WHITE][QUEENS] = 0x0000000000000008; // white queens
pieces[WHITE][KINGS] = 0x0000000000000010; // white kings
pieces[BLACK][PAWNS] = 0x00FF000000000000; // black pawns
pieces[BLACK][KNIGHTS] = 0x4200000000000000; // black knights
pieces[BLACK][BISHOPS] = 0x2400000000000000; // black bishops
pieces[BLACK][ROOKS] = 0x8100000000000000; // black rooks
pieces[BLACK][QUEENS] = 0x0800000000000000; // black queens
pieces[BLACK][KINGS] = 0x1000000000000000; // black kings
zobristKey = startPosZobristKey;
epCaptureFile = NO_EP_POSSIBLE;
playerToMove = WHITE;
moveNumber = 1;
castlingRights = WHITECASTLE | BLACKCASTLE;
fiftyMoveCounter = 0;
kingSqs[WHITE] = 4;
kingSqs[BLACK] = 60;
}
// Create a board object from a mailbox of the current board state.
Board::Board(int *mailboxBoard, bool _whiteCanKCastle, bool _blackCanKCastle,
bool _whiteCanQCastle, bool _blackCanQCastle, uint16_t _epCaptureFile,
int _fiftyMoveCounter, int _moveNumber, int _playerToMove) {
// Initialize bitboards
for (int i = 0; i < 12; i++) {
pieces[i/6][i%6] = 0;
}
for (int i = 0; i < 64; i++) {
if (0 <= mailboxBoard[i] && mailboxBoard[i] <= 11) {
pieces[mailboxBoard[i]/6][mailboxBoard[i]%6] |= indexToBit(i);
}
}
allPieces[WHITE] = 0;
for (int i = 0; i < 6; i++)
allPieces[WHITE] |= pieces[WHITE][i];
allPieces[BLACK] = 0;
for (int i = 0; i < 6; i++)
allPieces[BLACK] |= pieces[BLACK][i];
epCaptureFile = _epCaptureFile;
playerToMove = _playerToMove;
moveNumber = _moveNumber;
castlingRights = 0;
if (_whiteCanKCastle)
castlingRights |= WHITEKSIDE;
if (_whiteCanQCastle)
castlingRights |= WHITEQSIDE;
if (_blackCanKCastle)
castlingRights |= BLACKKSIDE;
if (_blackCanQCastle)
castlingRights |= BLACKQSIDE;
fiftyMoveCounter = _fiftyMoveCounter;
initZobristKey(mailboxBoard);
kingSqs[WHITE] = bitScanForward(pieces[WHITE][KINGS]);
kingSqs[BLACK] = bitScanForward(pieces[BLACK][KINGS]);
}
Board::~Board() {}
Board Board::staticCopy() const {
Board b;
std::memcpy(static_cast<void*>(&b), this, sizeof(Board));
return b;
}
//------------------------------------------------------------------------------
//---------------------------------Do Move--------------------------------------
//------------------------------------------------------------------------------
/**
* @brief Updates the board and Zobrist keys with Move m.
*/
void Board::doMove(Move m, int color) {
int startSq = getStartSq(m);
int endSq = getEndSq(m);
int pieceID = getPieceOnSquare(color, startSq);
// Update flag based elements of Zobrist key
zobristKey ^= zobristTable[769 + castlingRights];
zobristKey ^= zobristTable[785 + epCaptureFile];
if (isPromotion(m)) {
int promotionType = getPromotion(m);
if (isCapture(m)) {
int captureType = getPieceOnSquare(color^1, endSq);
pieces[color][PAWNS] &= ~indexToBit(startSq);
pieces[color][promotionType] |= indexToBit(endSq);
pieces[color^1][captureType] &= ~indexToBit(endSq);
allPieces[color] &= ~indexToBit(startSq);
allPieces[color] |= indexToBit(endSq);
allPieces[color^1] &= ~indexToBit(endSq);
zobristKey ^= zobristTable[384*color + startSq];
zobristKey ^= zobristTable[384*color + 64*promotionType + endSq];
zobristKey ^= zobristTable[384*(color^1) + 64*captureType + endSq];
}
else {
pieces[color][PAWNS] &= ~indexToBit(startSq);
pieces[color][promotionType] |= indexToBit(endSq);
allPieces[color] &= ~indexToBit(startSq);
allPieces[color] |= indexToBit(endSq);
zobristKey ^= zobristTable[384*color + startSq];
zobristKey ^= zobristTable[384*color + 64*promotionType + endSq];
}
epCaptureFile = NO_EP_POSSIBLE;
fiftyMoveCounter = 0;
} // end promotion
else if (isCapture(m)) {
if (isEP(m)) {
pieces[color][PAWNS] &= ~indexToBit(startSq);
pieces[color][PAWNS] |= indexToBit(endSq);
uint64_t epCaptureSq = indexToBit(epVictimSquare(color^1, epCaptureFile));
pieces[color^1][PAWNS] &= ~epCaptureSq;
allPieces[color] &= ~indexToBit(startSq);
allPieces[color] |= indexToBit(endSq);
allPieces[color^1] &= ~epCaptureSq;
int capSq = epVictimSquare(color^1, epCaptureFile);
zobristKey ^= zobristTable[384*color + startSq];
zobristKey ^= zobristTable[384*color + endSq];
zobristKey ^= zobristTable[384*(color^1) + capSq];
}
else {
int captureType = getPieceOnSquare(color^1, endSq);
pieces[color][pieceID] &= ~indexToBit(startSq);
pieces[color][pieceID] |= indexToBit(endSq);
pieces[color^1][captureType] &= ~indexToBit(endSq);
allPieces[color] &= ~indexToBit(startSq);
allPieces[color] |= indexToBit(endSq);
allPieces[color^1] &= ~indexToBit(endSq);
zobristKey ^= zobristTable[384*color + 64*pieceID + startSq];
zobristKey ^= zobristTable[384*color + 64*pieceID + endSq];
zobristKey ^= zobristTable[384*(color^1) + 64*captureType + endSq];
}
epCaptureFile = NO_EP_POSSIBLE;
fiftyMoveCounter = 0;
} // end capture
else { // Quiet moves
if (isCastle(m)) {
if (endSq == 6) { // white kside
pieces[WHITE][KINGS] &= ~indexToBit(4);
pieces[WHITE][KINGS] |= indexToBit(6);
pieces[WHITE][ROOKS] &= ~indexToBit(7);
pieces[WHITE][ROOKS] |= indexToBit(5);
allPieces[WHITE] &= ~indexToBit(4);
allPieces[WHITE] |= indexToBit(6);
allPieces[WHITE] &= ~indexToBit(7);
allPieces[WHITE] |= indexToBit(5);
zobristKey ^= zobristTable[64*KINGS+4];
zobristKey ^= zobristTable[64*KINGS+6];
zobristKey ^= zobristTable[64*ROOKS+7];
zobristKey ^= zobristTable[64*ROOKS+5];
}
else if (endSq == 2) { // white qside
pieces[WHITE][KINGS] &= ~indexToBit(4);
pieces[WHITE][KINGS] |= indexToBit(2);
pieces[WHITE][ROOKS] &= ~indexToBit(0);
pieces[WHITE][ROOKS] |= indexToBit(3);
allPieces[WHITE] &= ~indexToBit(4);
allPieces[WHITE] |= indexToBit(2);
allPieces[WHITE] &= ~indexToBit(0);
allPieces[WHITE] |= indexToBit(3);
zobristKey ^= zobristTable[64*KINGS+4];
zobristKey ^= zobristTable[64*KINGS+2];
zobristKey ^= zobristTable[64*ROOKS+0];
zobristKey ^= zobristTable[64*ROOKS+3];
}
else if (endSq == 62) { // black kside
pieces[BLACK][KINGS] &= ~indexToBit(60);
pieces[BLACK][KINGS] |= indexToBit(62);
pieces[BLACK][ROOKS] &= ~indexToBit(63);
pieces[BLACK][ROOKS] |= indexToBit(61);
allPieces[BLACK] &= ~indexToBit(60);
allPieces[BLACK] |= indexToBit(62);
allPieces[BLACK] &= ~indexToBit(63);
allPieces[BLACK] |= indexToBit(61);
zobristKey ^= zobristTable[384+64*KINGS+60];
zobristKey ^= zobristTable[384+64*KINGS+62];
zobristKey ^= zobristTable[384+64*ROOKS+63];
zobristKey ^= zobristTable[384+64*ROOKS+61];
}
else { // black qside
pieces[BLACK][KINGS] &= ~indexToBit(60);
pieces[BLACK][KINGS] |= indexToBit(58);
pieces[BLACK][ROOKS] &= ~indexToBit(56);
pieces[BLACK][ROOKS] |= indexToBit(59);
allPieces[BLACK] &= ~indexToBit(60);
allPieces[BLACK] |= indexToBit(58);
allPieces[BLACK] &= ~indexToBit(56);
allPieces[BLACK] |= indexToBit(59);
zobristKey ^= zobristTable[384+64*KINGS+60];
zobristKey ^= zobristTable[384+64*KINGS+58];
zobristKey ^= zobristTable[384+64*ROOKS+56];
zobristKey ^= zobristTable[384+64*ROOKS+59];
}
epCaptureFile = NO_EP_POSSIBLE;
fiftyMoveCounter++;
} // end castling
else { // other quiet moves
pieces[color][pieceID] &= ~indexToBit(startSq);
pieces[color][pieceID] |= indexToBit(endSq);
allPieces[color] &= ~indexToBit(startSq);
allPieces[color] |= indexToBit(endSq);
zobristKey ^= zobristTable[384*color + 64*pieceID + startSq];
zobristKey ^= zobristTable[384*color + 64*pieceID + endSq];
// check for en passant
if (pieceID == PAWNS) {
if (getFlags(m) == MOVE_DOUBLE_PAWN)
epCaptureFile = startSq & 7;
else
epCaptureFile = NO_EP_POSSIBLE;
fiftyMoveCounter = 0;
}
else {
epCaptureFile = NO_EP_POSSIBLE;
fiftyMoveCounter++;
}
} // end other quiet moves
} // end normal move
// change castling flags
if (pieceID == KINGS) {
if (color == WHITE)
castlingRights &= ~WHITECASTLE;
else
castlingRights &= ~BLACKCASTLE;
kingSqs[color] = endSq;
}
// Castling rights change because of the rook only when the rook moves or
// is captured
else if (isCapture(m) || pieceID == ROOKS) {
// No sense in removing the rights if they're already gone
if (castlingRights & WHITECASTLE) {
if ((pieces[WHITE][ROOKS] & 0x80) == 0)
castlingRights &= ~WHITEKSIDE;
if ((pieces[WHITE][ROOKS] & 1) == 0)
castlingRights &= ~WHITEQSIDE;
}
if (castlingRights & BLACKCASTLE) {
uint64_t blackR = pieces[BLACK][ROOKS] >> 56;
if ((blackR & 0x80) == 0)
castlingRights &= ~BLACKKSIDE;
if ((blackR & 0x1) == 0)
castlingRights &= ~BLACKQSIDE;
}
} // end castling flags
zobristKey ^= zobristTable[769 + castlingRights];
zobristKey ^= zobristTable[785 + epCaptureFile];
if (color == BLACK)
moveNumber++;
playerToMove = color^1;
zobristKey ^= zobristTable[768];
}
bool Board::doPseudoLegalMove(Move m, int color) {
doMove(m, color);
// Pseudo-legal moves require a check for legality
return !(isInCheck(color));
}
// Do a hash move, which requires a few more checks in case of a Type-1 error.
bool Board::doHashMove(Move m, int color) {
int pieceID = getPieceOnSquare(color, getStartSq(m));
// Check that the start square is not empty
if (pieceID == -1)
return false;
// Check that the end square has correct occupancy
uint64_t otherPieces = allPieces[color^1];
uint64_t endSingle = indexToBit(getEndSq(m));
bool captureRoutes = (isCapture(m) && (otherPieces & endSingle))
|| (isCapture(m) && pieceID == PAWNS && (~otherPieces & endSingle));
uint64_t empty = ~getOccupancy();
if (!(captureRoutes || (!isCapture(m) && (empty & endSingle))))
return false;
// Check that the king is not captured
if (isCapture(m) && ((endSingle & pieces[WHITE][KINGS]) || (endSingle & pieces[BLACK][KINGS])))
return false;
return doPseudoLegalMove(m, color);
}
// Handle null moves for null move pruning by switching the player to move.
void Board::doNullMove() {
playerToMove = playerToMove ^ 1;
zobristKey ^= zobristTable[768];
zobristKey ^= zobristTable[785 + epCaptureFile];
epCaptureFile = NO_EP_POSSIBLE;
zobristKey ^= zobristTable[785 + epCaptureFile];
}
void Board::undoNullMove(uint16_t _epCaptureFile) {
playerToMove = playerToMove ^ 1;
zobristKey ^= zobristTable[768];
zobristKey ^= zobristTable[785 + epCaptureFile];
epCaptureFile = _epCaptureFile;
zobristKey ^= zobristTable[785 + epCaptureFile];
}
//------------------------------------------------------------------------------
//-------------------------------Move Generation--------------------------------
//------------------------------------------------------------------------------
/*
* @brief Returns a list of structs containing the piece type, start square,
* and a bitboard of potential legal moves for each knight, bishop, rook, and queen.
* Used for mobility evaluation of pieces.
*/
PieceMoveList Board::getPieceMoveList(int color) const {
PieceMoveList pml;
uint64_t knights = pieces[color][KNIGHTS];
while (knights) {
int stSq = bitScanForward(knights);
knights &= knights-1;
uint64_t nSq = getKnightSquares(stSq);
pml.add(PieceMoveInfo(KNIGHTS, stSq, nSq));
}
pml.starts[BISHOPS] = pml.size();
uint64_t occ = allPieces[color^1] | pieces[color][PAWNS] | pieces[color][KNIGHTS] | pieces[color][KINGS];
uint64_t bishops = pieces[color][BISHOPS];
while (bishops) {
int stSq = bitScanForward(bishops);
bishops &= bishops-1;
uint64_t bSq = getBishopSquares(stSq, occ | pieces[color][ROOKS]);
pml.add(PieceMoveInfo(BISHOPS, stSq, bSq));
}
pml.starts[ROOKS] = pml.size();
uint64_t rooks = pieces[color][ROOKS];
while (rooks) {
int stSq = bitScanForward(rooks);
rooks &= rooks-1;
uint64_t rSq = getRookSquares(stSq, occ | pieces[color][BISHOPS]);
pml.add(PieceMoveInfo(ROOKS, stSq, rSq));
}
pml.starts[QUEENS] = pml.size();
uint64_t queens = pieces[color][QUEENS];
while (queens) {
int stSq = bitScanForward(queens);
queens &= queens-1;
uint64_t qSq = getQueenSquares(stSq, occ);
pml.add(PieceMoveInfo(QUEENS, stSq, qSq));
}
return pml;
}
// Get all legal moves and captures
MoveList Board::getAllLegalMoves(int color) const {
MoveList moves;
getAllPseudoLegalMoves(moves, color);
for (unsigned int i = 0; i < moves.size(); i++) {
Board b = staticCopy();
b.doMove(moves.get(i), color);
if (b.isInCheck(color)) {
moves.remove(i);
i--;
}
}
return moves;
}
//------------------------------Pseudo-legal Moves------------------------------
/* Pseudo-legal moves disregard whether the player's king is left in check
* The pseudo-legal move and capture generators all follow a similar scheme:
* Bitscan to obtain the square number for each piece (a1 is 0, a2 is 1, h8 is 63).
* Get the legal moves as a bitboard, then bitscan this to get the destination
* square and store as a Move object.
*/
void Board::getAllPseudoLegalMoves(MoveList &legalMoves, int color) const {
getPseudoLegalCaptures(legalMoves, color, true);
getPseudoLegalQuiets(legalMoves, color);
}
/*
* Quiet moves are generated in the following order:
* Castling
* Knight moves
* Bishop moves
* Rook moves
* Queen moves
* Pawn moves
* King moves
*/
void Board::getPseudoLegalQuiets(MoveList &quiets, int color) const {
addCastlesToList(quiets, color);
addPieceMovesToList<MOVEGEN_QUIETS>(quiets, color);
addPawnMovesToList(quiets, color);
uint64_t kingMoves = getKingSquares(kingSqs[color]);
addMovesToList<MOVEGEN_QUIETS>(quiets, kingSqs[color], kingMoves);
}
/*
* Do not include promotions for quiescence search, include promotions in normal search.
* Captures are generated in the following order:
* King captures
* Pawn captures
* Knight captures
* Bishop captures
* Rook captures
* Queen captures
*/
void Board::getPseudoLegalCaptures(MoveList &captures, int color, bool includePromotions) const {
uint64_t otherPieces = allPieces[color^1];
uint64_t kingMoves = getKingSquares(kingSqs[color]);
addMovesToList<MOVEGEN_CAPTURES>(captures, kingSqs[color], kingMoves, otherPieces);
addPawnCapturesToList(captures, color, otherPieces, includePromotions);
addPieceMovesToList<MOVEGEN_CAPTURES>(captures, color, otherPieces);
}
// Generates all queen promotions for quiescence search
void Board::getPseudoLegalPromotions(MoveList &moves, int color) const {
uint64_t otherPieces = allPieces[color^1];
uint64_t pawns = pieces[color][PAWNS];
uint64_t finalRank = (color == WHITE) ? RANK_8 : RANK_1;
int leftDiff = (color == WHITE) ? -7 : 9;
int rightDiff = (color == WHITE) ? -9 : 7;
uint64_t legal = (color == WHITE) ? getWPawnLeftCaptures(pawns)
: getBPawnLeftCaptures(pawns);
legal &= otherPieces;
uint64_t promotions = legal & finalRank;
while (promotions) {
int endSq = bitScanForward(promotions);
promotions &= promotions-1;
Move mq = encodeMove(endSq+leftDiff, endSq);
mq = setCapture(mq, true);
mq = setFlags(mq, MOVE_PROMO_Q);
moves.add(mq);
}
legal = (color == WHITE) ? getWPawnRightCaptures(pawns)
: getBPawnRightCaptures(pawns);
legal &= otherPieces;
promotions = legal & finalRank;
while (promotions) {
int endSq = bitScanForward(promotions);
promotions &= promotions-1;
Move mq = encodeMove(endSq+rightDiff, endSq);
mq = setCapture(mq, true);
mq = setFlags(mq, MOVE_PROMO_Q);
moves.add(mq);
}
int sqDiff = (color == WHITE) ? -8 : 8;
legal = (color == WHITE) ? getWPawnSingleMoves(pawns)
: getBPawnSingleMoves(pawns);
promotions = legal & finalRank;
while (promotions) {
int endSq = bitScanForward(promotions);
promotions &= promotions - 1;
int stSq = endSq + sqDiff;
Move mq = encodeMove(stSq, endSq);
mq = setFlags(mq, MOVE_PROMO_Q);
moves.add(mq);
}
}
/*
* Get all pseudo-legal non-capture checks for a position. Used in quiescence search.
* This function can be optimized compared to a normal getLegalMoves() because
* for each piece, we can intersect the legal moves of the piece with the attack
* map of this piece to the opposing king square to determine direct checks.
* Discovered checks have to be handled separately.
*
* For simplicity, promotions and en passant are left out of this function.
*/
void Board::getPseudoLegalChecks(MoveList &checks, int color) const {
int kingSq = kingSqs[color^1];
// Square parity for knight and bishop moves
uint64_t kingParity = (pieces[color^1][KINGS] & LIGHT) ? LIGHT : DARK;
uint64_t potentialXRay = pieces[color][BISHOPS] | pieces[color][ROOKS] | pieces[color][QUEENS];
// We can do pawns in parallel, since the start square of a pawn move is
// determined by its end square.
uint64_t pawns = pieces[color][PAWNS];
// First, deal with discovered checks
// TODO this is way too slow
/*
uint64_t tempPawns = pawns;
while (tempPawns) {
int stsq = bitScanForward(tempPawns);
tempPawns &= tempPawns - 1;
uint64_t xrays = getXRayPieceMap(color, kingSq, color, indexToBit(stsq));
// If moving the pawn caused a new xray piece to attack the king
if (!(xrays & invAttackMap)) {
// Every legal move of this pawn is a legal check
uint64_t legal = (color == WHITE) ? getWPawnSingleMoves(indexToBit(stsq)) | getWPawnDoubleMoves(indexToBit(stsq))
: getBPawnSingleMoves(indexToBit(stsq)) | getBPawnDoubleMoves(indexToBit(stsq));
while (legal) {
int endsq = bitScanForward(legal);
legal &= legal - 1;
checks.add(encodeMove(stsq, endsq, PAWNS, false));
}
// Remove this pawn from future consideration
pawns ^= indexToBit(stsq);
}
}
*/
uint64_t pAttackMap = (color == WHITE)
? getBPawnCaptures(indexToBit(kingSq))
: getWPawnCaptures(indexToBit(kingSq));
uint64_t finalRank = (color == WHITE) ? RANK_8 : RANK_1;
int sqDiff = (color == WHITE) ? -8 : 8;
uint64_t pLegal = (color == WHITE) ? getWPawnSingleMoves(pawns)
: getBPawnSingleMoves(pawns);
// Remove promotions
uint64_t promotions = pLegal & finalRank;
pLegal ^= promotions;
pLegal &= pAttackMap;
while (pLegal) {
int endsq = bitScanForward(pLegal);
pLegal &= pLegal - 1;
checks.add(encodeMove(endsq+sqDiff, endsq));
}
pLegal = (color == WHITE) ? getWPawnDoubleMoves(pawns)
: getBPawnDoubleMoves(pawns);
pLegal &= pAttackMap;
while (pLegal) {
int endsq = bitScanForward(pLegal);
pLegal &= pLegal - 1;
Move m = encodeMove(endsq+2*sqDiff, endsq);
m = setFlags(m, MOVE_DOUBLE_PAWN);
checks.add(m);
}
uint64_t occ = getOccupancy();
uint64_t knights = pieces[color][KNIGHTS] & kingParity;
uint64_t nAttackMap = getKnightSquares(kingSq);
while (knights) {
int stsq = bitScanForward(knights);
knights &= knights-1;
uint64_t nSq = getKnightSquares(stsq);
// Get any bishops, rooks, queens attacking king after knight has moved
uint64_t xrays = getXRayPieceMap(color, kingSq, occ ^ indexToBit(stsq));
// If still no xrayers are giving check, then we have no discovered
// check. Otherwise, every move by this piece is a (discovered) checking
// move
if (!(xrays & potentialXRay))
nSq &= nAttackMap;
addMovesToList<MOVEGEN_QUIETS>(checks, stsq, nSq);
}
uint64_t bishops = pieces[color][BISHOPS] & kingParity;
uint64_t bAttackMap = getBishopSquares(kingSq, occ);
while (bishops) {
int stsq = bitScanForward(bishops);
bishops &= bishops-1;
uint64_t bSq = getBishopSquares(stsq, occ);
uint64_t xrays = getXRayPieceMap(color, kingSq, occ ^ indexToBit(stsq));
if (!(xrays & potentialXRay))
bSq &= bAttackMap;
addMovesToList<MOVEGEN_QUIETS>(checks, stsq, bSq);
}
uint64_t rooks = pieces[color][ROOKS];
uint64_t rAttackMap = getRookSquares(kingSq, occ);
while (rooks) {
int stsq = bitScanForward(rooks);
rooks &= rooks-1;
uint64_t rSq = getRookSquares(stsq, occ);
uint64_t xrays = getXRayPieceMap(color, kingSq, occ ^ indexToBit(stsq));
if (!(xrays & potentialXRay))
rSq &= rAttackMap;
addMovesToList<MOVEGEN_QUIETS>(checks, stsq, rSq);
}
uint64_t queens = pieces[color][QUEENS];
uint64_t qAttackMap = getQueenSquares(kingSq, occ);
while (queens) {
int stsq = bitScanForward(queens);
queens &= queens-1;
uint64_t qSq = getQueenSquares(stsq, occ) & qAttackMap;
addMovesToList<MOVEGEN_QUIETS>(checks, stsq, qSq);
}
}
// Generate moves that (sort of but not really) get out of check
// This can only be used if we know the side to move is in check
// Optimizations include looking for double check (king moves only),
// otherwise we can only capture the checker or block if it is an xray piece
void Board::getPseudoLegalCheckEscapes(MoveList &escapes, int color) const {
int kingSq = kingSqs[color];
uint64_t otherPieces = allPieces[color^1];
uint64_t attackMap = getAttackMap(color^1, kingSq);
// Consider only captures of pieces giving check
otherPieces &= attackMap;
// If double check, we can only move the king
if (count(otherPieces) >= 2) {
uint64_t kingMoves = getKingSquares(kingSq);
addMovesToList<MOVEGEN_CAPTURES>(escapes, kingSq, kingMoves, allPieces[color^1]);
addMovesToList<MOVEGEN_QUIETS>(escapes, kingSq, kingMoves);
return;
}
addPawnCapturesToList(escapes, color, otherPieces, true);
uint64_t occ = getOccupancy();
// If bishops, rooks, or queens, get bitboard of attack path so we
// can intersect with legal moves to get legal block moves
uint64_t xraySqs = 0;
int attackerSq = bitScanForward(otherPieces);
int attackerType = getPieceOnSquare(color^1, attackerSq);
if (attackerType == BISHOPS)
xraySqs = getBishopSquares(attackerSq, occ);
else if (attackerType == ROOKS)
xraySqs = getRookSquares(attackerSq, occ);
else if (attackerType == QUEENS)
xraySqs = getQueenSquares(attackerSq, occ);
addPieceMovesToList<MOVEGEN_CAPTURES>(escapes, color, otherPieces);
uint64_t kingMoves = getKingSquares(kingSqs[color]);
addMovesToList<MOVEGEN_CAPTURES>(escapes, kingSqs[color], kingMoves, allPieces[color^1]);
addPawnMovesToList(escapes, color);
uint64_t knights = pieces[color][KNIGHTS];
while (knights) {
int stSq = bitScanForward(knights);
knights &= knights-1;
uint64_t nSq = getKnightSquares(stSq);
addMovesToList<MOVEGEN_QUIETS>(escapes, stSq, nSq & xraySqs);
}
uint64_t bishops = pieces[color][BISHOPS];
while (bishops) {
int stSq = bitScanForward(bishops);
bishops &= bishops-1;
uint64_t bSq = getBishopSquares(stSq, occ);
addMovesToList<MOVEGEN_QUIETS>(escapes, stSq, bSq & xraySqs);
}
uint64_t rooks = pieces[color][ROOKS];
while (rooks) {
int stSq = bitScanForward(rooks);
rooks &= rooks-1;
uint64_t rSq = getRookSquares(stSq, occ);
addMovesToList<MOVEGEN_QUIETS>(escapes, stSq, rSq & xraySqs);
}
uint64_t queens = pieces[color][QUEENS];
while (queens) {
int stSq = bitScanForward(queens);
queens &= queens-1;
uint64_t qSq = getQueenSquares(stSq, occ);
addMovesToList<MOVEGEN_QUIETS>(escapes, stSq, qSq & xraySqs);
}
addMovesToList<MOVEGEN_QUIETS>(escapes, kingSqs[color], kingMoves);
}
//------------------------------------------------------------------------------
//---------------------------Move Generation Helpers----------------------------
//------------------------------------------------------------------------------
// We can do pawns in parallel, since the start square of a pawn move is
// determined by its end square.
void Board::addPawnMovesToList(MoveList &quiets, int color) const {
uint64_t pawns = pieces[color][PAWNS];
uint64_t finalRank = (color == WHITE) ? RANK_8 : RANK_1;
int sqDiff = (color == WHITE) ? -8 : 8;
uint64_t pLegal = (color == WHITE) ? getWPawnSingleMoves(pawns)
: getBPawnSingleMoves(pawns);
// Promotions occur when a pawn reaches the final rank
uint64_t promotions = pLegal & finalRank;
pLegal ^= promotions;
while (promotions) {
int endSq = bitScanForward(promotions);
promotions &= promotions - 1;
int stSq = endSq + sqDiff;
addPromotionsToList<MOVEGEN_QUIETS>(quiets, stSq, endSq);
}
while (pLegal) {
int endsq = bitScanForward(pLegal);
pLegal &= pLegal - 1;
quiets.add(encodeMove(endsq+sqDiff, endsq));
}
pLegal = (color == WHITE) ? getWPawnDoubleMoves(pawns)
: getBPawnDoubleMoves(pawns);
while (pLegal) {
int endsq = bitScanForward(pLegal);
pLegal &= pLegal - 1;
Move m = encodeMove(endsq+2*sqDiff, endsq);
m = setFlags(m, MOVE_DOUBLE_PAWN);
quiets.add(m);
}
}
// For pawn captures, we can use a similar approach, but we must consider
// left-hand and right-hand captures separately so we can tell which
// pawn is doing the capturing.
void Board::addPawnCapturesToList(MoveList &captures, int color, uint64_t otherPieces, bool includePromotions) const {
uint64_t pawns = pieces[color][PAWNS];
uint64_t finalRank = (color == WHITE) ? RANK_8 : RANK_1;
int leftDiff = (color == WHITE) ? -7 : 9;
int rightDiff = (color == WHITE) ? -9 : 7;
uint64_t legal = (color == WHITE) ? getWPawnLeftCaptures(pawns)
: getBPawnLeftCaptures(pawns);
legal &= otherPieces;
uint64_t promotions = legal & finalRank;
legal ^= promotions;
if (includePromotions) {
while (promotions) {
int endSq = bitScanForward(promotions);
promotions &= promotions-1;
addPromotionsToList<MOVEGEN_CAPTURES>(captures, endSq+leftDiff, endSq);
}
}
while (legal) {
int endsq = bitScanForward(legal);
legal &= legal-1;
Move m = encodeMove(endsq+leftDiff, endsq);
m = setCapture(m, true);
captures.add(m);
}
legal = (color == WHITE) ? getWPawnRightCaptures(pawns)
: getBPawnRightCaptures(pawns);
legal &= otherPieces;
promotions = legal & finalRank;
legal ^= promotions;
if (includePromotions) {
while (promotions) {
int endSq = bitScanForward(promotions);
promotions &= promotions-1;
addPromotionsToList<MOVEGEN_CAPTURES>(captures, endSq+rightDiff, endSq);
}
}
while (legal) {
int endsq = bitScanForward(legal);
legal &= legal-1;
Move m = encodeMove(endsq+rightDiff, endsq);
m = setCapture(m, true);
captures.add(m);
}
// If there are en passants possible...
if (epCaptureFile != NO_EP_POSSIBLE) {
int victimSq = epVictimSquare(color^1, epCaptureFile);
// capturer's destination square is either the rank above (white) or
// below (black) the victim square
int rankDiff = (color == WHITE) ? 8 : -8;
// The capturer's start square is either 1 to the left or right of victim
if ((indexToBit(victimSq) << 1) & NOTA & pieces[color][PAWNS]) {
Move m = encodeMove(victimSq+1, victimSq+rankDiff);
m = setFlags(m, MOVE_EP);
captures.add(m);
}
if ((indexToBit(victimSq) >> 1) & NOTH & pieces[color][PAWNS]) {
Move m = encodeMove(victimSq-1, victimSq+rankDiff);
m = setFlags(m, MOVE_EP);
captures.add(m);
}
}
}
template <bool isCapture>
void Board::addPieceMovesToList(MoveList &moves, int color, uint64_t otherPieces) const {
uint64_t knights = pieces[color][KNIGHTS];
while (knights) {
int stSq = bitScanForward(knights);
knights &= knights-1;
uint64_t nSq = getKnightSquares(stSq);
addMovesToList<isCapture>(moves, stSq, nSq, otherPieces);
}
uint64_t occ = getOccupancy();
uint64_t bishops = pieces[color][BISHOPS];
while (bishops) {
int stSq = bitScanForward(bishops);
bishops &= bishops-1;
uint64_t bSq = getBishopSquares(stSq, occ);
addMovesToList<isCapture>(moves, stSq, bSq, otherPieces);
}
uint64_t rooks = pieces[color][ROOKS];
while (rooks) {
int stSq = bitScanForward(rooks);
rooks &= rooks-1;
uint64_t rSq = getRookSquares(stSq, occ);
addMovesToList<isCapture>(moves, stSq, rSq, otherPieces);
}
uint64_t queens = pieces[color][QUEENS];
while (queens) {
int stSq = bitScanForward(queens);
queens &= queens-1;
uint64_t qSq = getQueenSquares(stSq, occ);
addMovesToList<isCapture>(moves, stSq, qSq, otherPieces);
}
}
// Helper function that processes a bitboard of legal moves and adds all
// moves into a list.
template <bool isCapture>
void Board::addMovesToList(MoveList &moves, int stSq, uint64_t allEndSqs, uint64_t otherPieces) const {
uint64_t intersect = (isCapture) ? otherPieces : ~getOccupancy();
uint64_t legal = allEndSqs & intersect;
while (legal) {
int endSq = bitScanForward(legal);
legal &= legal-1;
Move m = encodeMove(stSq, endSq);
if (isCapture)
m = setCapture(m, true);
moves.add(m);
}
}
template <bool isCapture>
void Board::addPromotionsToList(MoveList &moves, int stSq, int endSq) const {
Move mk = encodeMove(stSq, endSq);
mk = setFlags(mk, MOVE_PROMO_N);
Move mb = encodeMove(stSq, endSq);
mb = setFlags(mb, MOVE_PROMO_B);
Move mr = encodeMove(stSq, endSq);
mr = setFlags(mr, MOVE_PROMO_R);
Move mq = encodeMove(stSq, endSq);
mq = setFlags(mq, MOVE_PROMO_Q);
if (isCapture) {
mk = setCapture(mk, true);
mb = setCapture(mb, true);
mr = setCapture(mr, true);
mq = setCapture(mq, true);
}
// Order promotions queen, knight, rook, bishop
moves.add(mq);
moves.add(mk);
moves.add(mr);
moves.add(mb);
}
void Board::addCastlesToList(MoveList &moves, int color) const {
// Add all possible castles
if (color == WHITE) {
// If castling rights still exist, squares in between king and rook are
// empty, and player is not in check
if ((castlingRights & WHITEKSIDE)
&& (getOccupancy() & WHITE_KSIDE_PASSTHROUGH_SQS) == 0
&& !isInCheck(WHITE)) {
// Check for castling through check
if (getAttackMap(BLACK, 5) == 0) {
Move m = encodeMove(4, 6);
m = setCastle(m, true);
moves.add(m);
}
}
if ((castlingRights & WHITEQSIDE)
&& (getOccupancy() & WHITE_QSIDE_PASSTHROUGH_SQS) == 0
&& !isInCheck(WHITE)) {
if (getAttackMap(BLACK, 3) == 0) {
Move m = encodeMove(4, 2);
m = setCastle(m, true);
moves.add(m);
}