-
Notifications
You must be signed in to change notification settings - Fork 0
/
Board.hpp
1517 lines (1390 loc) · 53.3 KB
/
Board.hpp
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
#pragma once
#ifdef FLAG_JEMALLOC_EXTERNAL
#include <jemalloc/jemalloc.h>
#endif
#include <vector>
#include <list>
#include <bitset>
#include <String.hpp>
#include <Piece.hpp>
#include <FEN.hpp>
#include <Zobrist.hpp>
#include <MoveScope.hpp>
#include <MoveLine.hpp>
// board view of the game
class Board {
protected:
static bool initialized_;
COLOR activePlayer_;
size_t current_ply_ = 0;
public:
// general chess960-specific variables
const bool chess960 = false;
std::array<pos_t, NO_COLORS>
kcastlrook = {0xff, 0xff},
qcastlrook = {0xff, 0xff};
// general crazyhouse-specific variables
const bool crazyhouse = false;
// cheap abstractions, pieces[Piece::piece_index(PAWN, BLACK)]
static constexpr std::array<Piece, board::NO_PIECE_INDICES+1> pieces = {
Piece(PAWN, WHITE), Piece(PAWN, BLACK),
Piece(KNIGHT, WHITE), Piece(KNIGHT, BLACK),
Piece(BISHOP, WHITE), Piece(BISHOP, BLACK),
Piece(ROOK, WHITE), Piece(ROOK, BLACK),
Piece(QUEEN, WHITE), Piece(QUEEN, BLACK),
Piece(KING, WHITE), Piece(KING, BLACK),
Piece(EMPTY, NEUTRAL)
};
// compressed (partially incomplete) board state for hashing
struct board_info {
COLOR active_player = NEUTRAL;
pos_t enpassant;
pos_pair_t castlings;
piece_bitboard_t whites, blacks, diag_slid, orth_slid, pawns;
uint64_t n_subs_mask;
piece_bitboard_t promoted_pawns;
INLINE bool operator==(board_info other) const noexcept {
return active_player == other.active_player &&
pawns == other.pawns &&
whites == other.whites && blacks == other.blacks &&
diag_slid == other.diag_slid && orth_slid == other.orth_slid &&
castlings == other.castlings && enpassant == other.enpassant &&
n_subs_mask == other.n_subs_mask && promoted_pawns == other.promoted_pawns;
}
// efficiently invalidate state for comparison purposes
INLINE void unset() {
active_player = NEUTRAL;
}
// check whether state is valid
INLINE bool is_unset() const {
return active_player == NEUTRAL;
}
// uncompressed king position
INLINE pos_t pos_king(COLOR c) const {
assert(c < NO_COLORS);
if(c == WHITE) {
return pos_t(pawns);
} else {
return pos_t(pawns >> (7 * board::LEN));
}
}
// uncompressed pawn positions
INLINE piece_bitboard_t pawn_bits() const {
return pawns & ((bitmask::full >> (board::LEN * 2)) << board::LEN);
}
// 64-bit word, each 8 bit word specifies a number of substitution pieces for the type
std::array<pos_t, board::NO_DROPPIECE_INDICES> get_n_subs() const {
std::array<pos_t, board::NO_DROPPIECE_INDICES> nsubs;
for(pos_t i = 0; i < board::NO_DROPPIECE_INDICES; ++i) {
//nsubs[i] = 0;
nsubs[i] = (n_subs_mask >> (i * 6)) & (board::SIZE - 1);
}
return nsubs;
}
// get_n_subs externally interfaced
static std::array<pos_t, board::NO_DROPPIECE_INDICES> get_n_subs(piece_bitboard_t n_subs_mask) {
std::array<pos_t, board::NO_DROPPIECE_INDICES> nsubs;
for(pos_t i = 0; i < board::NO_DROPPIECE_INDICES; ++i) {
//nsubs[i] = 0;
nsubs[i] = (n_subs_mask >> (i * 6)) & (board::SIZE - 1);
}
return nsubs;
}
};
public:
// irregular state variables:
// plies at which en-passants are set
std::vector<std::pair<ply_index_t, pos_t>> state_hist_enpassants;
// KQkq
std::array<ply_index_t, 4> castlings = {board::nocastlings, board::nocastlings, board::nocastlings, board::nocastlings};
// at which moves halfmove clock is reset
std::vector<ply_index_t> state_hist_halfmoves;
// when three-fold repetition occured:
ply_index_t state_hist_repetitions = INT16_MAX;
// bitboard representation
std::array<piece_bitboard_t, NO_COLORS> bits = {0x00, 0x00};
piece_bitboard_t bits_slid_diag = 0x00, bits_slid_orth = 0x00, bits_pawns = 0x00;
std::array<pos_t, NO_COLORS> pos_king = {board::nopos, board::nopos};
// crazyhouse-specific state variables
piece_bitboard_t bits_promoted_pawns = 0ULL;
std::array<pos_t, board::NO_DROPPIECE_INDICES> n_subs = {0};
// regular board state variables
struct board_state {
// mailbox can represent any bipartite board graph
using board_mailbox_t = std::array<piece_bitboard_t, board::SIZE>;
// null moves are fake, and should not trigger repetition and such
bool null_move_state;
// compressed board state: bijectively restore it when needed
board_info info;
board_mailbox_t attacks;
bool moves_initialized = false;
std::array<piece_bitboard_t, NO_COLORS> checkline = {0x00,0x00};
board_mailbox_t moves;
std::array<piece_bitboard_t, NO_COLORS> pins;
};
// current state
board_state state;
// (only) previous states, for fast unmaking
std::vector<board_state> state_hist;
size_t zobrist_size;
bool b_finalize = false;
explicit Board(const fen::FEN &f=fen::starting_pos, size_t zbsize=ZOBRIST_SIZE):
activePlayer_(f.active_player),
current_ply_(f.fullmove * 2 - (f.active_player == WHITE ? 1 : 0)),
chess960(f.chess960), crazyhouse(f.crazyhouse),
zobrist_size(bitmask::highest_bit(zbsize))
{
// external initializations (singleton)
if(!initialized_) {
M42::init();
zobrist::init(zobrist_size);
initialized_ = true;
}
// set bitboards
for(pos_t i = 0; i < f.board.length(); ++i) {
if(f.board[i]==' ')continue;
assert(i < board::SIZE);
const COLOR c = islower(f.board[i]) ? BLACK : WHITE;
PIECE p = EMPTY;
switch(tolower(f.board[i])) {
case 'p':p=PAWN;break;
case 'n':p=KNIGHT;break;
case 'b':p=BISHOP;break;
case 'r':p=ROOK;break;
case 'q':p=QUEEN;break;
case 'k':p=KING;break;
}
const pos_t x = board::_x(i), y = board::LEN - board::_y(i) - 1;
const pos_t pos = board::_pos(A+x, 1+y);
put_pos(pos, Piece(p, c));
if(f.crazyhouse && piece::is_set(f.crazyhouse_promoted, i)) {
piece::set_pos(self.bits_promoted_pawns, pos);
}
}
// crazyhouse: set substitution counts
if(crazyhouse) {
for(COLOR c : {WHITE, BLACK}) {
for(PIECE p : {PAWN, KNIGHT, BISHOP, ROOK, QUEEN}) {
const Piece piece = Piece(p, c);
n_subs[piece.piece_index] = std::count(f.subs.begin(), f.subs.end(), piece.str());
}
}
}
// set white castlings state (variation and position)
if(bitmask::first(f.castlings)) {
const pos_t wcastlmask = bitmask::first(f.castlings);
if(bitmask::log2_lsb(wcastlmask) < board::_x(pos_king[WHITE])) {
qcastlrook[WHITE] = bitmask::log2_lsb(wcastlmask);
} else {
unset_castling(WHITE, QUEEN_SIDE);
}
if(bitmask::log2(wcastlmask) > board::_x(pos_king[WHITE])) {
kcastlrook[WHITE] = bitmask::log2(wcastlmask);
} else {
unset_castling(WHITE, KING_SIDE);
}
} else {
unset_castling(WHITE, KING_SIDE);
unset_castling(WHITE, QUEEN_SIDE);
}
// set black castlings state (variation and position)
if(bitmask::second(f.castlings)) {
const pos_t bcastlmask = bitmask::second(f.castlings);
if(bitmask::log2_lsb(bcastlmask) < board::_x(pos_king[BLACK])) {
qcastlrook[BLACK] = bitmask::log2_lsb(bcastlmask);
} else {
unset_castling(BLACK, QUEEN_SIDE);
}
if(bitmask::log2(bcastlmask) > board::_x(pos_king[BLACK])) {
kcastlrook[BLACK] = bitmask::log2(bcastlmask);
} else {
unset_castling(BLACK, KING_SIDE);
}
} else {
unset_castling(BLACK, KING_SIDE);
unset_castling(BLACK, QUEEN_SIDE);
}
// initialize irregular state variables
state_hist_halfmoves.emplace_back(current_ply_ - f.halfmove_clock);
set_enpassant(f.enpassant);
state_hist_halfmoves.reserve(piece::size(bits[WHITE] | bits[BLACK]) - 2);
state_hist_enpassants.reserve(16);
// initialize regular state variables
state_hist.reserve(100);
init_state_attacks();
clear_state_unfinalize();
init_state_checkline(activePlayer());
init_state_moves();
update_state_info();
state.null_move_state = false;
}
INLINE constexpr COLOR activePlayer() const {
return activePlayer_;
}
// return piece-type interface, expensive
INLINE const Piece operator[](pos_t ind) const {
assert(ind <= board::MOVEMASK);
const piece_bitboard_t ind_mask = piece::pos_mask(ind);
const COLOR c = self.color_at_pos(ind);
if(c == NEUTRAL) {
return Piece(EMPTY, NEUTRAL);
}
if(bits_pawns & ind_mask) {
return Piece(PAWN, c);
} else if(bits_slid_diag & bits_slid_orth & ind_mask) {
return Piece(QUEEN, c);
} else if(bits_slid_diag & ind_mask) {
return Piece(BISHOP, c);
} else if(bits_slid_orth & ind_mask) {
return Piece(ROOK, c);
} else if(pos_king[c] == ind) {
return Piece(KING, c);
} else {
return Piece(KNIGHT, c);
}
abort();
}
// cheap lookup for emptiness
INLINE bool empty_at_pos(pos_t ind) const {
assert(ind <= board::MOVEMASK);
return !piece::is_set(bits[WHITE]|bits[BLACK], ind);
}
INLINE COLOR color_at_pos(pos_t ind) const {
assert(ind <= board::MOVEMASK);
const piece_bitboard_t ind_mask = piece::pos_mask(ind);
if(bits[WHITE] & ind_mask) {
return WHITE;
} else if(bits[BLACK] & ind_mask) {
return BLACK;
}
return NEUTRAL;
}
// update compressed (incomplete) state information
INLINE void update_state_info() {
uint64_t n_subs_mask = 0x00;
if(crazyhouse) {
for(pos_t i = 0; i < board::NO_DROPPIECE_INDICES; ++i) {
n_subs_mask |= uint64_t(n_subs[i]) << (i * 6);
}
assert(n_subs == board_info::get_n_subs(n_subs_mask));
}
state.info = (board_info){
.active_player=activePlayer(),
.enpassant=enpassant_trace(),
.castlings=get_castlings_rook_mask(),
.whites=bits[WHITE],
.blacks=bits[BLACK],
.diag_slid=bits_slid_diag,
.orth_slid=bits_slid_orth,
.pawns=bits_pawns
| (uint64_t(pos_king[BLACK]) << 7*board::LEN) \
| uint64_t(pos_king[WHITE]),
.n_subs_mask=n_subs_mask,
.promoted_pawns=bits_promoted_pawns
};
}
// methods to read and update bitboards
// obtain kings bitboard
INLINE piece_bitboard_t get_king_bits() const {
return piece::pos_mask(pos_king[WHITE]) | piece::pos_mask(pos_king[BLACK]);
}
// infer knights bitboard
INLINE piece_bitboard_t get_knight_bits() const {
return (bits[WHITE] | bits[BLACK]) ^ (bits_slid_diag | bits_slid_orth | bits_pawns | get_king_bits());
}
// remove i from bitboard representation
INLINE void unset_pos(pos_t i) {
assert(i < 64);
const piece_bitboard_t i_mask = piece::pos_mask(i);
if(bits[WHITE] & i_mask) {
piece::unset_pos(bits[WHITE], i);
// if(self[i].value == KING && pos_king[WHITE] == i) {
// pos_king[WHITE] = board::nopos;
// }
} else if(bits[BLACK] & i_mask) {
piece::unset_pos(bits[BLACK], i);
// if(self[i].value == KING && pos_king[BLACK] == i) {
// pos_king[BLACK] = board::nopos;
// }
}
if(bits_pawns & i_mask) {
piece::unset_pos(bits_pawns, i);
} else {
if(bits_slid_diag & i_mask) {
piece::unset_pos(bits_slid_diag, i);
}
if(bits_slid_orth & i_mask) {
piece::unset_pos(bits_slid_orth, i);
}
}
}
// add i to bitboard representation (for the given piece type and color)
INLINE void set_pos(pos_t i, const Piece p) {
if(p.color == WHITE) {
piece::set_pos(bits[WHITE], i);
if(p.value == KING) {
pos_king[WHITE] = i;
}
} else if(p.color == BLACK) {
piece::set_pos(bits[BLACK], i);
if(p.value == KING) {
pos_king[BLACK] = i;
}
}
switch(p.value) {
case BISHOP:piece::set_pos(bits_slid_diag, i);break;
case ROOK:piece::set_pos(bits_slid_orth, i);break;
case QUEEN:piece::set_pos(bits_slid_orth, i);
piece::set_pos(bits_slid_diag, i);break;
case PAWN:piece::set_pos(bits_pawns, i);break;
default:break;
}
}
// replace whatever is at i with a given piece-type
INLINE void put_pos(pos_t i, const Piece p) {
unset_pos(i);
set_pos(i, p);
}
// move i to j, emptiness at j guaranteed
INLINE void move_pos_quiet(pos_t i, pos_t j) {
assert(self.empty_at_pos(j));
// can't be promotion
assert(j <= board::MOVEMASK);
const piece_bitboard_t i_mask = piece::pos_mask(i);
const COLOR c = self.color_at_pos(i);
piece::move_pos(bits[c], i, j);
if(bits_pawns & i_mask) {
piece::move_pos(bits_pawns, i, j);
} else if(pos_king[c] == i) {
pos_king[c] = j;
} else {
if(bits_slid_diag & i_mask) {
piece::move_pos(bits_slid_diag, i, j);
}
if(bits_slid_orth & i_mask) {
piece::move_pos(bits_slid_orth, i, j);
}
}
}
INLINE void move_pos(pos_t i, pos_t j) {
assert(j <= board::MOVEMASK);
unset_pos(j);
move_pos_quiet(i, j);
}
// methods to determine move types
INLINE bool is_castling_move(pos_t i, pos_t j) const {
assert(j <= board::MOVEMASK);
const COLOR c = self.color_at_pos(i);
const pos_t castlrank = (c == WHITE) ? 1 : 8;
if(!chess960) {
return (i == pos_king[c]) && (i == board::_pos(E, castlrank))
&& (j == board::_pos(C, castlrank) || j == board::_pos(G, castlrank));
} else {
return (i == pos_king[c]) && (bits_slid_orth & ~bits_slid_diag & bits[c] & piece::pos_mask(j));
}
}
INLINE bool is_doublepush_move(pos_t i, pos_t j) const {
assert(i <= board::MOVEMASK && j <= board::MOVEMASK);
if(~bits_pawns & piece::pos_mask(i))return false;
const COLOR c = self.color_at_pos(i);
return piece::is_pawn_double_push(c, i, j);
}
INLINE bool is_drop_move(pos_t i, pos_t j) const {
return i & board::CRAZYHOUSE_DROP;
}
INLINE bool is_enpassant_take_move(pos_t i, pos_t j) const {
assert(i <= board::MOVEMASK && j <= board::MOVEMASK);
return (bits_pawns & piece::pos_mask(i)) && j == enpassant_trace();
}
INLINE bool is_promotion_move(pos_t i, pos_t j) const {
assert(i <= board::MOVEMASK && j <= board::MOVEMASK);
if(~bits_pawns & piece::pos_mask(i))return false;
const COLOR c = self.color_at_pos(i);
return piece::is_pawn_promotion_move(c, i, j);
}
INLINE bool is_naively_capture_move(pos_t i, pos_t j) const {
assert(i <= board::MOVEMASK && j <= board::MOVEMASK);
const COLOR c = self.color_at_pos(i);
return bits[enemy_of(c)] & piece::pos_mask(j);
}
INLINE bool is_naively_capture_move(move_t m) const {
return is_naively_capture_move(bitmask::first(m), bitmask::second(m));
}
INLINE bool is_naively_checking_move(pos_t i, pos_t j) const {
j &= board::MOVEMASK;
const COLOR c = activePlayer();
const pos_t target = pos_king[enemy_of(c)];
const piece_bitboard_t occupied = (bits[WHITE] | bits[BLACK]) & ~piece::pos_mask(i);
if(bits_pawns & piece::pos_mask(i)) {
return piece::get_pawn_attack(j, c) & piece::pos_mask(target);
}
if(bits_slid_diag & piece::pos_mask(i)) {
const piece_bitboard_t mask = piece::get_sliding_diag_attack(j, occupied) & piece::pos_mask(target);
if(mask)return true;
}
if(bits_slid_orth & piece::pos_mask(i)) {
const piece_bitboard_t mask = piece::get_sliding_orth_attack(j, occupied) & piece::pos_mask(target);
if(mask)return true;
}
if(get_knight_bits() & piece::pos_mask(i)) {
return piece::get_knight_attack(j) & piece::pos_mask(target);
}
return false;
}
// methods to read and update irregular state variables
INLINE pos_t enpassant_trace() const {
if(state_hist_enpassants.empty())return board::nopos;
const auto [ply, e] = state_hist_enpassants.back();
if(ply == get_current_ply()) {
return e;
}
return board::nopos;
}
INLINE pos_t enpassant_pawn() const {
if(enpassant_trace() == board::nopos)return 0xFF;
const pos_t x = board::_x(enpassant_trace());
return board::_y(enpassant_trace()) == 3-1 ? board::_pos(A+x, 4) : board::_pos(A+x, 5);
}
INLINE void set_enpassant(pos_t e) {
if(e != board::nopos) {
state_hist_enpassants.emplace_back(get_current_ply(), e);
}
}
INLINE pos_t get_halfmoves() const {
return get_current_ply() - state_hist_halfmoves.back();
}
INLINE bool is_castling(COLOR c, CASTLING_SIDE side) const {
return castlings[board::_castling_index(c, side)] == board::nocastlings;
}
INLINE void set_castling(COLOR c, CASTLING_SIDE side) {
castlings[board::_castling_index(c, side)] = board::nocastlings;
}
INLINE void unset_castling(COLOR c, CASTLING_SIDE side) {
if(castlings[board::_castling_index(c, side)] >= get_current_ply()) {
castlings[board::_castling_index(c, side)] = get_current_ply();
}
}
INLINE pos_pair_t get_castlings_rook_mask() const {
pos_t wcastl=0x00, bcastl=0x00;
if(is_castling(WHITE, KING_SIDE)) wcastl|=piece::pos_mask(kcastlrook[WHITE]);
if(is_castling(WHITE, QUEEN_SIDE))wcastl|=piece::pos_mask(qcastlrook[WHITE]);
if(is_castling(BLACK, KING_SIDE)) bcastl|=piece::pos_mask(kcastlrook[BLACK]);
if(is_castling(BLACK, QUEEN_SIDE))bcastl|=piece::pos_mask(qcastlrook[BLACK]);
return bitmask::_pos_pair(wcastl, bcastl);
}
void update_castlings(pos_t i, pos_t j) {
const COLOR c = self.color_at_pos(i);
const piece_bitboard_t rooks = bits_slid_orth & ~bits_slid_diag;
if(pos_king[c] == i) {
unset_castling(c, KING_SIDE);
unset_castling(c, QUEEN_SIDE);
} else if(rooks & bits[c] & piece::pos_mask(i)) {
const pos_t castlrank = (c == WHITE) ? 1 : 8;
if(is_castling(c, QUEEN_SIDE) && i == board::_pos(qcastlrook[c], castlrank)) {
unset_castling(c, QUEEN_SIDE);
} else if(is_castling(c, KING_SIDE) && i == board::_pos(kcastlrook[c], castlrank)) {
unset_castling(c, KING_SIDE);
}
}
const COLOR ec = enemy_of(c);
if(rooks & bits[ec] & piece::pos_mask(j)) {
const pos_t ecastlrank = (ec == WHITE) ? 1 : 8;
if(is_castling(ec, QUEEN_SIDE) && j == board::_pos(qcastlrook[ec], ecastlrank)) {
unset_castling(ec, QUEEN_SIDE);
} else if(is_castling(ec, KING_SIDE) && j == board::_pos(kcastlrook[ec], ecastlrank)) {
unset_castling(ec, KING_SIDE);
}
}
}
// methods to get zobrist hashing
piece_bitboard_t get_mask(const Piece p) const {
const COLOR c = p.color;
if(p.value == PAWN) {
return bits_pawns & bits[c];
} else if(p.value == KING) {
return piece::pos_mask(pos_king[c]);
}
piece_bitboard_t mask = 0x00;
if(p.value == BISHOP || p.value == QUEEN) {
mask |= bits_slid_diag;
}
if(p.value == ROOK || p.value == QUEEN) {
mask |= bits_slid_orth;
}
if(p.value == KNIGHT) {
mask = get_knight_bits();
}
return mask & bits[c];
}
INLINE zobrist::key_t zb_hash() const {
return zobrist::zb_hash(self);
}
INLINE zobrist::key_t zb_hash_material(bool mirror=false) const {
return zobrist::zb_hash_material(self, mirror);
}
INLINE ply_index_t get_current_ply() const {
return current_ply_;
}
// methods to check move validity
INLINE bool check_valid_drop_move(pos_t i, pos_t j) const {
const COLOR c = activePlayer();
const COLOR ec = enemy_of(c);
PIECE p = board::get_drop_as(i);
constexpr piece_bitboard_t non_final_ranks = piece::rank_mask(1) | piece::rank_mask(2) | piece::rank_mask(3)
| piece::rank_mask(4) | piece::rank_mask(5) | piece::rank_mask(6);
const piece_bitboard_t drop_locations = ~(bits[c] | bits[ec]) & state.checkline[c];
return crazyhouse
&& is_drop_move(i, j)
&& pos_t(p) < pos_t(NO_PIECES)
&& j <= board::MOVEMASK
&& n_subs[Piece::get_piece_index(board::get_drop_as(i), c)] > 0
&& (
(p != PAWN && (drop_locations & piece::pos_mask(j)))
|| (drop_locations & non_final_ranks & piece::pos_mask(j))
);
}
INLINE bool check_valid_move(pos_t i, pos_t j, bool strict=true) {
#ifndef NDEBUG
const bool tmp = !state.moves_initialized;
if(tmp) {
return true;
}
#endif
return (crazyhouse && check_valid_drop_move(i, j))
|| (!strict && bitmask::_pos_pair(i, j) == board::nullmove)
|| (i == (i & board::MOVEMASK)
&& (bits[activePlayer()] & piece::pos_mask(i))
&& (state.moves[i] & piece::pos_mask(j & board::MOVEMASK)));
}
INLINE bool check_valid_move(const move_t m, bool strict=true) {
return check_valid_move(bitmask::first(m), bitmask::second(m), strict);
}
// visitor methods for the engine
virtual void _restore_on_event() {}
virtual void _update_pos_change(pos_t i, pos_t j) {};
void make_move_unfinalized(pos_t i, pos_t j) {
const move_t m = bitmask::_pos_pair(i, j);
// split promotion and destination information
const pos_t promote_as = j & ~board::MOVEMASK;
j &= board::MOVEMASK;
// some move properties, which depend on increasing ply counter
const bool isdrop = crazyhouse && is_drop_move(i, j);
const bool known_move_type = (m == board::nullmove || isdrop);
const bool iscastling = !known_move_type && is_castling_move(i, j);
const bool is_enpassant_take = !known_move_type && is_enpassant_take_move(i, j);
const pos_t epawn = enpassant_pawn();
// back-up current state
state_hist.emplace_back(state);
assert(check_valid_move(m, false));
state.null_move_state = (m == board::nullmove);
++current_ply_;
if(m == board::nullmove) {
// do nothing
} else if(isdrop) {
// crazyhouse-specific move
assert(check_valid_drop_move(i, j));
const COLOR c = activePlayer();
const PIECE p = board::get_drop_as(i);
put_pos(j, Piece(p, c));
update_state_attacks_pos(j);
--n_subs[Piece::get_piece_index(p, c)];
state_hist_halfmoves.emplace_back(get_current_ply());
} else if(iscastling) {
// back up knights for later checksum, as none of the knights should move
const piece_bitboard_t knights = get_knight_bits();
const COLOR c = activePlayer();
const pos_t castlrank = (c == WHITE) ? 1 : 8;
// king moves k_i -> k_j
const pos_t k_i = i;
pos_t k_j = j;
// in chess 960, j is not where the king moves, but the rook it castles towards
// otherwise there can be ambiguity of whether the king wants to move or castle
if(chess960) {
if(j == board::_pos(qcastlrook[c], castlrank)) {
k_j = board::_pos(C, castlrank);
} else if(j == board::_pos(kcastlrook[c], castlrank)) {
k_j = board::_pos(G, castlrank);
} else {
abort();
}
}
// rook moves r_i -> r_j
const move_t rookmove = piece::get_king_castle_rook_move(c, i, k_j, qcastlrook[c], kcastlrook[c]);
const pos_t r_i = bitmask::first(rookmove),
r_j = bitmask::second(rookmove);
update_castlings(i, j);
{
// will k_i -> k_j overwrite rook?
// we update white and black bitboards, not piece-bitboards here
if(k_j != r_i) {
// what if k_i == k_j? this is why move is not necessarily quiet
piece::move_pos(bits[c], k_i, k_j);
} else {
piece::unset_pos(bits[c], k_i);
}
// if r_i == k_j, we've moved the king but have not moved the rook yet!
// at this moment, the rook bitboard also indicates k_j
pos_king[c] = k_j;
}
// this should work nonetheless: it takes a position at k_i,
// marks queen+knight reachable squares and updates their attacks
update_state_attacks_pos(k_i);
update_state_attacks_pos(k_j);
// notify a visitor of the change
_update_pos_change(i, k_j);
{
// again: is this some weird chess960 set-up where k_j == r_i?
// first, update color masks
if(k_j != r_i) {
// r_j might be the same as r_i, therefore this move is not necessarily quiet
piece::move_pos(bits[c], r_i, r_j);
} else {
piece::set_pos(bits[c], r_j);
}
// now, update the orthogonal pieces (Q|R) bitboard
piece::move_pos(bits_slid_orth, r_i, r_j);
}
// this should work fine: at this point the bitboards are valid
update_state_attacks_pos(r_i);
update_state_attacks_pos(r_j);
// notify a visitor of the change
_update_pos_change(r_i, r_j);
const COLOR ec = enemy_of(c);
// checksum between color-bitboards and piece-bitboards
assert((bits[c] | bits[ec]) == (bits_pawns | bits_slid_diag | bits_slid_orth | get_king_bits() | knights));
} else if(is_enpassant_take) {
const COLOR c = activePlayer();
const pos_t killwhere = epawn;
unset_pos(killwhere);
update_state_attacks_pos(killwhere);
{
piece::move_pos(bits[c], i, j);
piece::move_pos(bits_pawns, i, j);
}
update_state_attacks_pos(i);
update_state_attacks_pos(j);
_update_pos_change(i, j);
if(crazyhouse) {
++n_subs[Piece::get_piece_index(PAWN, c)];
}
state_hist_halfmoves.emplace_back(get_current_ply());
} else if(is_promotion_move(i, j)) {
const PIECE becomewhat = board::get_promotion_as(promote_as);
if(crazyhouse) {
// this is crazyhouse-specific: when promoted pawns are captured,
// in drop-value they are still pawns. bits_promoted_pawns keeps track
// of them for both sides
const PIECE p = self[j].value;
if(p != EMPTY) {
++n_subs[Piece::get_piece_index(p, activePlayer())];
}
bits_promoted_pawns |= piece::pos_mask(j);
}
update_castlings(i, j);
unset_pos(i);
put_pos(j, Piece(becomewhat, activePlayer()));
update_state_attacks_pos(i);
update_state_attacks_pos(j);
_update_pos_change(i, j);
state_hist_halfmoves.emplace_back(get_current_ply());
} else {
const COLOR c = activePlayer();
const bool is_capture = !self.empty_at_pos(j);
if(is_doublepush_move(i, j)) {
set_enpassant(piece::get_pawn_enpassant_trace(c, i, j));
}
if(is_capture || bits_pawns & piece::pos_mask(i)) {
if(crazyhouse && is_capture) {
const PIECE p = (bits_promoted_pawns & piece::pos_mask(j)) ? PAWN : self[j].value;
++n_subs[Piece::get_piece_index(p, c)];
}
state_hist_halfmoves.emplace_back(get_current_ply());
}
if(crazyhouse) {
piece::unset_pos(bits_promoted_pawns, j);
if(bits_promoted_pawns & piece::pos_mask(i)) {
piece::move_pos(bits_promoted_pawns, i, j);
}
}
update_castlings(i, j);
if(!is_capture) {
move_pos_quiet(i, j);
} else {
move_pos(i, j);
}
update_state_attacks_pos(i);
update_state_attacks_pos(j);
_update_pos_change(i, j);
}
// update active player (current ply was updated earler)
activePlayer_ = enemy_of(activePlayer());
// init_state_moves
update_state_info();
// repetition detection can now be updated too
if(state_hist_repetitions > self.get_current_ply()) {
update_state_repetitions();
}
clear_state_unfinalize();
init_state_checkline(activePlayer());
if(b_finalize)make_move_finalize();
}
INLINE void make_move_unfinalized(move_t m) {
make_move_unfinalized(bitmask::first(m), bitmask::second(m));
}
INLINE void make_move_finalize() {
if(!state.moves_initialized) {
init_state_moves();
}
}
// forward-pass, complete code
INLINE void make_move(pos_t i, pos_t j) {
make_move_unfinalized(i, j);
// post-processing: moves, current state info
make_move_finalize();
// notify the visitor that they can post-process now
}
INLINE void make_move(move_t m) {
make_move(bitmask::first(m), bitmask::second(m));
}
// backtrack, complete method
void retract_move() {
// this is more efficient than make_move-like code
// just fetch previous state and overwrite
if(state_hist.empty())return;
const board_info prev_info = state_hist.back().info;
bits = {prev_info.whites, prev_info.blacks};
bits_pawns = prev_info.pawn_bits();
bits_slid_diag = prev_info.diag_slid;
bits_slid_orth = prev_info.orth_slid;
pos_king = {prev_info.pos_king(WHITE), prev_info.pos_king(BLACK)};
activePlayer_ = enemy_of(activePlayer());
// restore substitutions and promoted pawns too in crazyhouse
if(crazyhouse) {
n_subs = prev_info.get_n_subs();
bits_promoted_pawns = prev_info.promoted_pawns;
}
--current_ply_;
// enpassants
while(!state_hist_enpassants.empty() && state_hist_enpassants.back().first > get_current_ply()) {
state_hist_enpassants.pop_back();
}
// castlings
for(pos_t cstl = 0; cstl < castlings.size(); ++cstl) {
if(castlings[cstl] > get_current_ply()) {
castlings[cstl] = board::nocastlings;
}
}
// halfmoves
if(state_hist_halfmoves.size() > 1) {
while(state_hist_halfmoves.back() > get_current_ply()) {
state_hist_halfmoves.pop_back();
}
}
// regular state variables
state = state_hist.back();
state_hist.pop_back();
// repetitions
if(self.get_current_ply() < state_hist_repetitions) {
state_hist_repetitions = INT16_MAX;
}
_restore_on_event();
}
// methods that produce scopes for making moves and series of moves
INLINE decltype(auto) move_scope(move_t m) {
return make_move_scope(self, m);
}
INLINE decltype(auto) move_unfinalized_scope(move_t m) {
return make_move_unfinalized_scope(self, m);
}
INLINE decltype(auto) mline_scope(move_t m, MoveLine &mline) {
return make_mline_scope(self, m, mline);
}
INLINE decltype(auto) mline_unfinalized_scope(move_t m, MoveLine &mline) {
return make_mline_unfinalized_scope(self, m, mline);
}
INLINE decltype(auto) recursive_move_scope() {
return make_recursive_move_scope(self);
}
INLINE decltype(auto) recursive_move_unfinalized_scope() {
return make_recursive_move_unfinalized_scope(self);
}
INLINE decltype(auto) recursive_mline_scope(MoveLine &mline) {
return make_recursive_mline_scope(self, mline);
}
template <typename LineT, typename F>
INLINE void foreach_early_stop(const LineT &mline, F &&func) {
for(const move_t m : mline) {
if(!func(m))break;
}
}
template <typename LineT, typename F>
INLINE void walk_unfinalized_early_stop(const LineT &mline, F &&func) {
walk_unfinalized_early_stop(mline, std::forward<F>(func), [](ply_index_t){});
}
template <typename LineT, typename F, typename FF>
INLINE void walk_unfinalized_early_stop(const LineT &mline, F &&func, FF &&endfunc) {
auto &&rec_mscope = self.recursive_move_unfinalized_scope();
foreach_early_stop(mline, [&](const move_t m) mutable -> bool {
return func(m, [&]() mutable -> void {
rec_mscope.scope(m);
});
});
endfunc((ply_index_t)rec_mscope.counter);
}
template <typename LineT, typename F>
INLINE void walk_early_stop(const LineT &mline, F &&func) {
walk_early_stop(mline, std::forward<F>(func), [](ply_index_t){});
}
template <typename LineT, typename F, typename FF>
INLINE void walk_early_stop(const LineT &mline, F &&func, FF &&endfunc) {
auto &&rec_mscope = self.recursive_move_scope();
foreach_early_stop(mline, [&](const move_t m) mutable -> bool {
return func(m, [&]() mutable -> void {
rec_mscope.scope(m);
});
});
endfunc((ply_index_t)rec_mscope.counter);
}
template <typename LineT, typename F>
INLINE void foreach(const LineT &mline, F &&func) {
for(const move_t m : mline) {
func(m);
}
}
template <typename LineT, typename F>
INLINE void walk_unfinalized(const LineT &mline, F &&func) {
walk_unfinalized(mline, std::forward<F>(func), [](ply_index_t){});
}
template <typename LineT, typename F, typename FF>
INLINE void walk_unfinalized(const LineT &mline, F &&func, FF &&endfunc) {
auto &&rec_mscope = self.recursive_move_unfinalized_scope();
foreach(mline, [&](const move_t m) mutable -> void {
func(m, [&]() mutable -> void {
rec_mscope.scope(m);
});
});
endfunc((ply_index_t)rec_mscope.counter);
}
template <typename LineT, typename F>
INLINE void walk(const LineT &mline, F &&func) {
walk(mline, std::forward<F>(func), [](ply_index_t){});
}
template <typename LineT, typename F, typename FF>
INLINE void walk(const LineT &mline, F &&func, FF &&endfunc) {
auto &&rec_mscope = self.recursive_move_scope();
foreach(mline, [&](const move_t m) mutable -> void {
func(m, [&]() mutable -> void {
rec_mscope.scope(m);
});
});
endfunc((ply_index_t)rec_mscope.counter);
}
template <typename LineT, typename FF>
INLINE void walk_unfinalized_end(const LineT &mline, FF &&endfunc) {
auto &&rec_mscope = self.recursive_move_scope();
foreach(mline, [&](const move_t m) mutable -> void {
rec_mscope.scope(m);
});
endfunc((ply_index_t)rec_mscope.counter);
}
template <typename LineT, typename FF>
INLINE void walk_end(const LineT &mline, FF &&endfunc) {
auto &&rec_mscope = self.recursive_move_unfinalized_scope();
foreach(mline, [&](const move_t m) mutable -> void {
rec_mscope.scope(m);
});
endfunc((ply_index_t)rec_mscope.counter);
}
template <typename LineT>
INLINE bool check_valid_sequence(const LineT &mline, bool strict=false) {
//make_move_finalize();
auto &&rec_mscope = recursive_move_scope();
for(const move_t m : mline) {
if(!state.moves_initialized) {
make_move_finalize();
const bool res = check_valid_move(m, false);
clear_state_unfinalize();
if(!res) {
return false;
}
} else if(!check_valid_move(m, false)) {
return false;
}
rec_mscope.scope(m);
}