-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBoard.cpp
More file actions
1371 lines (1065 loc) · 41.9 KB
/
Copy pathBoard.cpp
File metadata and controls
1371 lines (1065 loc) · 41.9 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 "board.h"
#include "piece_attack_table.h"
#include <iostream>
#include <iomanip>
#include <algorithm>
#include <chrono>
#include <cctype>
using namespace std;
using namespace std::chrono;
const std::vector<int> Board::MODEL_TOPOLOGY = { 12 * 64 + 64 + 4 + 1, 30, 30, 30, 1 };
char PieceToAscii(int piece)
{
switch (piece)
{
case WHITE_PAWN: return 'A';
case WHITE_KNIGHT: return 'N';
case WHITE_BISHOP: return 'B';
case WHITE_ROOK: return 'R';
case WHITE_QUEEN: return 'Q';
case WHITE_KING: return 'K';
case BLACK_PAWN: return 'v';
case BLACK_KNIGHT: return 'n';
case BLACK_BISHOP: return 'b';
case BLACK_ROOK: return 'r';
case BLACK_QUEEN: return 'q';
case BLACK_KING: return 'k';
default: return ' ';
}
}
char PieceToFen(int piece)
{
switch (piece)
{
case WHITE_PAWN: return 'P';
case WHITE_KNIGHT: return 'N';
case WHITE_BISHOP: return 'B';
case WHITE_ROOK: return 'R';
case WHITE_QUEEN: return 'Q';
case WHITE_KING: return 'K';
case BLACK_PAWN: return 'p';
case BLACK_KNIGHT: return 'n';
case BLACK_BISHOP: return 'b';
case BLACK_ROOK: return 'r';
case BLACK_QUEEN: return 'q';
case BLACK_KING: return 'k';
default: return ' ';
}
}
int FenToPiece(char FEN)
{
switch (FEN)
{
case 'P': return WHITE_PAWN;
case 'N': return WHITE_KNIGHT;
case 'B': return WHITE_BISHOP;
case 'R': return WHITE_ROOK;
case 'Q': return WHITE_QUEEN;
case 'K': return WHITE_KING;
case 'p': return BLACK_PAWN;
case 'n': return BLACK_KNIGHT;
case 'b': return BLACK_BISHOP;
case 'r': return BLACK_ROOK;
case 'q': return BLACK_QUEEN;
case 'k': return BLACK_KING;
default: return 420;
}
}
int FenToPieceType(char FEN)
{
switch (FEN)
{
case 'P':
case 'p':
return PAWN;
case 'N':
case 'n':
return KNIGHT;
case 'B':
case 'b':
return BISHOP;
case 'R':
case 'r':
return ROOK;
case 'Q':
case 'q':
return QUEEN;
case 'K':
case 'k':
return KING;
default:
return 0;
}
}
void Board::ParseFEN(const string& FEN)
{
this->boardstate.Clear();
this->boardstate_history.Clear();
this->visited_nodes = 0;
fill_n(&this->killer_heuristic[0][0], sizeof(this->killer_heuristic) / sizeof(this->killer_heuristic[0][0]), 0);
fill_n(&this->pv_length[0], sizeof(this->pv_length) / sizeof(this->pv_length[0]), 0);
fill_n(&this->pv_table[0][0], sizeof(this->pv_table) / sizeof(this->pv_table[0][0]), 0);
fill(this->repeated_position, this->repeated_position + REPEATED_POSITION_SIZE, false);
auto iter = FEN.begin();
for (int square = 0; square < 64;)
{
char letter = *iter++;
if (isdigit(letter)) square += letter - '0';
else if (isalpha(letter)) this->boardstate.bitboards[FenToPiece(letter)].SetBit(square++);
}
for (int piece = WHITE_PAWN; piece <= WHITE_KING; ++piece)
{
this->boardstate.occupancies[WHITE] |= this->boardstate.bitboards[piece];
}
for (int piece = BLACK_PAWN; piece <= BLACK_KING; ++piece)
{
this->boardstate.occupancies[BLACK] |= this->boardstate.bitboards[piece];
}
this->boardstate.occupancies[BOTH] = this->boardstate.occupancies[WHITE] | this->boardstate.occupancies[BLACK];
this->boardstate.side_to_move = (*++iter == 'w' ? WHITE : BLACK);
iter += 2;
while (true)
{
if (*iter == ' ') break;
switch (*iter)
{
case 'K': this->boardstate.castle |= WHITE_KING_SIDE_CASTLE; break;
case 'Q': this->boardstate.castle |= WHITE_QUEEN_SIDE_CASTLE; break;
case 'k': this->boardstate.castle |= BLACK_KING_SIDE_CASTLE; break;
case 'q': this->boardstate.castle |= BLACK_QUEEN_SIDE_CASTLE; break;
}
++iter;
}
++iter;
if (*iter != '-')
{
int file = ToFile(*iter);
++iter;
int rank = ToRank(*iter);
this->boardstate.enpassant_square = rank * 8 + file;
}
iter += 2;
//generate new position key
this->boardstate.GenerateNewKey();
}
bool Board::ParseMove(const string& move_str)
{
if (move_str.size() < 4) return false;
int source_file = ToFile(move_str[0]);
int source_rank = ToRank(move_str[1]);
int dest_file = ToFile(move_str[2]);
int dest_rank = ToRank(move_str[3]);
int promoted_piece_type = 0;
if (move_str.size() >= 5) promoted_piece_type = FenToPieceType(move_str[4]);
int source_square = source_rank * 8 + source_file;
int dest_square = dest_rank * 8 + dest_file;
vector<Move> pseudo_moves = GetPseudoMoves();
for (Move pseudo_move : pseudo_moves)
{
if (pseudo_move.GetSource() == source_square && pseudo_move.GetDest() == dest_square && pseudo_move.GetPromotedPieceType() == promoted_piece_type)
{
if (MakePseudoMove(pseudo_move)) return true;
else return false;
}
}
return false;
}
void Board::ParsePosition(const string& position_str)
{
size_t index = 9;
if (position_str.substr(index, 8) == "startpos")
{
index += 9;
ParseFEN("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
}
else if (position_str.substr(index, 8) == "kiwipete")
{
index += 9;
ParseFEN("r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQkq - 0 1");
}
else if (position_str.substr(index, 7) == "DTZ1033")
{
index += 8;
ParseFEN("8/r6n/8/8/5k2/3K4/7N/3b3Q b - - 0 0");
}
else if (position_str.substr(index, 7) == "endgame")
{
index += 8;
ParseFEN("8/P7/8/8/8/4k3/1K6/8 w - - 1 5");
}
else if (position_str.substr(index, 3) == "fen")
{
index += 4;
ParseFEN(position_str.substr(index));
}
if (index < position_str.size())
{
//4 bytes assembly level KMP
//index = strstr(position_str.c_str(), "move") - position_str.c_str();
index = position_str.find("moves");
if (index != string::npos)
{
index += 5;
while (index != string::npos)
{
++index;
bool parsed_success = ParseMove(position_str.substr(index));
if (parsed_success) index = position_str.find(" ", index);
else break;
}
}
}
}
void Board::ParsePerfTest(const string& perf_str)
{
size_t index = perf_str.find("depth") + 6;
if (index < string::npos)
{
this->visited_nodes = 0;
int max_depth = stoi(perf_str.substr(index));
high_resolution_clock::time_point timer = high_resolution_clock::now();
PerfTest(max_depth);
auto duration = duration_cast<milliseconds>(high_resolution_clock::now() - timer);
cout << "Node: " << this->visited_nodes << '\n';
cout << "Time: " << duration.count() << " ms\n";
cout << "Speed: " << this->visited_nodes / duration.count() << " knode/s"<<endl;
}
}
void Board::ParseGo(const string& go_str)
{
int max_depth = MAX_SEARCHING_DEPTH;
long long remaining_time_ms = Timer::DEFAULT_THINKING_TIME_MS;
long long increment_time_ms = Timer::DEFAULT_INCREMENT_TIME_MS;
size_t index = go_str.find("infinite");
if (index != string::npos)
{
max_depth = MAX_SEARCHING_DEPTH;
}
else if (go_str.find("depth") != string::npos)
{
max_depth = stoi(go_str.substr(9));
}
else if (go_str.find("movetime") != string::npos)
{
remaining_time_ms = stoi(go_str.substr(12));
increment_time_ms = remaining_time_ms;
}
else
{
if (this->boardstate.side_to_move == WHITE)
{
index = go_str.find("wtime");
if (index != string::npos) remaining_time_ms = stoi(go_str.substr(index + 6));
index = go_str.find("winc");
if (index != string::npos) increment_time_ms = stoi(go_str.substr(index + 5));
}
else
{
index = go_str.find("btime");
if (index != string::npos) remaining_time_ms = stoi(go_str.substr(index + 6));
index = go_str.find("binc");
if (index != string::npos) increment_time_ms = stoi(go_str.substr(index + 5));
}
}
this->timer.StartTimer(remaining_time_ms, increment_time_ms);
this->visited_nodes = 0;
fill_n(&this->killer_heuristic[0][0], sizeof(this->killer_heuristic) / sizeof(this->killer_heuristic[0][0]), 0);
fill_n(&this->pv_length[0], sizeof(this->pv_length) / sizeof(this->pv_length[0]), 0);
fill_n(&this->pv_table[0][0], sizeof(this->pv_table) / sizeof(this->pv_table[0][0]), 0);
for (int i = 0; i < TRANSPOSITION_TABLE_SIZE; ++i)
{
this->transposition_table[i].Clear();
}
Move best_move = 0;
for (int depth = 1; depth <= max_depth && depth < MAX_SEARCHING_DEPTH;)
{
int score = Search(depth, 0, -INT_MAX, INT_MAX);
if (timer.IsTimeOut()) break;
else
{
cout << "info score cp " << score << " depth " << depth << " nodes " << visited_nodes << " pv ";
for (int i = 0; i < this->pv_length[0]; ++i)
{
pv_table[0][i].PrintMove();
cout << ' ';
}
cout << endl;
best_move = pv_table[0][0];
++depth;
}
}
cout << "bestmove ";
best_move.PrintMove();
cout << endl;
if (best_move) MakePseudoMove(best_move);
}
void Board::ParseTrain(const std::string& train_str)
{
std::vector<Move> pseudo_moves;
int max_game = stoi(train_str.substr(6));
for (int game = 0; game < max_game; ++game)
{
std::cout << "Training game: " << game << '\n';
this->ParseFEN("rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1");
if (game % 2) this->neural_network_evaluation = true;
else this->neural_network_evaluation = false;
while (true)
{
bool in_check = IsKingAttacked();
bool has_legal_move = false;
pseudo_moves = GetPseudoMoves();
for (Move pseudo_move : pseudo_moves)
{
if (MakePseudoMove(pseudo_move))
{
RestoreState();
has_legal_move = true;
break;
}
}
//game over
if (!has_legal_move)
{
if (in_check) NeuralNetworkUpdate(this->boardstate.side_to_move ^ 1);
else NeuralNetworkUpdate(BOTH);
break;
}
else
{
int pawn_num = this->boardstate.bitboards[WHITE_PAWN].CountBit() + this->boardstate.bitboards[BLACK_PAWN].CountBit();
int minor_num = this->boardstate.bitboards[WHITE_KNIGHT].CountBit() + this->boardstate.bitboards[WHITE_BISHOP].CountBit() + this->boardstate.bitboards[BLACK_KNIGHT].CountBit() + this->boardstate.bitboards[BLACK_BISHOP].CountBit();
int major_num = this->boardstate.bitboards[WHITE_ROOK].CountBit() + this->boardstate.bitboards[WHITE_QUEEN].CountBit() + this->boardstate.bitboards[BLACK_ROOK].CountBit() + this->boardstate.bitboards[BLACK_QUEEN].CountBit();
if (!IsMaterialSufficient(pawn_num, minor_num, major_num))
{
NeuralNetworkUpdate(BOTH);
break;
}
}
this->ParseGo("go movetime " + to_string(MODEL_THINKING_TIME_MS_PER_MOVE));
this->PrintBoard();
this->neural_network_evaluation = !this->neural_network_evaluation;
}
}
this->neural_network_evaluation = false;
}
void Board::UCI()
{
ios_base::sync_with_stdio(false);
string command;
while (true)
{
getline(cin, command);
if (command == "uci")
{
cout << "id name KittyEngine\n";
cout << "id author UnboxTheCat\n";
cout << "uciok" << endl;
}
else if (command.find("isready") != string::npos) cout << "readyok" << endl;
else if (command.find("position") != string::npos) ParsePosition(command);
else if (command.find("ucinewgame") != string::npos) ParsePosition("position startpos");
else if (command.find("perft") != string::npos) ParsePerfTest(command);
else if (command.find("go") != string::npos) ParseGo(command);
else if (command.find("takeback") != string::npos) RestoreState();
else if (command.find("train") != string::npos) ParseTrain(command);
else if (command.find("quit") != string::npos) break;
else cout << "Invaild command\a\n";
PrintBoard();
}
}
bool Board::IsSquareAttacked(int square, int attack_side)
{
//pawn
int pawn = PIECE_LIST_TABLE[attack_side][PAWN];
if (GetPawnAttackExact(attack_side^1, square) & this->boardstate.bitboards[pawn]) return true;
int knight = PIECE_LIST_TABLE[attack_side][KNIGHT];
if (GetKnightAttackExact(square) & this->boardstate.bitboards[knight]) return true;
int bishop = PIECE_LIST_TABLE[attack_side][BISHOP];
if (GetBishopAttackExact(square, this->boardstate.occupancies[BOTH]) & this->boardstate.bitboards[bishop]) return true;
int rook = PIECE_LIST_TABLE[attack_side][ROOK];
if (GetRookAttackExact(square, this->boardstate.occupancies[BOTH]) & this->boardstate.bitboards[rook]) return true;
int queen = PIECE_LIST_TABLE[attack_side][QUEEN];
if (GetQueenAttackExact(square, this->boardstate.occupancies[BOTH]) & this->boardstate.bitboards[queen]) return true;
int king = PIECE_LIST_TABLE[attack_side][KING];
if (GetKingAttackExact(square) & this->boardstate.bitboards[king]) return true;
return false;
}
bool Board::IsKingAttacked()
{
int king_side = this->boardstate.side_to_move;
int king = PIECE_LIST_TABLE[king_side][KING];
int kign_square = this->boardstate.bitboards[king].GetLeastSigBit();
return IsSquareAttacked(kign_square, king_side^1);
}
int Board::GetCapturedPiece(Move move)
{
int moved_piece = move.GetMovedPiece();
int side = PIECE_SIDE_TABLE[moved_piece];
int dest_square = move.GetDest();
for (int captured_piece = PIECE_LIST_TABLE[!side][PAWN]; captured_piece <= PIECE_LIST_TABLE[!side][QUEEN]; ++captured_piece)
{
if (this->boardstate.bitboards[captured_piece].GetBit(dest_square)) return captured_piece;
}
return -1;
}
vector<Move> Board::GetPseudoMoves()
{
vector<Move> pseudo_moves;
pseudo_moves.reserve(218);
int side = this->boardstate.side_to_move;
int pawn = PIECE_LIST_TABLE[side][PAWN];
//reverse the occupancy, where bit 1 represents an empty space
Bitboard not_occupancy = ~this->boardstate.occupancies[BOTH];
Bitboard pawn_push = (side == WHITE ? this->boardstate.bitboards[pawn] >> 8 : this->boardstate.bitboards[pawn] << 8) & not_occupancy;
for (Bitboard cpy_pawn_push = pawn_push; cpy_pawn_push; cpy_pawn_push.PopBit())
{
int dest_square = cpy_pawn_push.GetLeastSigBit();
int source_square = (side == WHITE ? dest_square + 8 : dest_square - 8);
//no need to check for the side, because pawn can not move backward
if (GetRank(dest_square) == RANK_8 || GetRank(dest_square) == RANK_1)
{
for (int promoted_piece_type = KNIGHT; promoted_piece_type <= QUEEN; ++promoted_piece_type)
{
pseudo_moves.push_back(Move(source_square, dest_square, pawn, promoted_piece_type));
}
}
else
{
pseudo_moves.push_back(Move(source_square, dest_square, pawn));
}
}
Bitboard double_pawn_push = (side == WHITE ? (pawn_push & RANK_3_MASK) >> 8 : (pawn_push & RANK_6_MASK) << 8) & not_occupancy;
while (double_pawn_push)
{
int dest_square = double_pawn_push.GetLeastSigBit();
int source_square = (side == WHITE ? dest_square + 16 : dest_square - 16);
pseudo_moves.push_back(Move(source_square, dest_square, pawn, 0, Move::DOUBLE_PUSH_FLAG));
double_pawn_push.PopBit();
}
for (Bitboard pawn_bitboard = this->boardstate.bitboards[pawn]; pawn_bitboard; pawn_bitboard.PopBit())
{
int source_square = pawn_bitboard.GetLeastSigBit();
for (Bitboard pawn_attack = PAWN_ATTACK_TABLE[side][source_square] & this->boardstate.occupancies[!side]; pawn_attack; pawn_attack.PopBit())
{
int dest_square = pawn_attack.GetLeastSigBit();
if (GetRank(dest_square) == RANK_8 || GetRank(dest_square) == RANK_1)
{
for (int promoted_piece_type = KNIGHT; promoted_piece_type <= QUEEN; ++promoted_piece_type)
{
pseudo_moves.push_back(Move(source_square, dest_square, pawn, promoted_piece_type, Move::CAPTURE_FLAG));
}
}
else
{
pseudo_moves.push_back(Move(source_square, dest_square, pawn, 0, Move::CAPTURE_FLAG));
}
}
}
if (this->boardstate.enpassant_square != INVALID_SQUARE)
{
int dest_square = this->boardstate.enpassant_square;
//simulate an attack from the being captured side, the source must be a pawn
for (Bitboard pawn_attack = PAWN_ATTACK_TABLE[!side][dest_square] & this->boardstate.bitboards[pawn]; pawn_attack; pawn_attack.PopBit())
{
int source_square = pawn_attack.GetLeastSigBit();
//do i need capture flag?
pseudo_moves.push_back(Move(source_square, dest_square, pawn, 0, Move::ENPASSANT_FLAG));
}
}
int knight = PIECE_LIST_TABLE[side][KNIGHT];
for (Bitboard knight_bitboard = this->boardstate.bitboards[knight]; knight_bitboard; knight_bitboard.PopBit())
{
int source_square = knight_bitboard.GetLeastSigBit();
//knight attacks empty or enemy occupied squares
for (Bitboard knight_attack = GetKnightAttackExact(source_square) & ~this->boardstate.occupancies[side]; knight_attack; knight_attack.PopBit())
{
int dest_square = knight_attack.GetLeastSigBit();
if(this->boardstate.occupancies[!side].GetBit(dest_square)) pseudo_moves.push_back(Move(source_square, dest_square, knight, 0, Move::CAPTURE_FLAG));
else pseudo_moves.push_back(Move(source_square, dest_square, knight));
}
}
int king = PIECE_LIST_TABLE[side][KING];
int king_square = this->boardstate.bitboards[king].GetLeastSigBit();
for (Bitboard king_attack = GetKingAttackExact(king_square) & ~this->boardstate.occupancies[side]; king_attack; king_attack.PopBit())
{
int dest_square = king_attack.GetLeastSigBit();
if (this->boardstate.occupancies[!side].GetBit(dest_square)) pseudo_moves.push_back(Move(king_square, dest_square, king, 0, Move::CAPTURE_FLAG));
else pseudo_moves.push_back(Move(king_square, dest_square, king));
}
for (int castle_type = KING_SIDE; castle_type <= QUEEN_SIDE; ++castle_type)
{
//check for the permission
if (this->boardstate.castle & CASTLE_PERMISSION_REQUIREMENT_TABLE[side][castle_type])
{
//check the occupancy
if ((this->boardstate.occupancies[BOTH] & CASTLE_GET_OCCUPANCY_MASK_TABLE[side][castle_type]) == 0)
{
//king and the adjacent squares can not be in checked
int adj_square = (castle_type == KING_SIDE ? king_square + 1 : king_square - 1);
int dest_square = (castle_type == KING_SIDE ? king_square + 2 : king_square - 2);
if (!IsSquareAttacked(king_square, side^1) && !IsSquareAttacked(adj_square, side^1))
{
pseudo_moves.push_back(Move(king_square, dest_square, king, 0, Move::CASTLING_FLAG));
}
}
}
}
int bishop = PIECE_LIST_TABLE[side][BISHOP];
for (Bitboard bishop_bitboard = this->boardstate.bitboards[bishop]; bishop_bitboard; bishop_bitboard.PopBit())
{
int source_square = bishop_bitboard.GetLeastSigBit();
for (Bitboard bishop_attack = GetBishopAttackExact(source_square, this->boardstate.occupancies[BOTH]) & ~this->boardstate.occupancies[side]; bishop_attack; bishop_attack.PopBit())
{
int dest_square = bishop_attack.GetLeastSigBit();
if (this->boardstate.occupancies[!side].GetBit(dest_square)) pseudo_moves.push_back(Move(source_square, dest_square, bishop, 0, Move::CAPTURE_FLAG));
else pseudo_moves.push_back(Move(source_square, dest_square, bishop));
}
}
int rook = PIECE_LIST_TABLE[side][ROOK];
for (Bitboard rook_bitboard = this->boardstate.bitboards[rook]; rook_bitboard; rook_bitboard.PopBit())
{
int source_square = rook_bitboard.GetLeastSigBit();
//knight attacks empty or enemy occupied squares
for (Bitboard rook_attack = GetRookAttackExact(source_square, this->boardstate.occupancies[BOTH]) & ~this->boardstate.occupancies[side]; rook_attack; rook_attack.PopBit())
{
int dest_square = rook_attack.GetLeastSigBit();
if (this->boardstate.occupancies[!side].GetBit(dest_square)) pseudo_moves.push_back(Move(source_square, dest_square, rook, 0, Move::CAPTURE_FLAG));
else pseudo_moves.push_back(Move(source_square, dest_square, rook));
}
}
int queen = PIECE_LIST_TABLE[side][QUEEN];
for (Bitboard queen_bitboard = this->boardstate.bitboards[queen]; queen_bitboard; queen_bitboard.PopBit())
{
int source_square = queen_bitboard.GetLeastSigBit();
//knight attacks empty or enemy occupied squares
for (Bitboard queen_attack = GetQueenAttackExact(source_square, this->boardstate.occupancies[BOTH]) & ~this->boardstate.occupancies[side]; queen_attack; queen_attack.PopBit())
{
int dest_square = queen_attack.GetLeastSigBit();
if (this->boardstate.occupancies[!side].GetBit(dest_square)) pseudo_moves.push_back(Move(source_square, dest_square, queen, 0, Move::CAPTURE_FLAG));
else pseudo_moves.push_back(Move(source_square, dest_square, queen));
}
}
return pseudo_moves;
}
bool Board::MakePseudoMove(Move move)
{
SaveState();
int side = this->boardstate.side_to_move;
int source_square = move.GetSource();
int dest_square = move.GetDest();
int moving_piece = move.GetMovedPiece();
this->boardstate.bitboards[moving_piece].FlipBit(source_square);
this->boardstate.bitboards[moving_piece].FlipBit(dest_square);
this->boardstate.occupancies[side].FlipBit(source_square);
this->boardstate.occupancies[side].FlipBit(dest_square);
this->boardstate.HashPiece(moving_piece, source_square);
this->boardstate.HashPiece(moving_piece, dest_square);
if (move.IsCapture())
{
int captured_piece = GetCapturedPiece(move);
this->boardstate.bitboards[captured_piece].FlipBit(dest_square);
this->boardstate.occupancies[!side].FlipBit(dest_square);
this->boardstate.HashPiece(captured_piece, dest_square);
}
int promoted_piece_type = move.GetPromotedPieceType();
if (promoted_piece_type)
{
int promoted_piece = PIECE_LIST_TABLE[side][promoted_piece_type];
this->boardstate.bitboards[moving_piece].FlipBit(dest_square);
this->boardstate.bitboards[promoted_piece].FlipBit(dest_square);
this->boardstate.HashPiece(moving_piece, dest_square);
this->boardstate.HashPiece(promoted_piece, dest_square);
}
if (move.IsEnpassant())
{
int captured_pawn = PIECE_LIST_TABLE[!side][PAWN];
int captured_pawn_square = (side == WHITE ? dest_square + 8 : dest_square - 8);
this->boardstate.bitboards[captured_pawn].FlipBit(captured_pawn_square);
this->boardstate.occupancies[!side].FlipBit(captured_pawn_square);
this->boardstate.HashPiece(captured_pawn, captured_pawn_square);
}
//unhash the old enpassant square
if(this->boardstate.enpassant_square != INVALID_SQUARE) this->boardstate.HashEnpassant(this->boardstate.enpassant_square);
if (move.IsDoublePush())
{
this->boardstate.enpassant_square = (side == WHITE ? source_square - 8 : source_square + 8);
this->boardstate.HashEnpassant(this->boardstate.enpassant_square);
}
else this->boardstate.enpassant_square = INVALID_SQUARE;
if (move.IsCastling())
{
int rook = PIECE_LIST_TABLE[side][ROOK];
int rook_source = 0, rook_dest = 0;
switch (dest_square)
{
case G1: rook_source = H1; rook_dest = F1; break;
case C1: rook_source = A1; rook_dest = D1; break;
case G8: rook_source = H8; rook_dest = F8; break;
case C8: rook_source = A8; rook_dest = D8; break;
}
//flip bit is cheaper
this->boardstate.bitboards[rook].FlipBit(rook_source);
this->boardstate.bitboards[rook].FlipBit(rook_dest);
this->boardstate.occupancies[side].FlipBit(rook_source);
this->boardstate.occupancies[side].FlipBit(rook_dest);
this->boardstate.HashPiece(rook, rook_source);
this->boardstate.HashPiece(rook, rook_dest);
}
this->boardstate.HashCastle(this->boardstate.castle);
this->boardstate.castle &= CASTLING_PERMISSION_FILTER_TABLE[source_square];
this->boardstate.castle &= CASTLING_PERMISSION_FILTER_TABLE[dest_square];
this->boardstate.HashCastle(this->boardstate.castle);
//update the occupancies
this->boardstate.occupancies[BOTH] = this->boardstate.occupancies[WHITE] | this->boardstate.occupancies[BLACK];
//update fifty moves rule
if (moving_piece == WHITE_PAWN || moving_piece == BLACK_PAWN || move.IsCapture() || move.IsEnpassant()) this->boardstate.fifty_moves = 0;
else ++this->boardstate.fifty_moves;
//check for check
bool in_check = IsKingAttacked();
if (in_check)
{
RestoreState();
return false;
}
else
{
this->boardstate.side_to_move = this->boardstate.side_to_move^1;
this->boardstate.HashSideToMove();
return true;
}
}
void Board::MakeNullMove()
{
SaveState();
//unhash the old enpassant square
if (this->boardstate.enpassant_square != INVALID_SQUARE) this->boardstate.HashEnpassant(this->boardstate.enpassant_square);
this->boardstate.enpassant_square = INVALID_SQUARE;
//update the side to move hash
this->boardstate.HashSideToMove();
this->boardstate.side_to_move = this->boardstate.side_to_move^1;
}
//huge bottleneck
void Board::SaveState()
{
this->repeated_position[this->boardstate.position_key & (REPEATED_POSITION_SIZE - 1)] = true;
this->boardstate_history.PushBack(this->boardstate);
}
void Board::RestoreState()
{
this->boardstate = this->boardstate_history.Back();
this->boardstate_history.PopBack();
this->repeated_position[this->boardstate.position_key & (REPEATED_POSITION_SIZE - 1)] = false;
}
bool Board::IsMaterialSufficient(int pawn_num, int minor_num, int major_num)
{
return pawn_num || minor_num > 1 || major_num;
}
int Board::Evaluate()
{
int score = 0;
int minor_piece_num[3] = {};
int major_piece_num[3] = {};
const Bitboard* this_bitboards = this->boardstate.bitboards;
for (Bitboard bitboard = this_bitboards[WHITE_KNIGHT]; bitboard; bitboard.PopBit())
{
int square = bitboard.GetLeastSigBit();
score += PIECE_MATERIAL_VALUE_TABLE[WHITE_KNIGHT] + PIECE_POSITIONAL_VALUE_TABLE[WHITE_KNIGHT][square];
//mobility bonus
Bitboard attack_table = GetKnightAttackExact(square) & ~this->boardstate.occupancies[WHITE];
score += attack_table.CountBit() * PIECE_MOBILITY_BONUS_TABLE[WHITE_KNIGHT];
}
for (Bitboard bitboard = this_bitboards[WHITE_BISHOP]; bitboard; bitboard.PopBit())
{
int square = bitboard.GetLeastSigBit();
score += PIECE_MATERIAL_VALUE_TABLE[WHITE_BISHOP] + PIECE_POSITIONAL_VALUE_TABLE[WHITE_BISHOP][square];
//mobility bonus
Bitboard attack_table = GetBishopAttackExact(square, this->boardstate.occupancies[BOTH]) & ~this->boardstate.occupancies[WHITE];
score += attack_table.CountBit() * PIECE_MOBILITY_BONUS_TABLE[WHITE_BISHOP];
}
for (Bitboard bitboard = this_bitboards[WHITE_ROOK]; bitboard; bitboard.PopBit())
{
int square = bitboard.GetLeastSigBit();
score += PIECE_MATERIAL_VALUE_TABLE[WHITE_ROOK] + PIECE_POSITIONAL_VALUE_TABLE[WHITE_ROOK][square];
//mobility bonus
Bitboard attack_table = GetRookAttackExact(square, this->boardstate.occupancies[BOTH]) & ~this->boardstate.occupancies[WHITE];
score += attack_table.CountBit() * PIECE_MOBILITY_BONUS_TABLE[WHITE_ROOK];
//semi open / open file bonuses
if ((this_bitboards[WHITE_PAWN] & FILE_MASK_TABLE[square]) == 0)
{
if ((this_bitboards[BLACK_PAWN] & FILE_MASK_TABLE[square]) == 0) score += OPEN_FILE_BONUS;
else score += SEMI_OPEN_FILE_BONUS;
}
}
for (Bitboard bitboard = this_bitboards[WHITE_QUEEN]; bitboard; bitboard.PopBit())
{
int square = bitboard.GetLeastSigBit();
score += PIECE_MATERIAL_VALUE_TABLE[WHITE_QUEEN] + PIECE_POSITIONAL_VALUE_TABLE[WHITE_QUEEN][square];
//mobility bonus
Bitboard attack_table = GetQueenAttackExact(square, this->boardstate.occupancies[BOTH]) & ~this->boardstate.occupancies[WHITE];
score += attack_table.CountBit() * PIECE_MOBILITY_BONUS_TABLE[WHITE_QUEEN];
}
for (Bitboard bitboard = this_bitboards[BLACK_KNIGHT]; bitboard; bitboard.PopBit())
{
int square = bitboard.GetLeastSigBit();
score -= PIECE_MATERIAL_VALUE_TABLE[BLACK_KNIGHT] + PIECE_POSITIONAL_VALUE_TABLE[BLACK_KNIGHT][square];
//mobility bonus
Bitboard attack_table = GetKnightAttackExact(square) & ~this->boardstate.occupancies[BLACK];
score -= attack_table.CountBit() * PIECE_MOBILITY_BONUS_TABLE[BLACK_KNIGHT];
}
for (Bitboard bitboard = this_bitboards[BLACK_BISHOP]; bitboard; bitboard.PopBit())
{
int square = bitboard.GetLeastSigBit();
score -= PIECE_MATERIAL_VALUE_TABLE[BLACK_BISHOP] + PIECE_POSITIONAL_VALUE_TABLE[BLACK_BISHOP][square];
//mobility bonus
Bitboard attack_table = GetBishopAttackExact(square, this->boardstate.occupancies[BOTH]) & ~this->boardstate.occupancies[BLACK];
score -= attack_table.CountBit() * PIECE_MOBILITY_BONUS_TABLE[BLACK_BISHOP];
}
for (Bitboard bitboard = this_bitboards[BLACK_ROOK]; bitboard; bitboard.PopBit())
{
int square = bitboard.GetLeastSigBit();
score -= PIECE_MATERIAL_VALUE_TABLE[BLACK_ROOK] + PIECE_POSITIONAL_VALUE_TABLE[BLACK_ROOK][square];
//mobility bonus
Bitboard attack_table = GetRookAttackExact(square, this->boardstate.occupancies[BOTH]) & ~this->boardstate.occupancies[BLACK];
score -= attack_table.CountBit() * PIECE_MOBILITY_BONUS_TABLE[BLACK_ROOK];
//semi open / open file bonuses
if ((this_bitboards[BLACK_PAWN] & FILE_MASK_TABLE[square]) == 0)
{
if ((this_bitboards[WHITE_PAWN] & FILE_MASK_TABLE[square]) == 0) score -= OPEN_FILE_BONUS;
else score -= SEMI_OPEN_FILE_BONUS;
}
}
for (Bitboard bitboard = this_bitboards[BLACK_QUEEN]; bitboard; bitboard.PopBit())
{
int square = bitboard.GetLeastSigBit();
score -= PIECE_MATERIAL_VALUE_TABLE[BLACK_QUEEN] + PIECE_POSITIONAL_VALUE_TABLE[BLACK_QUEEN][square];
//mobility bonus
Bitboard attack_table = GetQueenAttackExact(square, this->boardstate.occupancies[BOTH]) & ~this->boardstate.occupancies[BLACK];
score -= attack_table.CountBit() * PIECE_MOBILITY_BONUS_TABLE[BLACK_QUEEN];
}
minor_piece_num[WHITE] = this_bitboards[WHITE_KNIGHT].CountBit() + this_bitboards[WHITE_BISHOP].CountBit();
major_piece_num[WHITE] = this_bitboards[WHITE_ROOK].CountBit() + this_bitboards[WHITE_QUEEN].CountBit();
minor_piece_num[BLACK] = this_bitboards[BLACK_KNIGHT].CountBit() + this_bitboards[BLACK_BISHOP].CountBit();
major_piece_num[BLACK] = this_bitboards[BLACK_ROOK].CountBit() + this_bitboards[BLACK_QUEEN].CountBit();
minor_piece_num[BOTH] = minor_piece_num[WHITE] + minor_piece_num[BLACK];
major_piece_num[BOTH] = major_piece_num[WHITE] + major_piece_num[BLACK];
bool is_end_game = minor_piece_num[this->boardstate.side_to_move ^ 1] + major_piece_num[this->boardstate.side_to_move ^ 1] <= 3;
for (Bitboard bitboard = this_bitboards[WHITE_PAWN]; bitboard; bitboard.PopBit())
{
int square = bitboard.GetLeastSigBit();
score += PIECE_MATERIAL_VALUE_TABLE[WHITE_PAWN] + PIECE_PAWN_POSITIONAL_VALUE_TABLE[WHITE][is_end_game][square];
//stacked pawns penalty
if (this_bitboards[WHITE_PAWN] & FILE_MASK_TABLE[square]) score -= STACKED_PAWN_PENALTY;
//isolated pawn penalty
if ((this_bitboards[WHITE_PAWN] & ISOLATED_PAWN_MASK_TABLE[square]) == 0) score -= ISOLATED_PAWN_PENALTY;
//passed pawn bonus
if ((this_bitboards[BLACK_PAWN] & PASSED_PAWN_MASK_TABLE[WHITE][square]) == 0) score += PASSED_PAWN_BONUS_TABLE[WHITE][GetRank(square)];
}
for (Bitboard bitboard = this_bitboards[BLACK_PAWN]; bitboard; bitboard.PopBit())
{
int square = bitboard.GetLeastSigBit();
score -= PIECE_MATERIAL_VALUE_TABLE[BLACK_PAWN] + PIECE_PAWN_POSITIONAL_VALUE_TABLE[BLACK][is_end_game][square];
//stacked pawns penalty
if (this_bitboards[BLACK_PAWN] & FILE_MASK_TABLE[square]) score += STACKED_PAWN_PENALTY;
//isolated pawn penalty
if ((this_bitboards[BLACK_PAWN] & ISOLATED_PAWN_MASK_TABLE[square]) == 0) score += ISOLATED_PAWN_PENALTY;
//passed pawn bonus
if ((this_bitboards[WHITE_PAWN] & PASSED_PAWN_MASK_TABLE[BLACK][square]) == 0) score -= PASSED_PAWN_BONUS_TABLE[BLACK][GetRank(square)];
}
//check material sufficiency
if (!IsMaterialSufficient(this_bitboards[WHITE_PAWN].CountBit() + this_bitboards[BLACK_PAWN].CountBit(), minor_piece_num[BOTH], major_piece_num[BOTH])) return 0;
//king early/endgame positional bonus
int white_king_square = this_bitboards[WHITE_KING].GetLeastSigBit();
score += PIECE_KING_POSITIONAL_VALUE_TABLE[WHITE][is_end_game][white_king_square];
int black_king_square = this_bitboards[BLACK_KING].GetLeastSigBit();
score -= PIECE_KING_POSITIONAL_VALUE_TABLE[BLACK][is_end_game][black_king_square];
//bishop pair compensation
if (this_bitboards[WHITE_BISHOP].CountBit() >= 2 && this_bitboards[BLACK_BISHOP].CountBit() == 0) score += BISHOP_PAIR_BONUS;
else if (this_bitboards[BLACK_BISHOP].CountBit() >= 2 && this_bitboards[WHITE_BISHOP].CountBit() == 0) score -= BISHOP_PAIR_BONUS;
return this->boardstate.side_to_move == WHITE ? score : -score;
}