-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
2342 lines (2117 loc) · 78.1 KB
/
Copy pathmain.cpp
File metadata and controls
2342 lines (2117 loc) · 78.1 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 <algorithm>
#include <array>
#include <bit>
#include <cmath>
#include <cctype>
#include <cstdint>
#include <cstdlib>
#include <iostream>
#include <limits>
#include <optional>
#include <sstream>
#include <string>
#include <unordered_map>
#include <utility>
#include <vector>
namespace {
constexpr double kMateScore = 10000.0;
constexpr double kInf = 1e18;
constexpr int kMaxDepth = 4;
constexpr std::array<double, 7> kPieceValues = {0.0, 1.0, 3.0, 3.5, 5.0, 10.0, 0.0};
enum PieceType {
EMPTY = 0,
PAWN = 1,
KNIGHT = 2,
BISHOP = 3,
ROOK = 4,
QUEEN = 5,
KING = 6,
};
enum CastlingRights {
WHITE_KINGSIDE = 1 << 0,
WHITE_QUEENSIDE = 1 << 1,
BLACK_KINGSIDE = 1 << 2,
BLACK_QUEENSIDE = 1 << 3,
};
struct Move {
int from = -1;
int to = -1;
int promotion = EMPTY;
bool is_en_passant = false;
bool is_castling = false;
};
struct UndoState {
int captured_piece = EMPTY;
int moved_piece = EMPTY;
int castling_rights = 0;
int ep_square = -1;
int halfmove_clock = 0;
int fullmove_number = 1;
bool white_to_move = true;
std::uint64_t hash_key = 0;
bool position_play_valid = false;
double position_play_cache = 0.0;
};
struct SearchResult {
double score = 0.0;
std::vector<Move> pv;
};
int piece_color(int piece) {
if (piece > 0) {
return 1;
}
if (piece < 0) {
return -1;
}
return 0;
}
int piece_type(int piece) {
return std::abs(piece);
}
bool is_white_piece(int piece) {
return piece > 0;
}
int make_piece(bool white, int type) {
return white ? type : -type;
}
int file_of(int square) {
return square % 8;
}
int rank_of(int square) {
return square / 8;
}
bool on_board(int square) {
return square >= 0 && square < 64;
}
std::string square_to_string(int square) {
std::string text = "a1";
text[0] = static_cast<char>('a' + file_of(square));
text[1] = static_cast<char>('1' + rank_of(square));
return text;
}
int square_from_string(const std::string &text) {
if (text.size() != 2) {
return -1;
}
int file = text[0] - 'a';
int rank = text[1] - '1';
if (file < 0 || file >= 8 || rank < 0 || rank >= 8) {
return -1;
}
return rank * 8 + file;
}
char promotion_char(int type) {
switch (type) {
case KNIGHT:
return 'n';
case BISHOP:
return 'b';
case ROOK:
return 'r';
case QUEEN:
return 'q';
default:
return '\0';
}
}
std::string move_to_uci(const Move &move) {
std::string text = square_to_string(move.from) + square_to_string(move.to);
if (move.promotion != EMPTY) {
text.push_back(promotion_char(move.promotion));
}
return text;
}
using Bitboard = std::uint64_t;
constexpr Bitboard bit_for(int square) {
return 1ULL << square;
}
int lsb_square(Bitboard bb) {
return static_cast<int>(std::countr_zero(bb));
}
int pop_lsb(Bitboard &bb) {
int square = lsb_square(bb);
bb &= bb - 1;
return square;
}
std::array<Bitboard, 64> g_knight_attacks{};
std::array<Bitboard, 64> g_king_attacks{};
std::array<std::array<Bitboard, 64>, 2> g_pawn_attacks{};
bool g_attack_tables_ready = false;
Bitboard rook_attacks(int square, Bitboard occ) {
Bitboard attacks = 0;
int file = file_of(square);
int rank = rank_of(square);
for (int nf = file + 1; nf < 8; ++nf) {
int to = rank * 8 + nf;
attacks |= bit_for(to);
if (occ & bit_for(to)) {
break;
}
}
for (int nf = file - 1; nf >= 0; --nf) {
int to = rank * 8 + nf;
attacks |= bit_for(to);
if (occ & bit_for(to)) {
break;
}
}
for (int nr = rank + 1; nr < 8; ++nr) {
int to = nr * 8 + file;
attacks |= bit_for(to);
if (occ & bit_for(to)) {
break;
}
}
for (int nr = rank - 1; nr >= 0; --nr) {
int to = nr * 8 + file;
attacks |= bit_for(to);
if (occ & bit_for(to)) {
break;
}
}
return attacks;
}
Bitboard bishop_attacks(int square, Bitboard occ) {
Bitboard attacks = 0;
int file = file_of(square);
int rank = rank_of(square);
for (int nf = file + 1, nr = rank + 1; nf < 8 && nr < 8; ++nf, ++nr) {
int to = nr * 8 + nf;
attacks |= bit_for(to);
if (occ & bit_for(to)) {
break;
}
}
for (int nf = file + 1, nr = rank - 1; nf < 8 && nr >= 0; ++nf, --nr) {
int to = nr * 8 + nf;
attacks |= bit_for(to);
if (occ & bit_for(to)) {
break;
}
}
for (int nf = file - 1, nr = rank + 1; nf >= 0 && nr < 8; --nf, ++nr) {
int to = nr * 8 + nf;
attacks |= bit_for(to);
if (occ & bit_for(to)) {
break;
}
}
for (int nf = file - 1, nr = rank - 1; nf >= 0 && nr >= 0; --nf, --nr) {
int to = nr * 8 + nf;
attacks |= bit_for(to);
if (occ & bit_for(to)) {
break;
}
}
return attacks;
}
void init_attack_tables_once() {
if (g_attack_tables_ready) {
return;
}
for (int square = 0; square < 64; ++square) {
int file = file_of(square);
int rank = rank_of(square);
static const int knight_offsets[8][2] = {
{1, 2}, {2, 1}, {2, -1}, {1, -2},
{-1, -2}, {-2, -1}, {-2, 1}, {-1, 2},
};
static const int king_offsets[8][2] = {
{1, 0}, {1, 1}, {0, 1}, {-1, 1},
{-1, 0}, {-1, -1}, {0, -1}, {1, -1},
};
Bitboard knight_mask = 0;
for (const auto &offset : knight_offsets) {
int nf = file + offset[0];
int nr = rank + offset[1];
if (nf >= 0 && nf < 8 && nr >= 0 && nr < 8) {
knight_mask |= bit_for(nr * 8 + nf);
}
}
g_knight_attacks[square] = knight_mask;
Bitboard king_mask = 0;
for (const auto &offset : king_offsets) {
int nf = file + offset[0];
int nr = rank + offset[1];
if (nf >= 0 && nf < 8 && nr >= 0 && nr < 8) {
king_mask |= bit_for(nr * 8 + nf);
}
}
g_king_attacks[square] = king_mask;
Bitboard white_pawns = 0;
Bitboard black_pawns = 0;
for (int df : {-1, 1}) {
int wf = file + df;
int wr = rank + 1;
if (wf >= 0 && wf < 8 && wr >= 0 && wr < 8) {
white_pawns |= bit_for(wr * 8 + wf);
}
int bf = file + df;
int br = rank - 1;
if (bf >= 0 && bf < 8 && br >= 0 && br < 8) {
black_pawns |= bit_for(br * 8 + bf);
}
}
g_pawn_attacks[0][square] = white_pawns;
g_pawn_attacks[1][square] = black_pawns;
}
g_attack_tables_ready = true;
}
class Board;
std::uint64_t modern_hash_position(const Board &board);
std::uint64_t modern_hash_after_move(const Board &board, const Move &move);
class Board {
public:
static constexpr int kPieceListCap = 32;
std::array<int, 64> squares{};
std::array<int, 64> piece_slot{};
std::array<std::array<std::array<int, kPieceListCap>, 7>, 2> piece_list{};
std::array<std::array<int, 7>, 2> piece_count{};
std::array<std::array<Bitboard, 7>, 2> piece_bb{};
std::array<Bitboard, 2> occ{};
Bitboard occ_all = 0;
std::array<int, 2> king_sq{{-1, -1}};
bool white_to_move = true;
int castling_rights = WHITE_KINGSIDE | WHITE_QUEENSIDE | BLACK_KINGSIDE | BLACK_QUEENSIDE;
int ep_square = -1;
int halfmove_clock = 0;
int fullmove_number = 1;
std::uint64_t hash_key = 0;
bool position_play_valid = false;
double position_play_cache = 0.0;
Board() {
set_startpos();
}
void set_startpos() {
set_fen("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
}
void set_fen(const std::string &fen) {
init_attack_tables_once();
squares.fill(EMPTY);
piece_slot.fill(-1);
clear_piece_lists();
std::istringstream stream(fen);
std::string board_part;
std::string stm_part;
std::string castling_part;
std::string ep_part;
stream >> board_part >> stm_part >> castling_part >> ep_part >> halfmove_clock >> fullmove_number;
int rank = 7;
int file = 0;
for (char c : board_part) {
if (c == '/') {
--rank;
file = 0;
continue;
}
if (std::isdigit(static_cast<unsigned char>(c))) {
file += c - '0';
continue;
}
int type = EMPTY;
switch (std::tolower(static_cast<unsigned char>(c))) {
case 'p':
type = PAWN;
break;
case 'n':
type = KNIGHT;
break;
case 'b':
type = BISHOP;
break;
case 'r':
type = ROOK;
break;
case 'q':
type = QUEEN;
break;
case 'k':
type = KING;
break;
}
bool white = std::isupper(static_cast<unsigned char>(c));
int sq = rank * 8 + file;
squares[sq] = make_piece(white, type);
add_piece_state(sq, squares[sq]);
++file;
}
white_to_move = (stm_part == "w");
castling_rights = 0;
if (castling_part.find('K') != std::string::npos) {
castling_rights |= WHITE_KINGSIDE;
}
if (castling_part.find('Q') != std::string::npos) {
castling_rights |= WHITE_QUEENSIDE;
}
if (castling_part.find('k') != std::string::npos) {
castling_rights |= BLACK_KINGSIDE;
}
if (castling_part.find('q') != std::string::npos) {
castling_rights |= BLACK_QUEENSIDE;
}
ep_square = (ep_part == "-") ? -1 : square_from_string(ep_part);
hash_key = modern_hash_position(*this);
position_play_valid = false;
}
std::string epd() const {
std::ostringstream out;
for (int rank = 7; rank >= 0; --rank) {
int empties = 0;
for (int file = 0; file < 8; ++file) {
int piece = squares[rank * 8 + file];
if (piece == EMPTY) {
++empties;
continue;
}
if (empties) {
out << empties;
empties = 0;
}
char c = ' ';
switch (piece_type(piece)) {
case PAWN:
c = 'p';
break;
case KNIGHT:
c = 'n';
break;
case BISHOP:
c = 'b';
break;
case ROOK:
c = 'r';
break;
case QUEEN:
c = 'q';
break;
case KING:
c = 'k';
break;
default:
break;
}
if (is_white_piece(piece)) {
c = static_cast<char>(std::toupper(static_cast<unsigned char>(c)));
}
out << c;
}
if (empties) {
out << empties;
}
if (rank > 0) {
out << '/';
}
}
out << (white_to_move ? " w " : " b ");
std::string castling = "-";
if (castling_rights) {
castling.clear();
if (castling_rights & WHITE_KINGSIDE) {
castling.push_back('K');
}
if (castling_rights & WHITE_QUEENSIDE) {
castling.push_back('Q');
}
if (castling_rights & BLACK_KINGSIDE) {
castling.push_back('k');
}
if (castling_rights & BLACK_QUEENSIDE) {
castling.push_back('q');
}
}
out << castling << ' ';
out << (ep_square == -1 ? "-" : square_to_string(ep_square));
return out.str();
}
int king_square(bool white) const {
return king_sq[color_index(white)];
}
bool is_square_attacked(int square, bool by_white) const {
init_attack_tables_once();
int color = color_index(by_white);
if (g_pawn_attacks[by_white ? 1 : 0][square] & piece_bb[color][PAWN]) {
return true;
}
if (g_knight_attacks[square] & piece_bb[color][KNIGHT]) {
return true;
}
if (g_king_attacks[square] & piece_bb[color][KING]) {
return true;
}
if (bishop_attacks(square, occ_all) & (piece_bb[color][BISHOP] | piece_bb[color][QUEEN])) {
return true;
}
if (rook_attacks(square, occ_all) & (piece_bb[color][ROOK] | piece_bb[color][QUEEN])) {
return true;
}
return false;
}
bool in_check(bool white) const {
int king = king_square(white);
return king != -1 && is_square_attacked(king, !white);
}
void generate_legal_moves(std::vector<Move> &moves) {
std::vector<Move> pseudo;
generate_pseudo_moves(pseudo);
moves.clear();
bool side = white_to_move;
for (const Move &move : pseudo) {
UndoState undo;
make_move(move, undo);
if (!in_check(side)) {
moves.push_back(move);
}
undo_move(move, undo);
}
}
void generate_tactical_legal_moves(std::vector<Move> &out, bool include_quiet_checks = true) {
out.clear();
bool side = white_to_move;
std::vector<Move> pseudo;
generate_pseudo_moves(pseudo, !include_quiet_checks && !in_check(side));
if (in_check(side)) {
for (const Move &move : pseudo) {
UndoState undo;
make_move(move, undo);
if (!in_check(side)) {
out.push_back(move);
}
undo_move(move, undo);
}
return;
}
for (const Move &move : pseudo) {
bool capture = is_capture(move);
if (!capture) {
if (!include_quiet_checks) {
continue;
}
UndoState undo;
make_move(move, undo);
bool legal = !in_check(side);
bool gives_chk = legal && in_check(!side);
undo_move(move, undo);
if (gives_chk) {
out.push_back(move);
}
continue;
}
UndoState undo;
make_move(move, undo);
if (!in_check(side)) {
out.push_back(move);
}
undo_move(move, undo);
}
}
bool is_capture(const Move &move) const {
if (move.is_en_passant) {
return true;
}
return squares[move.to] != EMPTY;
}
bool gives_check(const Move &move) {
UndoState undo;
bool mover = white_to_move;
make_move(move, undo);
bool check = in_check(!mover);
undo_move(move, undo);
return check;
}
bool is_checkmate() {
bool side = white_to_move;
if (!in_check(side)) {
return false;
}
std::vector<Move> moves;
generate_legal_moves(moves);
return moves.empty();
}
bool is_stalemate() {
bool side = white_to_move;
if (in_check(side)) {
return false;
}
std::vector<Move> moves;
generate_legal_moves(moves);
return moves.empty();
}
void make_move(const Move &move, UndoState &undo) {
undo.captured_piece = move.is_en_passant ? squares[white_to_move ? move.to - 8 : move.to + 8] : squares[move.to];
undo.moved_piece = squares[move.from];
undo.castling_rights = castling_rights;
undo.ep_square = ep_square;
undo.halfmove_clock = halfmove_clock;
undo.fullmove_number = fullmove_number;
undo.white_to_move = white_to_move;
undo.hash_key = hash_key;
undo.position_play_valid = position_play_valid;
undo.position_play_cache = position_play_cache;
hash_key = modern_hash_after_move(*this, move);
int piece = squares[move.from];
remove_piece_state(move.from, piece);
squares[move.from] = EMPTY;
if (move.is_en_passant) {
int captured_square = white_to_move ? move.to - 8 : move.to + 8;
remove_piece_state(captured_square, squares[captured_square]);
squares[captured_square] = EMPTY;
} else if (undo.captured_piece != EMPTY) {
remove_piece_state(move.to, undo.captured_piece);
}
if (move.is_castling) {
if (move.to == square_from_string("g1")) {
move_piece_state(square_from_string("h1"), square_from_string("f1"), squares[square_from_string("h1")]);
squares[square_from_string("f1")] = squares[square_from_string("h1")];
squares[square_from_string("h1")] = EMPTY;
} else if (move.to == square_from_string("c1")) {
move_piece_state(square_from_string("a1"), square_from_string("d1"), squares[square_from_string("a1")]);
squares[square_from_string("d1")] = squares[square_from_string("a1")];
squares[square_from_string("a1")] = EMPTY;
} else if (move.to == square_from_string("g8")) {
move_piece_state(square_from_string("h8"), square_from_string("f8"), squares[square_from_string("h8")]);
squares[square_from_string("f8")] = squares[square_from_string("h8")];
squares[square_from_string("h8")] = EMPTY;
} else if (move.to == square_from_string("c8")) {
move_piece_state(square_from_string("a8"), square_from_string("d8"), squares[square_from_string("a8")]);
squares[square_from_string("d8")] = squares[square_from_string("a8")];
squares[square_from_string("a8")] = EMPTY;
}
}
if (move.promotion != EMPTY) {
piece = make_piece(white_to_move, move.promotion);
}
squares[move.to] = piece;
add_piece_state(move.to, piece);
ep_square = -1;
if (piece_type(piece) == PAWN && std::abs(rank_of(move.to) - rank_of(move.from)) == 2) {
ep_square = white_to_move ? move.from + 8 : move.from - 8;
}
update_castling_rights(move, piece, undo.captured_piece);
if (piece_type(piece) == PAWN || undo.captured_piece != EMPTY) {
halfmove_clock = 0;
} else {
++halfmove_clock;
}
if (!white_to_move) {
++fullmove_number;
}
white_to_move = !white_to_move;
position_play_valid = false;
}
void undo_move(const Move &move, const UndoState &undo) {
white_to_move = undo.white_to_move;
castling_rights = undo.castling_rights;
ep_square = undo.ep_square;
halfmove_clock = undo.halfmove_clock;
fullmove_number = undo.fullmove_number;
remove_piece_state(move.to, squares[move.to]);
if (move.is_castling) {
if (move.to == square_from_string("g1")) {
move_piece_state(square_from_string("f1"), square_from_string("h1"), squares[square_from_string("f1")]);
squares[square_from_string("h1")] = squares[square_from_string("f1")];
squares[square_from_string("f1")] = EMPTY;
} else if (move.to == square_from_string("c1")) {
move_piece_state(square_from_string("d1"), square_from_string("a1"), squares[square_from_string("d1")]);
squares[square_from_string("a1")] = squares[square_from_string("d1")];
squares[square_from_string("d1")] = EMPTY;
} else if (move.to == square_from_string("g8")) {
move_piece_state(square_from_string("f8"), square_from_string("h8"), squares[square_from_string("f8")]);
squares[square_from_string("h8")] = squares[square_from_string("f8")];
squares[square_from_string("f8")] = EMPTY;
} else if (move.to == square_from_string("c8")) {
move_piece_state(square_from_string("d8"), square_from_string("a8"), squares[square_from_string("d8")]);
squares[square_from_string("a8")] = squares[square_from_string("d8")];
squares[square_from_string("d8")] = EMPTY;
}
}
squares[move.from] = undo.moved_piece;
add_piece_state(move.from, undo.moved_piece);
squares[move.to] = move.is_en_passant ? EMPTY : undo.captured_piece;
if (move.is_en_passant) {
int captured_square = white_to_move ? move.to - 8 : move.to + 8;
squares[captured_square] = undo.captured_piece;
add_piece_state(captured_square, undo.captured_piece);
} else if (undo.captured_piece != EMPTY) {
add_piece_state(move.to, undo.captured_piece);
}
hash_key = undo.hash_key;
position_play_valid = undo.position_play_valid;
position_play_cache = undo.position_play_cache;
}
std::optional<Move> parse_uci_move(const std::string &text) {
std::vector<Move> moves;
generate_legal_moves(moves);
for (const Move &move : moves) {
if (move_to_uci(move) == text) {
return move;
}
}
return std::nullopt;
}
public:
static int color_index(bool white) {
return white ? 0 : 1;
}
void clear_piece_lists() {
for (auto &side : piece_count) {
side.fill(0);
}
for (auto &side : piece_bb) {
side.fill(0);
}
occ.fill(0);
occ_all = 0;
for (auto &side : piece_list) {
for (auto &kind : side) {
kind.fill(-1);
}
}
piece_slot.fill(-1);
king_sq = {-1, -1};
}
void rebuild_piece_lists() {
clear_piece_lists();
for (int square = 0; square < 64; ++square) {
int piece = squares[square];
if (piece == EMPTY) {
continue;
}
int color = color_index(is_white_piece(piece));
int type = piece_type(piece);
int slot = piece_count[color][type]++;
if (slot >= kPieceListCap) {
piece_count[color][type] = kPieceListCap;
continue;
}
piece_list[color][type][slot] = square;
piece_bb[color][type] |= bit_for(square);
occ[color] |= bit_for(square);
occ_all |= bit_for(square);
piece_slot[square] = slot;
if (type == KING) {
king_sq[color] = square;
}
}
}
void add_piece_state(int square, int piece) {
if (piece == EMPTY) {
return;
}
int color = color_index(is_white_piece(piece));
int type = piece_type(piece);
if (piece_count[color][type] >= kPieceListCap) {
rebuild_piece_lists();
}
int slot = piece_count[color][type]++;
if (slot >= kPieceListCap) {
return;
}
piece_list[color][type][slot] = square;
piece_bb[color][type] |= bit_for(square);
occ[color] |= bit_for(square);
occ_all |= bit_for(square);
piece_slot[square] = slot;
if (type == KING) {
king_sq[color] = square;
}
}
void remove_piece_state(int square, int piece) {
if (piece == EMPTY) {
return;
}
int color = color_index(is_white_piece(piece));
int type = piece_type(piece);
int slot = piece_slot[square];
if (slot < 0 || slot >= piece_count[color][type]) {
rebuild_piece_lists();
slot = piece_slot[square];
if (slot < 0 || slot >= piece_count[color][type]) {
return;
}
}
int last_idx = --piece_count[color][type];
int last_sq = piece_list[color][type][last_idx];
piece_list[color][type][slot] = last_sq;
piece_slot[last_sq] = slot;
piece_slot[square] = -1;
piece_bb[color][type] &= ~bit_for(square);
occ[color] &= ~bit_for(square);
occ_all &= ~bit_for(square);
if (type == KING) {
king_sq[color] = -1;
}
}
void move_piece_state(int from, int to, int piece) {
if (piece == EMPTY) {
return;
}
int color = color_index(is_white_piece(piece));
int type = piece_type(piece);
int slot = piece_slot[from];
if (slot < 0 || slot >= piece_count[color][type]) {
rebuild_piece_lists();
slot = piece_slot[from];
if (slot < 0 || slot >= piece_count[color][type]) {
return;
}
}
piece_list[color][type][slot] = to;
piece_bb[color][type] &= ~bit_for(from);
piece_bb[color][type] |= bit_for(to);
occ[color] &= ~bit_for(from);
occ[color] |= bit_for(to);
occ_all &= ~bit_for(from);
occ_all |= bit_for(to);
piece_slot[to] = slot;
piece_slot[from] = -1;
if (type == KING) {
king_sq[color] = to;
}
}
private:
void update_castling_rights(const Move &move, int moved_piece, int captured_piece) {
auto clear_if_on = [&](int square) {
if (square == square_from_string("a1")) {
castling_rights &= ~WHITE_QUEENSIDE;
} else if (square == square_from_string("h1")) {
castling_rights &= ~WHITE_KINGSIDE;
} else if (square == square_from_string("a8")) {
castling_rights &= ~BLACK_QUEENSIDE;
} else if (square == square_from_string("h8")) {
castling_rights &= ~BLACK_KINGSIDE;
}
};
if (piece_type(moved_piece) == KING) {
if (is_white_piece(moved_piece)) {
castling_rights &= ~(WHITE_KINGSIDE | WHITE_QUEENSIDE);
} else {
castling_rights &= ~(BLACK_KINGSIDE | BLACK_QUEENSIDE);
}
}
if (piece_type(moved_piece) == ROOK) {
clear_if_on(move.from);
}
if (captured_piece != EMPTY) {
clear_if_on(move.to);
}
}
void generate_pseudo_moves(std::vector<Move> &moves, bool captures_only = false) {
moves.clear();
int color = color_index(white_to_move);
for (int idx = 0; idx < piece_count[color][PAWN]; ++idx) {
generate_pawn_moves(piece_list[color][PAWN][idx], moves, captures_only);
}
for (int idx = 0; idx < piece_count[color][KNIGHT]; ++idx) {
generate_leaper_moves(piece_list[color][KNIGHT][idx], moves, KNIGHT, captures_only);
}
for (int idx = 0; idx < piece_count[color][BISHOP]; ++idx) {
generate_slider_moves(piece_list[color][BISHOP][idx], moves, true, false, captures_only);
}
for (int idx = 0; idx < piece_count[color][ROOK]; ++idx) {
generate_slider_moves(piece_list[color][ROOK][idx], moves, false, true, captures_only);
}
for (int idx = 0; idx < piece_count[color][QUEEN]; ++idx) {
generate_slider_moves(piece_list[color][QUEEN][idx], moves, true, true, captures_only);
}
for (int idx = 0; idx < piece_count[color][KING]; ++idx) {
int square = piece_list[color][KING][idx];
generate_leaper_moves(square, moves, KING, captures_only);
if (!captures_only) {
generate_castling_moves(square, moves);
}
}
}
void add_promotion_moves(int from, int to, bool is_capture, std::vector<Move> &moves) {
(void)is_capture;
for (int promo : {QUEEN, ROOK, BISHOP, KNIGHT}) {
Move move{from, to, promo, false, false};
moves.push_back(move);
}
}
void generate_pawn_moves(int square, std::vector<Move> &moves, bool captures_only = false) {
bool white = white_to_move;
int dir = white ? 8 : -8;
int start_rank = white ? 1 : 6;
int promotion_rank = white ? 6 : 1;
int rank = rank_of(square);
int own = color_index(white);
int enemy = own ^ 1;
if (!captures_only) {
int one = square + dir;
if (on_board(one) && squares[one] == EMPTY) {
if (rank == promotion_rank) {
add_promotion_moves(square, one, false, moves);
} else {
moves.push_back(Move{square, one, EMPTY, false, false});
int two = square + 2 * dir;
if (rank == start_rank && squares[two] == EMPTY) {
moves.push_back(Move{square, two, EMPTY, false, false});
}
}
}
}
Bitboard captures = g_pawn_attacks[own][square] & occ[enemy];
while (captures) {
int to = pop_lsb(captures);
if (rank == promotion_rank) {
add_promotion_moves(square, to, true, moves);
} else {
moves.push_back(Move{square, to, EMPTY, false, false});
}
}
if (ep_square != -1 && (g_pawn_attacks[own][square] & bit_for(ep_square))) {
moves.push_back(Move{square, ep_square, EMPTY, true, false});
}
}
void generate_leaper_moves(int square, std::vector<Move> &moves, int type, bool captures_only = false) {
int own = color_index(white_to_move);
int enemy = own ^ 1;
Bitboard targets = (type == KNIGHT ? g_knight_attacks[square] : g_king_attacks[square]) & ~occ[own];
if (captures_only) {
targets &= occ[enemy];
}
while (targets) {
int to = pop_lsb(targets);
moves.push_back(Move{square, to, EMPTY, false, false});
}
}
void generate_slider_moves(int square, std::vector<Move> &moves, bool diagonal, bool orthogonal, bool captures_only = false) {
int own = color_index(white_to_move);
int enemy = own ^ 1;
Bitboard attacks = 0;
if (diagonal) {
attacks |= bishop_attacks(square, occ_all);
}
if (orthogonal) {
attacks |= rook_attacks(square, occ_all);
}
attacks &= ~occ[own];
if (captures_only) {
attacks &= occ[enemy];
}
while (attacks) {
int to = pop_lsb(attacks);
moves.push_back(Move{square, to, EMPTY, false, false});
}
}
void generate_castling_moves(int square, std::vector<Move> &moves) {
bool white = white_to_move;
if (white) {
if ((castling_rights & WHITE_KINGSIDE) &&
squares[square_from_string("f1")] == EMPTY &&
squares[square_from_string("g1")] == EMPTY &&
!is_square_attacked(square_from_string("e1"), false) &&
!is_square_attacked(square_from_string("f1"), false) &&
!is_square_attacked(square_from_string("g1"), false)) {
moves.push_back(Move{square, square_from_string("g1"), EMPTY, false, true});
}
if ((castling_rights & WHITE_QUEENSIDE) &&
squares[square_from_string("d1")] == EMPTY &&
squares[square_from_string("c1")] == EMPTY &&
squares[square_from_string("b1")] == EMPTY &&