-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgame.cpp
executable file
·1119 lines (996 loc) · 46.4 KB
/
game.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <iostream>
#include "game.h"
#include "global.h"
game::game()
{
}
game::game(board *b) {
position = b;
score = 0;
tz = new char[64];
dest_sqs = new char[64];
perft = 0;
perft_total = 0;
}
game::~game() {
delete [] tz;
delete [] dest_sqs;
}
int game::evaluate(){
if(DEBUG_MODE)
perft++;
int b_piece_cnt = 0;
int w_piece_cnt = 0;
char w_king_pos;
char b_king_pos;
int pos = 0;
short unsigned int w_bishop_count, w_knight_count;
short unsigned int b_bishop_count, b_knight_count;
short unsigned int bishop_pos, knight_pos, pawn_pos;
U64 white_king = position->pieceBB[position->white_king];
U64 black_king = position->pieceBB[position->black_king];
U64 white_bishops = position->pieceBB[position->white_bishops];
U64 black_bishops = position->pieceBB[position->black_bishops];
U64 white_knights = position->pieceBB[position->white_knights];
U64 black_knights = position->pieceBB[position->black_knights];
U64 white_rooks = position->pieceBB[position->white_rooks];
U64 black_rooks= position->pieceBB[position->black_rooks];
U64 white_pawns = position->pieceBB[position->white_pawns];
U64 black_pawns = position->pieceBB[position->black_pawns];
U64 white_queens = position->pieceBB[position->white_queens];
U64 black_queens = position->pieceBB[position->black_queens];
U64 white_pieces = position->pieceBB[position->white_pieces];
U64 black_pieces = position->pieceBB[position->black_pieces];
pos += 20000 * (popcount(white_king) - popcount(black_king));
pos += king_bonus_w[lsb_scan(white_king)] - king_bonus_b[lsb_scan(black_king)];
w_bishop_count = popcount(white_bishops);
b_bishop_count = popcount(black_bishops);
w_piece_cnt = popcount(white_pieces);
b_piece_cnt = popcount(black_pieces);
pos += 510 * (popcount(white_rooks) - popcount(black_rooks));
pos += 320 * (popcount(white_knights) - popcount(black_knights));
pos += 330 * (popcount(white_bishops) - popcount(black_bishops));
pos += 100 * (popcount(white_pawns) - popcount(black_pawns));
pos += 890 * (popcount(white_queens) - popcount(black_queens));
while(white_bishops) {
bishop_pos = lsb_scan(white_bishops);
pos += bishop_bonus_w[bishop_pos];
white_bishops ^= (1ULL << bishop_pos);
}
while(black_bishops) {
bishop_pos = lsb_scan(black_bishops);
pos -= bishop_bonus_b[bishop_pos];
black_bishops ^= (1ULL << bishop_pos);
}
while(white_knights) {
knight_pos = lsb_scan(white_knights);
pos += knight_bonus[knight_pos];
white_knights ^= (1ULL << knight_pos);
}
while(black_bishops) {
knight_pos = lsb_scan(black_knights);
pos -= knight_bonus[knight_pos];
black_knights ^= (1ULL << knight_pos);
}
while(white_pawns) {
pawn_pos = lsb_scan(white_pawns);
pos += pawn_bonus_w[pawn_pos];
white_pawns ^= (1ULL << pawn_pos);
}
while(white_pawns) {
pawn_pos = lsb_scan(black_pawns);
pos -= pawn_bonus_b[pawn_pos];
black_pawns ^= (1ULL << pawn_pos);
}
if((b_piece_cnt + w_piece_cnt) < 16) {
pos += king_endgame_bonus_w[lsb_scan(white_king)];
pos -= king_endgame_bonus_b[lsb_scan(black_king)];
}
else {
pos += king_bonus_b[lsb_scan(white_king)];
pos -= king_bonus_b[lsb_scan(black_king)];
}
if(w_bishop_count >= 2) {
pos += 15;
}
if(b_bishop_count >= 2) {
pos -= 15;
}
return pos;
}
int game::search_max(char depth, int start) {
if( depth == 0) return evaluate();
int max = -30000;
bool root = depth == DEPTH;
int total = position->move_t.size();
if(root) {
move_count = position->move_t.size();
}
if(total == start) {
return -3000;
}
for(int j = start; j < total; j++) {
if(make_move(j)) {
if(root) {
position->print_algebraic(position->game_t.size()-1);
std::cout << "\t\t";
}
score = search_min(depth - 1, total);
if(root) {
std::cout << perft << std::endl;
perft_total += perft;
perft = 0;
}
unmake_move();
position->clear_moves(total, position->move_t.size());
if(score > max) {
max = score;
if(root) engine_move = j;
}
}
}
return max;
}
int game::search_min(char depth, int start) {
if( depth == 0) return evaluate();
int min = 30000;
bool root = depth == DEPTH;
int total = position->move_t.size();
if(root) {
move_count = position->move_t.size();
}
for(int j=start; j < total; j++) {
if(make_move(j)) {
if(root) {
position->print_algebraic(position->game_t.size()-1);
std::cout << "\t\t";
}
score = search_max(depth - 1, total);
if(root) {
std::cout << perft << std::endl;
perft_total += perft;
perft = 0;
}
unmake_move();
position->clear_moves(total, position->move_t.size());
if(score < min) {
min = score;
if(root) engine_move = j;
}
}
}
return min;
}
int game::alpha_beta_max(int alpha, int beta, char depth, int start) {
bool no_moves = true;
if(depth == 0) return evaluate(); //even: light. odd: dark
bool root = depth == DEPTH;
int total = position->move_t.size();
if(root) {
move_count = position->move_t.size();
}
for(int j = start; j < total; j++) {
if(make_move(j)) {
no_moves = false;
score = alpha_beta_min(alpha, beta, depth - 1, total);
unmake_move();
position->clear_moves(total, position->move_t.size());
if(score >= beta)
return beta;
if(score > alpha) {
alpha = score;
if(root) engine_move = j;
}
}
}
if(no_moves) {
if(mate_check()) {
return (-25000 - depth);
}
}
return alpha;
}
int game::alpha_beta_min(int alpha, int beta, char depth, int start) {
bool no_moves = true;
if( depth == 0) return evaluate(); //even: dark. odd: light
bool root = depth == DEPTH;
int total = position->move_t.size();
if(root) {
move_count = position->move_t.size();
}
for(int j=start; j < total; j++) {
if(make_move(j)) {
no_moves = false;
score = alpha_beta_max(alpha, beta, depth - 1, total);
unmake_move();
position->clear_moves(total, position->move_t.size());
if(score <= alpha)
return alpha;
if(score < beta) {
beta = score;
if(root) engine_move = j;
}
}
}
if(no_moves) {
if(mate_check()) {
return (25000 + depth);
}
//return 1000 + depth;
}
return beta;
}
bool game::mate_check() {
int start = position->move_t.size();
position->switch_side();
generate_moves();
position->switch_side();
int total = position->move_t.size();
//std::cout << "# moves: " << position->move_t.size();
for(int i = start; i < total; i++) {
//std::cout << "i: " << i << std::endl;
//std::cout << position->king+position->side << " == ";
//std::cout << " " << static_cast<int>(position->charBoard[position->move_t[i].move[1]]) << std::endl;
if(position->charBoard[position->move_t[i].move[1]] == (position->king + position->side)) {
position->clear_moves(start, total);
//std::cout << "\n\t\tCheckmate\n";
//position->print_char_board();
return true;
}
}
position->clear_moves(start, total);
return false;
}
void game::generate_moves() {
if(DEBUG_MODE)
assert(position->integrity());
U64 valid_attack_set, temp;
char sigBit, sq;
//////////////////////////////////////
//BISHOP MOVES
/////////////////////////////////////
temp = position->pieceBB[position->bishops+position->side];
while(temp) {
sq = lsb_scan(temp);
temp ^= sqBB[sq];
valid_attack_set = northEastAttack(sq);
valid_attack_set |= northWestAttack(sq);
valid_attack_set |= southEastAttack(sq);
valid_attack_set |= southWestAttack(sq);
valid_attack_set &= (~position->pieceBB[position->pieces+position->side]);
if(valid_attack_set) {
dest_sqs = serialize(valid_attack_set);
push_moves(dest_sqs, sq, position->bishops+position->side);
}
}
//////////////////////////////////////
//ROOK MOVES
/////////////////////////////////////
char j=0;
temp = position->pieceBB[position->rooks+position->side];
while(temp) {
sq = lsb_scan(temp);
temp ^= sqBB[sq];
valid_attack_set = northAttack(sq);
valid_attack_set |= southAttack(sq);
valid_attack_set |= eastAttack(sq);
valid_attack_set |= westAttack(sq);
valid_attack_set &= (~position->pieceBB[position->pieces+position->side]);
///need to turn off castling for rook moves! ?????/////
while(valid_attack_set) {
sigBit = lsb_scan(valid_attack_set);
valid_attack_set ^= sqBB[sigBit];
push_single_move(sigBit, sq, position->rooks+position->side);
///note: the if statements below are necessary so that on the unmake move fxn
//the castle ability can be re established with confidence
if(position->can_castle[position->king+position->side] && (sq == 7 || sq == 63))
position->move_t.back().canCastle_off[position->king+position->side] = true;
if(position->can_castle[position->queens+position->side] && (sq == 0 || sq == 56))
position->move_t.back().canCastle_off[position->queens+position->side] = true;
}
}
//////////////////////////////////////
//QUEEN MOVES
/////////////////////////////////////
temp = position->pieceBB[position->queens+position->side];
while(temp) {
sq = lsb_scan(temp);
temp ^= sqBB[sq];
valid_attack_set = northEastAttack(sq);
valid_attack_set |= northWestAttack(sq);
valid_attack_set |= southEastAttack(sq);
valid_attack_set |= southWestAttack(sq);
valid_attack_set |= northAttack(sq);
valid_attack_set |= southAttack(sq);
valid_attack_set |= eastAttack(sq);
valid_attack_set |= westAttack(sq);
valid_attack_set &= (~position->pieceBB[position->pieces+position->side]);
dest_sqs = serialize(valid_attack_set);
push_moves(dest_sqs, sq, position->queens+position->side);
}
////////////////////////////////
//KNIGHT MOVES
////////////////////////////////
temp = position->pieceBB[position->knights+position->side];
while(temp) {
sq = lsb_scan(temp);
temp ^= sqBB[sq];
valid_attack_set = (knightAtt[sq] & (~position->pieceBB[position->pieces+position->side]));
dest_sqs = serialize(valid_attack_set);
push_moves(dest_sqs, sq, position->knights+position->side);
}
/////////////////////////////////////////////////////
//KING MOVES//
////////////////////////////////////////////////////
char king = position->king+position->side;
temp = position->pieceBB[king];
U64 b_bad_pawn = sqBB[11] | sqBB[13];
U64 w_bad_pawn = sqBB[51] | sqBB[53];
sq = lsb_scan(temp);
valid_attack_set = (kingAtt[sq] & (~position->pieceBB[position->pieces+position->side]));
bool kingside = !((sqBB[sq+1] | sqBB[sq+2]) & position->occupied_squares); //true if the castling squares are open.
bool queenside = !((sqBB[sq-1] | sqBB[sq-2] | sqBB[sq-3]) & position->occupied_squares); //same
if((position->side == LIGHT) && (b_bad_pawn & position->pieceBB[position->black_pawns]))//((position->charBoard[11] == (position->black_pawns)) | (position->charBoard[13] == position->black_pawns))) {
{kingside = false; queenside = false; }
if((position->side == DARK) && (w_bad_pawn & position->pieceBB[position->white_pawns]))//((position->charBoard[51] == (position->white_pawns)) | (position->charBoard[53] == position->white_pawns))){
{kingside = false; queenside = false;}
while(valid_attack_set) {
sigBit = lsb_scan(valid_attack_set);
valid_attack_set ^= sqBB[sigBit];
push_single_move(sigBit, sq, king);
////the castle code below can be condensed and made more efficient! Do it!
if(position->can_castle[king])
position->move_t.back().canCastle_off[king] = true;
if(position->can_castle[position->queens + position->side])
position->move_t.back().canCastle_off[position->queens+position->side] = true;
}
//castle kingside
if(position->can_castle[king] && kingside) {
push_single_move(sq+2, sq, king);
position->move_t.back().isCastle[king] = true;
position->move_t.back().canCastle_off[king] = true;
if(position->can_castle[position->queens+position->side])
position->move_t.back().canCastle_off[position->queens+position->side] = true;
}
//castle queenside
if(position->can_castle[position->queens+position->side] && queenside) {
push_single_move(sq-2, sq, king);
position->move_t.back().isCastle[position->queens+position->side] = true;
if(position->can_castle[king])
position->move_t.back().canCastle_off[king] = true;
position->move_t.back().canCastle_off[position->queens+position->side] = true;
}
//////////////////////////////////////////
//PAWN MOVES///
//////////////////////////////////////////
if(position->side==LIGHT) {
temp = position->pieceBB[position->white_pawns];
valid_attack_set = position->pieceBB[position->white_pawns] << 8; //single push
valid_attack_set &= (~position->pieceBB[position->white_pieces]);
valid_attack_set &= (~position->pieceBB[position->black_pieces]);
temp = valid_attack_set;
while(temp) {
sq = lsb_scan(temp);
temp ^= sqBB[sq];
push_single_move(sq, sq-8, position->white_pawns);
if(sq >= 56) {push_promotion(sq, -8); }
}
valid_attack_set = valid_attack_set << 8; //double push
valid_attack_set &= (~position->pieceBB[position->white_pieces]);
valid_attack_set &= (~position->pieceBB[position->black_pieces]);
valid_attack_set &= row4;
temp = valid_attack_set;
while(temp) {
sq = lsb_scan(temp);
temp ^= sqBB[sq];
push_single_move(sq, sq-16, position->white_pawns);
position->move_t.back().enPassant= sq-8;
}
valid_attack_set = position->pieceBB[position->white_pawns]; //attack right
valid_attack_set = valid_attack_set << 9;
valid_attack_set &= notAfile;
valid_attack_set &= (position->pieceBB[position->black_pieces] | sqBB[position->game_t.back().enPassant]); //position->enPassant[0]);
while(valid_attack_set) {
sq = lsb_scan(valid_attack_set);
valid_attack_set ^= sqBB[sq];
push_single_move(sq, sq-9, position->white_pawns);
if(sq == position->game_t.back().enPassant) {position->move_t.back().passant_capture = true;}
if(sq >= 56) {push_promotion(sq, -9);}
}
valid_attack_set = position->pieceBB[position->white_pawns]; //attack left
valid_attack_set = valid_attack_set << 7;
valid_attack_set &= notHfile;
valid_attack_set &= (position->pieceBB[position->black_pieces] | sqBB[position->game_t.back().enPassant]);
while(valid_attack_set) {
sq = lsb_scan(valid_attack_set);
valid_attack_set ^= sqBB[sq];
push_single_move(sq, sq-7, position->white_pawns);
if(sq == position->game_t.back().enPassant) {position->move_t.back().passant_capture = true;}
if(sq >= 56) {push_promotion(sq, -7);}
}
}
//DARK//
else {
temp = position->pieceBB[position->black_pawns];
valid_attack_set = position->pieceBB[position->black_pawns] >> 8; //single push
valid_attack_set &= (~position->pieceBB[position->black_pieces]);
valid_attack_set &= (~position->pieceBB[position->white_pieces]);
temp = valid_attack_set;
while(temp) {
sq = lsb_scan(temp);
temp ^= sqBB[sq];
push_single_move(sq, sq+8, position->black_pawns);
if(sq <= 7) {push_promotion(sq, 8);}
}
valid_attack_set = valid_attack_set >> 8; //double push
valid_attack_set &= (~position->pieceBB[position->black_pieces]);
valid_attack_set &= (~position->pieceBB[position->white_pieces]);
valid_attack_set &= row5;
temp = valid_attack_set;
while(temp) {
sq = lsb_scan(temp);
temp ^= sqBB[sq];
push_single_move(sq, sq+16, position->black_pawns);
position->move_t.back().enPassant = sq+8;
}
valid_attack_set = position->pieceBB[position->black_pawns]; //attack down and left
valid_attack_set = valid_attack_set >> 9;
valid_attack_set &= notHfile;
valid_attack_set &= (position->pieceBB[position->white_pieces] | sqBB[position->game_t.back().enPassant]);
while(valid_attack_set) {
sq = lsb_scan(valid_attack_set);
valid_attack_set ^= sqBB[sq];
push_single_move(sq, sq+9, position->black_pawns);
if(sq == position->game_t.back().enPassant) {position->move_t.back().passant_capture = true;}
if(sq <= 7) {push_promotion(sq , 9);}
}
valid_attack_set = position->pieceBB[position->black_pawns]; //attack down and right
valid_attack_set = valid_attack_set >> 7;
valid_attack_set &= notAfile;
valid_attack_set &= (position->pieceBB[position->white_pieces] | sqBB[position->game_t.back().enPassant]);
while(valid_attack_set) {
sq = lsb_scan(valid_attack_set);
valid_attack_set ^= sqBB[sq];
push_single_move(sq, sq+7, position->black_pawns);
if(sq == position->game_t.back().enPassant) {position->move_t.back().passant_capture = true;}
if(sq <= 7) {push_promotion(sq, 7);}
}
}
if(DEBUG_MODE)
assert(position->integrity());
}
U64 game::northEastAttack(char square) {
U64 temp = position->occupied_squares & northEast[square];
char sigBit=0;
if(temp){
sigBit = lsb_scan(temp);
return (northEast[square] ^ northEast[sigBit]);
}
else
return northEast[square];
}
U64 game::eastAttack(char square) {
U64 temp = position->occupied_squares & east[square];
char sigBit=0;
if(temp){
sigBit = lsb_scan(temp);
return (east[square] ^ east[sigBit]);
}
else
return east[square];
}
U64 game::southEastAttack(char square) {
U64 temp = position->occupied_squares & southEast[square];
char sigBit=0;
if(temp){
sigBit = msb_scan(temp);
return (southEast[square] ^ southEast[sigBit]);
}
else
return southEast[square];
}
U64 game::southAttack(char square) {
U64 temp = position->occupied_squares & south[square];
char sigBit=0;
if(temp){
sigBit = msb_scan(temp);
return (south[square] ^ south[sigBit]);
}
else
return south[square];
}
U64 game::southWestAttack(char square) {
U64 temp = position->occupied_squares & southWest[square];
char sigBit=0;
if(temp){
sigBit = msb_scan(temp);
return (southWest[square] ^ southWest[sigBit]);
}
else
return southWest[square];
}
U64 game::westAttack(char square) {
U64 temp = position->occupied_squares & west[square];
char sigBit=0;
if(temp){
sigBit = msb_scan(temp);
return (west[square] ^ west[sigBit]);
}
else
return west[square];
}
U64 game::northWestAttack(char square) {
U64 temp = position->occupied_squares & northWest[square];
char sigBit=0;
if(temp){
sigBit = lsb_scan(temp);
return (northWest[square] ^ northWest[sigBit]);
}
else
return northWest[square];
}
U64 game::northAttack(char square) {
U64 temp = position->occupied_squares & north[square];
char sigBit=0;
if(temp){
sigBit = lsb_scan(temp);
return (north[square] ^ north[sigBit]);
}
else
return north[square];
}
void game::push_promotion(char sq, int shift) {
if(DEBUG_MODE)
assert(position->integrity());
bool side = position->side;
char origin = sq + shift;
position->move_t.back().promotion = true;
position->move_t.back().promotion_piece = position->queens + side;
push_single_move(sq, origin, position->pawns + side);
position->move_t.back().promotion = true;
position->move_t.back().promotion_piece = position->knights + side;
push_single_move(sq, origin, position->pawns + side);
position->move_t.back().promotion = true;
position->move_t.back().promotion_piece = position->bishops + side;
push_single_move(sq, origin, position->pawns + side);
position->move_t.back().promotion = true;
position->move_t.back().promotion_piece = position->rooks + side;
if(DEBUG_MODE)
assert(position->integrity());
}
void game::push_moves(char *dest, char sq, char piece) {
char i=0;
while(dest[i] != NOT_MOVE) {
push_single_move(dest[i], sq, piece);
i++;
}
}
void game::push_single_move(char dest, char sq, char piece) {
if(DEBUG_MODE)
assert(position->integrity());
position->move1.move[0] = sq;
position->move1.move[1] = dest;
position->move1.side = position->side;
position->move1.piece = piece;
position->move1.capture = sqBB[dest] & position->pieceBB[position->pieces+(!position->side)];
if(position->move1.capture) {
position->move1.captured_piece = position->charBoard[dest];
}
position->move1.enPassant = NOT_MOVE;
position->move1.passant_capture = false;
position->move_t.push_back(position->move1);
if(position->move_t.back().capture) {
if(position->move_t.back().captured_piece == (position->rooks + (!position->move1.side))) {
if((position->move1.move[1] == 7) && (position->can_castle[position->white_king]))
position->move_t.back().canCastle_off[position->white_king] = true;
else if((position->move1.move[1] == 63) && (position->can_castle[position->black_king]))
position->move_t.back().canCastle_off[position->black_king] = true;
else if((position->move1.move[1] == 0) && (position->can_castle[position->white_queens]))
position->move_t.back().canCastle_off[position->white_queens] = true;
else if((position->move1.move[1] == 56) && (position->can_castle[position->black_queens]))
position->move_t.back().canCastle_off[position->black_queens] = true;
}
}
if(DEBUG_MODE)
assert(position->integrity());
}
int game::check_move(char x, char y) {
for(int i=0;i < position->move_t.size(); i++) {
if(x==position->move_t[i].move[0] && y==position->move_t[i].move[1])
return i;
}
return NOT_MOVE;
}
//////////////////////////////////////////////////////////////////
//name:unmake_move
//returns: none
//parameters: index of move to undo in move_t vector
//function:updates all board information, changes side to move.
///////////////////////////////////////////////////////////////////
void game::unmake_move() {
if(DEBUG_MODE)
assert(position->integrity());
position->makeMove = position->game_t.back(); //safe the indexed moved into the more convenient/expedient makeMove.
position->game_t.pop_back();
bool side = position->makeMove.side; //save the current side into the more convenient/readable side variable
bool otherSide = !side; //save opposite side into the more conv/readable otherSide var.
char piece = position->makeMove.piece; //save the piece in the current move object into the more conv/readable piece var
//fromToBB holds the location of the pieces origin and destination
if(!position->makeMove.capture && (position->makeMove.piece != (position->pawns + side))) {
position->fifty_move_rule--;
}
U64 fromToBB = sqBB[position->makeMove.move[0]] ^ sqBB[position->makeMove.move[1]];
position->pieceBB[piece] ^= fromToBB; //reinstate moving pieces BitBoard
position->pieceBB[position->pieces + side] ^= fromToBB; //reinstate moving sides pieces BitBoard
position->charBoard[position->makeMove.move[1]] = EMPTY; //reinstate charBoard destination back to empty
position->charBoard[position->makeMove.move[0]] = piece; //reinsate charBoard origin back to the moving piece
/*
for non captures
*/
if(!position->makeMove.capture){ //if this is NOT a capture
position->occupied_squares ^= fromToBB; //then last thing to do is update the occupied square BitBoard
}
/*
for captures
*/
else { //if this is a capture
U64 fromBB = sqBB[position->makeMove.move[0]]; //hold origin Bibboard
U64 toBB = sqBB[position->makeMove.move[1]]; //holds destinations BitBoard
position->pieceBB[position->makeMove.captured_piece] ^= toBB; //reinstate piece taken BitBoard with the destination
position->pieceBB[position->pieces + (otherSide)] ^= toBB; //reinstate the pieces bitboard for the opposing side with the destination
position->occupied_squares ^= fromBB; //reinstate occupied BitBoard with only the origin, bc the dest is still occupied
//reinstate charBoard, destination should now show the captured piece replaced.
position->charBoard[position->makeMove.move[1]] = position->makeMove.captured_piece;
}
/*
//note: en passant is not considered a capture, so non capture code above is used
*/
if(position->makeMove.passant_capture) { //if this move was an enPassant capture
position->occupied_squares ^= sqBB[position->game_t.back().move[1]]; //add pawn back to occupied squares BB
position->pieceBB[position->pawns + otherSide] ^= sqBB[position->game_t.back().move[1]]; //add attacked pawn back to appropriate pawns BitBoard
position->pieceBB[position->pieces + otherSide] ^= sqBB[position->game_t.back().move[1]]; //add attacked pawn back to appropriate pieces BitBoard
//add attacked pawn back to charBoard.
position->charBoard[position->game_t.back().move[1]] = position->pawns + otherSide;
}
if(position->makeMove.canCastle_off[position->king + side]) //if this move took away castling ability on the king side
position->can_castle[position->king+side] = true; //then reinstate it
if(position->makeMove.canCastle_off[position->queens + side]) //if this move took away castling ability on the queen side
position->can_castle[position->queens + side] = true; //then reinsate it
if(position->makeMove.canCastle_off[position->king + otherSide]) //if this move took away castling ability on the king side
position->can_castle[position->king + otherSide] = true; //then reinstate it
if(position->makeMove.canCastle_off[position->queens + otherSide]) //if this move took away castling ability on the queen side
position->can_castle[position->queens + otherSide] = true; //then reinsate it
/*
CASTLE KING SIDE
*/
if(position->makeMove.isCastle[position->king + side]) { //if this is a castle on the king side
//holds where the rook should move bitBoard with origin and dest
U64 rookFromToBB = sqBB[position->makeMove.move[1] + 1] ^ sqBB[position->makeMove.move[1] - 1];
position->pieceBB[position->pieces+side] ^= rookFromToBB; //reinstate this moves pieces BibBoard for the rook move
position->pieceBB[position->rooks+side] ^= rookFromToBB; //reinstate this moves rook BitBoard
position->occupied_squares ^= rookFromToBB; //reinstate the occupied BitBoard
//reinstate the charBoard corner to rook
position->charBoard[position->makeMove.move[1] + 1] = position->rooks + side;
position->charBoard[position->makeMove.move[1] - 1] = EMPTY; //reinstate the charBoard rook dest back to empty
position->has_castled[side] = false;
}
/*
CASTLE QUEEN SIDE
*/
if(position->makeMove.isCastle[position->queens + side]) { //if this is a castle on the queen side
//create a rook origin/dest BB
U64 rookFromToBB = sqBB[position->makeMove.move[1] - 2] | sqBB[position->makeMove.move[1] + 1];
position->pieceBB[position->pieces+side] ^= rookFromToBB; //reinstate pieces BB for the rook
position->pieceBB[position->rooks+side] ^= rookFromToBB; //reinstate rook BB
position->occupied_squares ^= rookFromToBB; //reinstate occupied BB
//reinstate corner to rook on charBoard
position->charBoard[position->makeMove.move[1] - 2] = position->rooks + side;
position->charBoard[position->makeMove.move[1] + 1] = EMPTY; //reinstate charBoard rook dest to empty
position->has_castled[side] = false;
}
/*
PAWN PROMOTION
*/ //if white pawn moved to back rank
if(position->makeMove.promotion) {
position->pieceBB[position->pawns + side] ^= sqBB[position->makeMove.move[1]]; //remove from pawn bit board
position->pieceBB[position->makeMove.promotion_piece] ^= sqBB[position->makeMove.move[1]]; //remove the indicated piece from it's bit board
//position->charBoard[position->makeMove.move[0]] = position->white_pawns; //change on charBoard
}
/*
if(position->makeMove.piece == position->black_pawns && position->makeMove.move[1] < 8) {
position->pieceBB[position->black_pawns] ^= sqBB[position->makeMove.move[1]]; //remove from pawn bit board
position->pieceBB[position->black_queens] ^= sqBB[position->makeMove.move[1]]; //remove queen from her bit board
position->charBoard[position->makeMove.move[0]] = position->black_pawns; //change on charBoard
}
*/
position->switch_side(); //side switched whenever unmake move is called.
if(DEBUG_MODE)
assert(position->integrity());
}
//////////////////////////////////////////////////////////////////
//name:make_move
//returns: boolean indicating success of move
//parameters: index of move in move_t vector
//function:updates all board information, changes side to move, returns true if successful. Checks if move leaves king in check or
//castles illegally through check. If so it undoes the move via unmake move(), and returns false.
//if successful leaves move_t vector with the nexts sides moves already generated, and the list of the side that just moved still in the vector,
//if unsuccesful leaves move_t vector with the same list of moves for the current side to reuse
/////and leaves the board info on the side that should select a different move.
///////////////////////////////////////////////////////////////////
bool game::make_move(const int i) {
if(DEBUG_MODE)
assert(position->integrity());
position->makeMove = position->move_t[i];
bool side = position->makeMove.side;
bool otherSide = !side;
//assert(position->integrity());
//king side castle but rooks not in position.
//do it here so as not to do all the make/unmake move business.
if(position->makeMove.isCastle[position->king + side] && (position->charBoard[position->makeMove.move[1] + 1] != (position->rooks + side) ) ) {
return false; }
//quuen side castle but rooks not in position.
//do it here so as not to do all the make/unmake move business.
if(position->makeMove.isCastle[position->queens + side] && (position->charBoard[position->makeMove.move[1] - 2] != (position->rooks + side) ) ) {
return false; }
if(!position->makeMove.capture && (position->makeMove.piece != (position->pawns + side))) {
position->fifty_move_rule++;
}
const int it = position->move_t.size();
position->switch_side();
//assert(position->integrity());
/*
all moves do the following
*/
U64 fromToBB = sqBB[position->makeMove.move[0]] ^ sqBB[position->makeMove.move[1]];
position->pieceBB[position->makeMove.piece] ^= fromToBB; //update moving piece BB
position->pieceBB[position->pieces + side] ^= fromToBB; //update moving side pieces BB
position->charBoard[position->makeMove.move[0]] = EMPTY; //update charBoard
position->charBoard[position->makeMove.move[1]] = position->makeMove.piece; //update charBoard
//assert(position->integrity());
/*
for non captures
*/
if(!position->makeMove.capture){
position->occupied_squares ^= fromToBB; //update occupied squares
}
/*
for captures
*/
else {
U64 fromBB = sqBB[position->makeMove.move[0]];
U64 toBB = sqBB[position->makeMove.move[1]];
position->pieceBB[position->makeMove.captured_piece] ^= toBB; //update piece taken BB BB
position->pieceBB[position->pieces + (otherSide)] ^= toBB; //update side attacked pieces BB
position->occupied_squares ^= fromBB; //update occupide squares BB
}
//assert(position->integrity());
/*
//note: en passant is not considered a capture, so non capture code above is used
*/
if(position->makeMove.passant_capture) {
position->occupied_squares ^= sqBB[position->game_t.back().move[1]]; //remove pawn from occupied squares BB
position->pieceBB[position->pawns + otherSide] ^= sqBB[position->game_t.back().move[1]]; //remove pawn from attacked piece BB
position->pieceBB[position->pieces + otherSide] ^= sqBB[position->game_t.back().move[1]]; //remove pawn from attacked side pieces BB
position->charBoard[position->game_t.back().move[1]] = EMPTY;
}
//assert(position->integrity());
/*
this move removes the following positions castling ability
*/
if(position->makeMove.canCastle_off[position->king + side])
position->can_castle[position->king+side] = false;
if(position->makeMove.canCastle_off[position->queens + side])
position->can_castle[position->queens + side] = false;
if(position->makeMove.canCastle_off[position->king + otherSide])
position->can_castle[position->king+otherSide] = false;
if(position->makeMove.canCastle_off[position->queens + otherSide])
position->can_castle[position->queens + otherSide] = false;
//assert(position->integrity());
/*
PAWN PROMOTION
*/
if(position->makeMove.promotion) {
position->pieceBB[position->pawns + side] ^= sqBB[position->makeMove.move[1]]; //remove from pawn bit board
position->pieceBB[position->makeMove.promotion_piece] ^= sqBB[position->makeMove.move[1]]; //add indicated piece to it's pieces bit board
position->charBoard[position->makeMove.move[1]] = position->makeMove.promotion_piece; //change on charBoard
}
//push move should be after passant code, because it uses the last move on game_t.
//but before the king legality code.
position->push_move(i);
/*
this move is a castle on the king side
*/
if(position->makeMove.isCastle[position->king + side]) {
U64 rookFromToBB = sqBB[position->makeMove.move[1] + 1] | sqBB[position->makeMove.move[1] - 1];
position->pieceBB[position->rooks+side] ^= rookFromToBB; //update rooks bitboard
position->occupied_squares ^= rookFromToBB; //update occupied sq bb
position->pieceBB[position->pieces+side] ^= rookFromToBB; //missing this line for a while really fucked things hah
position->charBoard[position->makeMove.move[1] + 1] = EMPTY; //update charboard rook
position->charBoard[position->makeMove.move[1] - 1] = position->rooks + side; //update charboard rook
position->has_castled[side] = true;
//assert(position->integrity());
generate_moves();
const int sizeNow = position->move_t.size();
/////////////////////////////
//this is the check for castling through/out of/into check.
/////////////////////////////
for(int j = it; j < sizeNow; j++) {
if(position->move_t[j].move[1] == position->move_t[i].move[1]) {
unmake_move();
position->clear_moves(it, position->move_t.size());
return false;
}
if(position->move_t[j].move[1] == position->move_t[i].move[1]-1) {
unmake_move();
position->clear_moves(it, position->move_t.size());
return false;
}
if(position->move_t[j].move[1] == position->move_t[i].move[1]-2) {
unmake_move();
position->clear_moves(it, position->move_t.size());
return false;
}
}
assert(position->integrity());
return true;
}
/*
this move is a castle on the queen side
*/
else if(position->makeMove.isCastle[position->queens + side]) {
U64 rookFromToBB = sqBB[position->makeMove.move[1] - 2] | sqBB[position->makeMove.move[1] + 1];
position->pieceBB[position->rooks+side] ^= rookFromToBB;
position->pieceBB[position->pieces+side] ^= rookFromToBB;
position->occupied_squares ^= rookFromToBB;
position->charBoard[position->makeMove.move[1] - 2] = EMPTY;
position->charBoard[position->makeMove.move[1] + 1] = position->rooks + side;
position->has_castled[side] = true;
//assert(position->integrity());
generate_moves();
int sizeNow = position->move_t.size();
//this is the check for castling through/out of/into check.
for(int j = it ; j < sizeNow; j++) {
if(position->move_t[j].move[1] == position->move_t[i].move[1]) {
unmake_move();
position->clear_moves(it, position->move_t.size());
//position->clear_move(i);
return false;
}
if(position->move_t[j].move[1] == position->move_t[i].move[1]+1) {
unmake_move();
position->clear_moves(it, position->move_t.size());
//position->clear_move(i);
return false;
}
if(position->move_t[j].move[1] == position->move_t[i].move[1]+2) {
unmake_move();
position->clear_moves(it, position->move_t.size());
//position->clear_move(i);
return false;
}
}
assert(position->integrity());
return true;
}
//assert(position->integrity());
generate_moves();
int sizeNow = position->move_t.size();
for(int j = it ; j < sizeNow; j++) {
if(position->charBoard[position->move_t[j].move[1]] == position->king + side) {
unmake_move();
position->clear_moves(it, position->move_t.size());
return false;
}
}
assert(position->integrity());
return true;
}
void game::print_move(const int i) {
bool side = position->side;
const char *pieces[] = {"king", "queen", "rook" , "knight" , "bishop" , "pawn"};
std::cout << "\n\n";
std::cout << "i: " << static_cast<int>(i) << std::endl;