-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprawn.c
More file actions
3432 lines (2931 loc) · 107 KB
/
prawn.c
File metadata and controls
3432 lines (2931 loc) · 107 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 "common.h"
static char buffer[1024];
static board_t board;
static board_ext_t board_ext;
#define NO_SCORE -536870912
#define CHECK_MATE -536870911
#define DRAW 536870911
#define MAX(A,B) ((A) > (B) ? (A) : (B))
#define MIN(A,B) ((A) < (B) ? (A) : (B))
static uint64_t white_pawn_capture_masks[64];
static uint64_t black_pawn_capture_masks[64];
static uint64_t white_en_passant_capture_masks[8];
static uint64_t black_en_passant_capture_masks[8];
static uint64_t knight_moves_masks[64];
static uint64_t king_moves_masks[64];
static int64_t zobrist_map[64][12];
static int64_t zobrist_depth[MAX_SEARCH_DEPTH + 1];
static int64_t zobrist_en_passant[8];
static int64_t zobrist_castling[4];
static hash_table_entry_t * hash_table;
static unsigned int opening_book_size;
static uint64_t opening_book[MAX_SUPPORTED_OB_RULES];
static char ob_play_colors[MAX_SUPPORTED_OB_RULES];
static play_short_t ob_plays[MAX_SUPPORTED_OB_RULES][4];
static int opening_book_enabled = 1;
static int extend_uci = 0;
static int uci_game_in_error_state = 0;
static int convert_at_ob_depth = -1;
static void reset_board();
static void actual_play(board_t * board, board_ext_t * board_ext, const play_t * play);
static int64_t hash_from_board(const board_t * board);
static int enumerate_legal_plays(play_t * valid_plays, const board_t * board);
static void init_opening_book() {
opening_book_size = 0;
play_t play;
play.promotion_option = 0;
play_t valid_plays[218];
int valid_plays_i;
int play_is_legal;
int file_row = 0;
play.promotion_option = 0;
FILE * fp = fopen("openings.txt", "r");
while (opening_book_size < MAX_SUPPORTED_OB_RULES) {
file_row += 1;
char * r = fgets(buffer, 4 * 1024, fp);
if (r == NULL) {
break;
}
char * s = buffer;
s[strlen(s) - 1] = 0;
if (s[0] == 0 || s[0] == '#') {
continue;
}
reset_board();
int plays_found = -1;
int ob_depth = 1;
char * token;
while ((token = strsep(&s, " ")) != NULL && plays_found < 4) {
if (token[0] == '|') {
plays_found = 0;
continue;
}
char color = board.color;
play.from_x = token[0] - 'a';
play.from_y = '8' - token[1];
play.to_x = token[2] - 'a';
play.to_y = '8' - token[3];
valid_plays_i = enumerate_legal_plays(valid_plays, &board);
play_is_legal = 0;
for (int i = 0; i < valid_plays_i; ++i) {
if (valid_plays[i].from_x == play.from_x && valid_plays[i].from_y == play.from_y && valid_plays[i].to_x == play.to_x && valid_plays[i].to_y == play.to_y && valid_plays[i].promotion_option == 0) {
play_is_legal = 1;
break;
}
}
if (!play_is_legal) {
fprintf(stderr, "Error reading openings book - invalid play @ line %d\n", file_row);
exit(EXIT_FAILURE);
}
if (plays_found == -1) {
actual_play(&board, &board_ext, &play);
ob_depth++;
continue;
}
ob_plays[opening_book_size][plays_found].from_x = play.from_x;
ob_plays[opening_book_size][plays_found].from_y = play.from_y;
ob_plays[opening_book_size][plays_found].to_x = play.to_x;
ob_plays[opening_book_size][plays_found].to_y = play.to_y;
ob_play_colors[opening_book_size] = color;
plays_found++;
}
if (plays_found != 4) {
fprintf(stderr, "Error loading opening book\n");
exit(EXIT_FAILURE);
}
if (ob_depth == convert_at_ob_depth) {
char fen_buf[100];
board_t board_cpy;
board_ext_t board_ext_cpy;
for (int i = 0; i < 4; ++i) {
memcpy(&board_cpy, &board, sizeof(board_t));
memcpy(&board_ext_cpy, &board_ext, sizeof(board_ext_t));
play.from_x = ob_plays[opening_book_size][i].from_x;
play.from_y = ob_plays[opening_book_size][i].from_y;
play.to_x = ob_plays[opening_book_size][i].to_x;
play.to_y = ob_plays[opening_book_size][i].to_y;
actual_play(&board_cpy, &board_ext_cpy, &play);
board_to_fen(fen_buf, &board_cpy, &board_ext_cpy);
printf("%s\n", fen_buf);
}
}
opening_book[opening_book_size] = hash_from_board(&board);
opening_book_size++;
}
if (opening_book_size == MAX_SUPPORTED_OB_RULES) {
fprintf(stderr, "Maximum number of opening books reached\n");
}
fclose(fp);
if (convert_at_ob_depth != -1) {
exit(EXIT_SUCCESS);
}
}
static int get_opening_book_play(play_t * play, uint64_t board_hash) {
for (unsigned int i = 0; i < opening_book_size; ++i) {
if (board_hash == opening_book[i] && ob_play_colors[i] == board.color) {
int play_picked = rand() % 4;
play->from_x = ob_plays[i][play_picked].from_x;
play->from_y = ob_plays[i][play_picked].from_y;
play->to_x = ob_plays[i][play_picked].to_x;
play->to_y = ob_plays[i][play_picked].to_y;
play->promotion_option = 0;
return 1;
}
}
return 0;
}
static void populate_pawn_capture_masks() {
int x2, y2;
for (int p = 0; p < 64; ++p) {
white_pawn_capture_masks[p] = 0ULL;
black_pawn_capture_masks[p] = 0ULL;
int x = p % 8;
int y = p / 8;
x2 = x - 1;
y2 = y - 1;
if (x2 >= 0 && x2 < 8 && y2 >= 0 && y2 < 8) {
white_pawn_capture_masks[p] |= (1ULL << (y2 * 8 + x2));
}
x2 = x + 1;
y2 = y - 1;
if (x2 >= 0 && x2 < 8 && y2 >= 0 && y2 < 8) {
white_pawn_capture_masks[p] |= (1ULL << (y2 * 8 + x2));
}
x2 = x - 1;
y2 = y + 1;
if (x2 >= 0 && x2 < 8 && y2 >= 0 && y2 < 8) {
black_pawn_capture_masks[p] |= (1ULL << (y2 * 8 + x2));
}
x2 = x + 1;
y2 = y + 1;
if (x2 >= 0 && x2 < 8 && y2 >= 0 && y2 < 8) {
black_pawn_capture_masks[p] |= (1ULL << (y2 * 8 + x2));
}
}
for (int x = 0; x < 8; ++x) {
white_en_passant_capture_masks[x] = 0ULL;
black_en_passant_capture_masks[x] = 0ULL;
int y = 2;
x2 = x - 1;
y2 = y + 1;
if (x2 >= 0 && x2 < 8 && y2 >= 0 && y2 < 8) {
white_en_passant_capture_masks[x] |= (1ULL << (y2 * 8 + x2));
}
x2 = x + 1;
y2 = y + 1;
if (x2 >= 0 && x2 < 8 && y2 >= 0 && y2 < 8) {
white_en_passant_capture_masks[x] |= (1ULL << (y2 * 8 + x2));
}
y = 5;
x2 = x - 1;
y2 = y - 1;
if (x2 >= 0 && x2 < 8 && y2 >= 0 && y2 < 8) {
black_en_passant_capture_masks[x] |= (1ULL << (y2 * 8 + x2));
}
x2 = x + 1;
y2 = y - 1;
if (x2 >= 0 && x2 < 8 && y2 >= 0 && y2 < 8) {
black_en_passant_capture_masks[x] |= (1ULL << (y2 * 8 + x2));
}
}
}
static void populate_knight_moves_masks() {
const int offsets[8][2] = {
{-1, -2}, {+1, -2}, {-2, -1}, {+2, -1},
{-2, +1}, {+2, +1}, {-1, +2}, {+1, +2}
};
for (int p = 0; p < 64; ++p) {
int x = p % 8, y = p / 8;
knight_moves_masks[p] = 0ULL;
for (int i = 0; i < 8; i++) {
int x2 = x + offsets[i][0];
int y2 = y + offsets[i][1];
if (x2 >= 0 && x2 < 8 && y2 >= 0 && y2 < 8) {
knight_moves_masks[p] |= (1ULL << (y2 * 8 + x2));
}
}
}
}
static void populate_king_moves_masks() {
const int offsets[8][2] = {
{-1, -1}, {0, -1}, {+1, -1},
{-1, 0}, {+1, 0},
{-1, +1}, {0, +1}, {+1, +1}
};
for (int p = 0; p < 64; ++p) {
int x = p % 8, y = p / 8;
king_moves_masks[p] = 0ULL;
for (int i = 0; i < 8; i++) {
int x2 = x + offsets[i][0];
int y2 = y + offsets[i][1];
if (x2 >= 0 && x2 < 8 && y2 >= 0 && y2 < 8) {
king_moves_masks[p] |= (1ULL << (y2 * 8 + x2));
}
}
}
}
// We tell if the entry is filled in by the 2 lowest bits of the score_w_type present, that must be zero
static int hash_table_find(int * score_w_type, int64_t hash) {
int start_key = hash & (HASH_TABLE_SIZE - 1);
int i = 0;
while (i < 5) {
int key = (start_key + i) & (HASH_TABLE_SIZE - 1);
if (hash_table[key].hash == hash) {
*score_w_type = hash_table[key].score_w_type;
return 1;
}
++i;
}
return 0;
}
static void hash_table_insert(int64_t hash, int score_w_type) {
int start_key = hash & (HASH_TABLE_SIZE - 1);
int i = 0;
while (i < 5) {
int key = (start_key + i) & (HASH_TABLE_SIZE - 1);
if ((hash_table[key].score_w_type & 3) == 0) {
hash_table[key].hash = hash;
hash_table[key].score_w_type = score_w_type;
return;
}
++i;
}
}
static void populate_zobrist_masks() {
int nItems = 64 * 12 + MAX_SEARCH_DEPTH + 1 + 4;
sprintf(buffer, "zobrist_%d.bin", (nItems & 1) ? nItems + 1 : nItems);
FILE * s = fopen(buffer, "rb");
if (s == NULL) {
fprintf(stderr, "Zobrist file with %d entries (depth %d) not found.\n", nItems, MAX_SEARCH_DEPTH);
exit(EXIT_FAILURE);
}
int64_t * zobrist_file = malloc(sizeof(int64_t) * nItems);
fread(zobrist_file, sizeof(int64_t), nItems, s);
fclose(s);
int item = 0;
for (int p = 0; p < 64; ++p) {
for (int t = 0; t < 12; ++t) {
zobrist_map[p][t] = zobrist_file[item++];
}
}
for (int t = 0; t < MAX_SEARCH_DEPTH + 1; ++t) {
zobrist_depth[t] = zobrist_file[item++];
}
for (int t = 0; t < 4; ++t) {
zobrist_castling[t] = zobrist_file[item++];
}
free(zobrist_file);
}
static int64_t update_hash_with_piece_white(int64_t hash, int pos, char piece) {
switch (piece) {
case 'P': return hash ^ (zobrist_map[pos][0]);
case 'R': return hash ^ (zobrist_map[pos][2]);
case 'N': return hash ^ (zobrist_map[pos][4]);
case 'B': return hash ^ (zobrist_map[pos][6]);
case 'K': return hash ^ (zobrist_map[pos][10]);
default: return hash ^ (zobrist_map[pos][8]);
}
}
static int64_t update_hash_with_piece_black(int64_t hash, int pos, char piece) {
switch (piece) {
case 'p': return hash ^ (zobrist_map[pos][1]);
case 'r': return hash ^ (zobrist_map[pos][3]);
case 'n': return hash ^ (zobrist_map[pos][5]);
case 'b': return hash ^ (zobrist_map[pos][7]);
case 'k': return hash ^ (zobrist_map[pos][11]);
default: return hash ^ (zobrist_map[pos][9]);
}
}
static int64_t update_hash_with_piece(int64_t hash, int pos, char piece) {
switch (piece) {
case 'P': return hash ^ (zobrist_map[pos][0]);
case 'p': return hash ^ (zobrist_map[pos][1]);
case 'R': return hash ^ (zobrist_map[pos][2]);
case 'r': return hash ^ (zobrist_map[pos][3]);
case 'N': return hash ^ (zobrist_map[pos][4]);
case 'n': return hash ^ (zobrist_map[pos][5]);
case 'B': return hash ^ (zobrist_map[pos][6]);
case 'b': return hash ^ (zobrist_map[pos][7]);
case 'Q': return hash ^ (zobrist_map[pos][8]);
case 'q': return hash ^ (zobrist_map[pos][9]);
case 'K': return hash ^ (zobrist_map[pos][10]);
default: return hash ^ (zobrist_map[pos][11]);
}
}
static char identify_piece(const board_t * board, int p) {
uint64_t mask = (1ULL << p);
if (board->white_pawns & mask) return 'P';
if (board->black_pawns & mask) return 'p';
if (board->white_knights & mask) return 'N';
if (board->black_knights & mask) return 'n';
if (board->white_bishops & mask) return 'B';
if (board->black_bishops & mask) return 'b';
if (board->white_rooks & mask) return 'R';
if (board->black_rooks & mask) return 'r';
if (board->white_queens & mask) return 'Q';
if (board->black_queens & mask) return 'q';
if (board->white_kings & mask) return 'K';
if (board->black_kings & mask) return 'k';
return ' ';
}
static char identify_piece_white(const board_t * board, int p) {
uint64_t mask = (1ULL << p);
if (board->white_pawns & mask) return 'P';
if (board->white_knights & mask) return 'N';
if (board->white_bishops & mask) return 'B';
if (board->white_rooks & mask) return 'R';
if (board->white_queens & mask) return 'Q';
if (board->white_kings & mask) return 'K';
return ' ';
}
static char identify_piece_black(const board_t * board, int p) {
uint64_t mask = (1ULL << p);
if (board->black_pawns & mask) return 'p';
if (board->black_knights & mask) return 'n';
if (board->black_bishops & mask) return 'b';
if (board->black_rooks & mask) return 'r';
if (board->black_queens & mask) return 'q';
if (board->black_kings & mask) return 'k';
return ' ';
}
static int64_t hash_from_board(const board_t * board) {
int64_t hash = 0xAAAAAAAAAAAAAAAA;
for (int p = 0; p < 64; ++p) {
char piece = identify_piece(board, p);
if (piece != ' ') {
hash = update_hash_with_piece(hash, p, piece);
}
}
if (board->en_passant_x != NO_EN_PASSANT) {
hash ^= zobrist_en_passant[(int)(board->en_passant_x)];
}
if (board->white_right_castling) {
hash ^= zobrist_castling[CASTLING_WHITE_RIGHT];
}
if (board->white_left_castling) {
hash ^= zobrist_castling[CASTLING_WHITE_LEFT];
}
if (board->black_right_castling) {
hash ^= zobrist_castling[CASTLING_BLACK_RIGHT];
}
if (board->black_left_castling) {
hash ^= zobrist_castling[CASTLING_BLACK_LEFT];
}
return hash;
}
static void fprint_board(FILE * fd, const board_t * board, const board_ext_t * board_ext) {
board_to_fen(buffer, board, board_ext);
fprintf(fd, "%s\n", buffer);
int line_len = 0;
for (unsigned int i = 0; i < board_ext->past_plays_count; ++i) {
if ((i & 1) == 0) {
if (line_len > 68) {
fprintf(fd, "\n");
line_len = 0;
}
if (line_len == 0) {
line_len += fprintf(fd, "%d.", i / 2 + 1);
} else {
line_len += fprintf(fd, " %d.", i / 2 + 1);
}
}
line_len += fprintf(fd, " %c%d%c%d", 'a' + board_ext->past_plays[i].from_x, 8 - board_ext->past_plays[i].from_y, 'a' + board_ext->past_plays[i].to_x, 8 - board_ext->past_plays[i].to_y);
}
if (line_len > 0) {
fprintf(fd, "\n");
}
fprintf(fd, "╔═══╤═══╤═══╤═══╤═══╤═══╤═══╤═══╗┈╮\n");
for (int y = 0; y < 8; y++) {
fprintf(fd, "║ ");
for (int x = 0; x < 8; x++) {
if (x == board_ext->last_play_x && y == board_ext->last_play_y) {
fprintf(fd, "\033[32m%c\e[0m", identify_piece(board, y * 8 + x));
} else {
fprintf(fd, "%c", identify_piece(board, y * 8 + x));
}
if (x < 7) {
fprintf(fd, " │ ");
} else {
fprintf(fd, " ║ %c", 8 - y + '0');
}
}
fprintf(fd, ("\n"));
if (y < 7) {
fprintf(fd, "╟───┼───┼───┼───┼───┼───┼───┼───╢ ┊\n");
}
}
fprintf(fd, "╚═══╧═══╧═══╧═══╧═══╧═══╧═══╧═══╝ ┊\n");
fprintf(fd, "╰┈a┈┈┈b┈┈┈c┈┈┈d┈┈┈e┈┈┈f┈┈┈g┈┈┈h┈┈┈╯\n\n");
}
static void print_board(const board_t * board, const board_ext_t * board_ext) {
fprint_board(stdout, board, board_ext);
}
static void just_play_white_simple(board_t * board, const play_t * play) {
char from_x = play->from_x;
char from_y = play->from_y;
char to_x = play->to_x;
char to_y = play->to_y;
char promotion_option = play->promotion_option;
int from_p = from_y * 8 + from_x;
int to_p = to_y * 8 + to_x;
char from_piece = identify_piece_white(board, from_p);
uint64_t from_mask = 1ULL << from_p;
uint64_t to_mask = 1ULL << to_p;
// Remove captured piece, at destination
board->black_pawns &= ~to_mask;
board->black_knights &= ~to_mask;
board->black_bishops &= ~to_mask;
board->black_rooks &= ~to_mask;
board->black_queens &= ~to_mask;
board->black_kings &= ~to_mask;
// Remove moved piece from origin and add it to destination
switch (from_piece) {
case 'P':
board->white_pawns ^= from_mask;
if (promotion_option == 0) {
board->white_pawns ^= to_mask;
} else {
switch (promotion_option) {
case PROMOTION_QUEEN: board->white_queens ^= to_mask; break;
case PROMOTION_KNIGHT: board->white_knights ^= to_mask; break;
case PROMOTION_BISHOP: board->white_bishops ^= to_mask; break;
case PROMOTION_ROOK: board->white_rooks ^= to_mask; break;
}
}
break;
case 'N':
board->white_knights ^= from_mask;
board->white_knights ^= to_mask;
break;
case 'B':
board->white_bishops ^= from_mask;
board->white_bishops ^= to_mask;
break;
case 'R':
board->white_rooks ^= from_mask;
board->white_rooks ^= to_mask;
break;
case 'Q':
board->white_queens ^= from_mask;
board->white_queens ^= to_mask;
break;
case 'K':
board->white_kings ^= from_mask;
board->white_kings ^= to_mask;
break;
}
if (board->en_passant_x == to_x) {
if (from_y == 3 && from_piece == 'P') {
board->black_pawns ^= (1ULL << (from_y * 8 + to_x));
}
}
if (from_piece == 'K') {
if (from_x == 4) {
if (to_x == 6) {
board->white_rooks ^= (1ULL << (0 * 8 + 5));
board->white_rooks ^= (1ULL << (0 * 8 + 7));
} else if (to_x == 2) {
board->white_rooks ^= (1ULL << (0 * 8 + 0));
board->white_rooks ^= (1ULL << (0 * 8 + 3));
}
}
}
}
static void just_play_black_simple(board_t * board, const play_t * play) {
char from_x = play->from_x;
char from_y = play->from_y;
char to_x = play->to_x;
char to_y = play->to_y;
char promotion_option = play->promotion_option;
int from_p = from_y * 8 + from_x;
int to_p = to_y * 8 + to_x;
char from_piece = identify_piece_black(board, from_p);
uint64_t from_mask = 1ULL << from_p;
uint64_t to_mask = 1ULL << to_p;
// Remove captured piece, at destination
board->white_pawns &= ~to_mask;
board->white_knights &= ~to_mask;
board->white_bishops &= ~to_mask;
board->white_rooks &= ~to_mask;
board->white_queens &= ~to_mask;
board->white_kings &= ~to_mask;
// Remove moved piece from origin and add it to destination
switch (from_piece) {
case 'p':
board->black_pawns ^= from_mask;
if (promotion_option == 0) {
board->black_pawns ^= to_mask;
} else {
switch (promotion_option) {
case PROMOTION_QUEEN: board->black_queens ^= to_mask; break;
case PROMOTION_KNIGHT: board->black_knights ^= to_mask; break;
case PROMOTION_BISHOP: board->black_bishops ^= to_mask; break;
case PROMOTION_ROOK: board->black_rooks ^= to_mask; break;
}
}
break;
case 'n':
board->black_knights ^= from_mask;
board->black_knights ^= to_mask;
break;
case 'b':
board->black_bishops ^= from_mask;
board->black_bishops ^= to_mask;
break;
case 'r':
board->black_rooks ^= from_mask;
board->black_rooks ^= to_mask;
break;
case 'q':
board->black_queens ^= from_mask;
board->black_queens ^= to_mask;
break;
case 'k':
board->black_kings ^= from_mask;
board->black_kings ^= to_mask;
break;
}
if (board->en_passant_x == to_x) {
if (from_y == 4 && from_piece == 'p') {
board->white_pawns ^= (1ULL << (from_y * 8 + to_x));
}
}
if (from_piece == 'k') {
if (from_x == 4) {
if (to_x == 6) {
board->black_rooks ^= (1ULL << (0 * 8 + 5));
board->black_rooks ^= (1ULL << (0 * 8 + 7));
} else if (to_x == 2) {
board->black_rooks ^= (1ULL << (0 * 8 + 0));
board->black_rooks ^= (1ULL << (0 * 8 + 3));
}
}
}
}
static void just_play_white_pawn(board_t * board, const play_t * play, int depth, int64_t * out_hash) {
int from_x = play->from_x;
int from_y = play->from_y;
int to_x = play->to_x;
int to_y = play->to_y;
int en_passant_x = board->en_passant_x;
int from_p = from_y * 8 + from_x;
int to_p = to_y * 8 + to_x;
char promotion_option = play->promotion_option;
char from_piece = 'P';
char to_piece = identify_piece_black(board, to_p);
int64_t hash = *out_hash;
hash = update_hash_with_piece_white(hash, from_p, from_piece);
if (to_piece != ' ') {
hash = update_hash_with_piece_black(hash, to_p, to_piece);
}
hash ^= zobrist_depth[depth];
hash ^= zobrist_depth[depth + 1];
uint64_t from_mask = 1ULL << from_p;
uint64_t to_mask = 1ULL << to_p;
// Remove captured piece, at destination
switch (to_piece) {
case 'p':
board->black_pawns ^= to_mask;
break;
case 'n':
board->black_knights ^= to_mask;
break;
case 'b':
board->black_bishops ^= to_mask;
break;
case 'r':
board->black_rooks ^= to_mask;
break;
case 'q':
board->black_queens ^= to_mask;
break;
case 'k':
board->black_kings ^= to_mask;
break;
}
board->white_pawns ^= from_mask;
if (en_passant_x != NO_EN_PASSANT) {
if (en_passant_x == to_x && from_y == 3) {
hash = update_hash_with_piece_black(hash, 3 * 8 + en_passant_x, 'p');
board->black_pawns ^= 1ULL << (3 * 8 + en_passant_x);
}
hash ^= zobrist_en_passant[en_passant_x];
board->en_passant_x = NO_EN_PASSANT;
}
if (to_y == 0) {
if (to_x == 0) {
if (board->black_left_castling) {
board->black_left_castling = 0;
hash ^= zobrist_castling[CASTLING_BLACK_LEFT];
}
} else if (to_x == 7) {
if (board->black_right_castling) {
board->black_right_castling = 0;
hash ^= zobrist_castling[CASTLING_BLACK_RIGHT];
}
}
if (promotion_option == PROMOTION_QUEEN) {
board->white_queens ^= to_mask;
hash = update_hash_with_piece(hash, to_p, 'Q');
} else if (promotion_option == PROMOTION_KNIGHT) {
board->white_knights ^= to_mask;
hash = update_hash_with_piece(hash, to_p, 'N');
} else if (promotion_option == PROMOTION_BISHOP) {
board->white_bishops ^= to_mask;
hash = update_hash_with_piece(hash, to_p, 'B');
} else {
board->white_rooks ^= to_mask;
hash = update_hash_with_piece(hash, to_p, 'R');
}
} else {
board->white_pawns ^= to_mask;
hash = update_hash_with_piece(hash, to_p, 'P');
if (from_y == 6 && to_y == 4) {
board->en_passant_x = from_x;
hash ^= zobrist_en_passant[from_x];
}
}
board->color = BLACK_COLOR;
*out_hash = hash;
}
static void just_play_white_complex(board_t * board, const play_t * play, int depth, int64_t * out_hash) {
char from_x = play->from_x;
char from_y = play->from_y;
int from_p = from_y * 8 + from_x;
char from_piece = identify_piece_white(board, from_p);
if (from_piece == 'P') {
just_play_white_pawn(board, play, depth, out_hash);
return;
}
char to_x = play->to_x;
char to_y = play->to_y;
int to_p = to_y * 8 + to_x;
char to_piece = identify_piece_black(board, to_p);
int64_t hash = *out_hash;
hash = update_hash_with_piece_white(hash, from_p, from_piece);
if (to_piece != ' ') {
hash = update_hash_with_piece_black(hash, to_p, to_piece);
}
int en_passant_x = board->en_passant_x;
if (en_passant_x != NO_EN_PASSANT) {
hash ^= zobrist_en_passant[en_passant_x];
board->en_passant_x = NO_EN_PASSANT;
}
hash ^= zobrist_depth[depth];
uint64_t from_mask = 1ULL << from_p;
uint64_t to_mask = 1ULL << to_p;
// Remove captured piece, at destination
switch (to_piece) {
case 'p':
board->black_pawns ^= to_mask;
break;
case 'n':
board->black_knights ^= to_mask;
break;
case 'b':
board->black_bishops ^= to_mask;
break;
case 'r':
board->black_rooks ^= to_mask;
break;
case 'q':
board->black_queens ^= to_mask;
break;
case 'k':
board->black_kings ^= to_mask;
break;
}
// Remove moved piece from origin and add it to destination
switch (from_piece) {
case 'N':
board->white_knights ^= from_mask;
board->white_knights ^= to_mask;
break;
case 'B':
board->white_bishops ^= from_mask;
board->white_bishops ^= to_mask;
break;
case 'R':
board->white_rooks ^= from_mask;
board->white_rooks ^= to_mask;
break;
case 'Q':
board->white_queens ^= from_mask;
board->white_queens ^= to_mask;
break;
case 'K':
board->white_kings ^= from_mask;
board->white_kings ^= to_mask;
break;
}
if (from_piece == 'K') {
if (from_x == 4) {
if (to_x == 6) {
board->white_rooks ^= (1ULL << (7 * 8 + 5));
board->white_rooks ^= (1ULL << (7 * 8 + 7));
hash = update_hash_with_piece_white(hash, 7 * 8 + 7, 'R');
hash = update_hash_with_piece_white(hash, 7 * 8 + 5, 'R');
hash ^= zobrist_castling[CASTLING_WHITE_RIGHT];
if (board->white_left_castling) {
hash ^= zobrist_castling[CASTLING_WHITE_LEFT];
}
} else if (to_x == 2) {
board->white_rooks ^= (1ULL << (7 * 8 + 0));
board->white_rooks ^= (1ULL << (7 * 8 + 3));
hash = update_hash_with_piece_white(hash, 7 * 8 + 0, 'R');
hash = update_hash_with_piece_white(hash, 7 * 8 + 3, 'R');
hash ^= zobrist_castling[CASTLING_WHITE_LEFT];
if (board->white_right_castling) {
hash ^= zobrist_castling[CASTLING_WHITE_RIGHT];
}
}
}
board->white_right_castling = 0;
board->white_left_castling = 0;
} else {
if (from_y == 7) {
if (from_x == 0) {
if (board->white_left_castling) {
board->white_left_castling = 0;
hash ^= zobrist_castling[CASTLING_WHITE_LEFT];
}
} else if (from_x == 7) {
if (board->white_right_castling) {
board->white_right_castling = 0;
hash ^= zobrist_castling[CASTLING_WHITE_RIGHT];
}
}
}
}
if (to_y == 0) {
if (to_x == 0) {
if (board->black_left_castling) {
board->black_left_castling = 0;
hash ^= zobrist_castling[CASTLING_BLACK_LEFT];
}
} else if (to_x == 7) {
if (board->black_right_castling) {
board->black_right_castling = 0;
hash ^= zobrist_castling[CASTLING_BLACK_RIGHT];
}
}
}
hash = update_hash_with_piece(hash, to_p, from_piece);
hash ^= zobrist_depth[depth + 1];
board->color = BLACK_COLOR;
*out_hash = hash;
}
static void just_play_black_pawn(board_t * board, const play_t * play, int depth, int64_t * out_hash) {
int from_x = play->from_x;
int from_y = play->from_y;
int to_x = play->to_x;
int to_y = play->to_y;
int en_passant_x = board->en_passant_x;
int from_p = from_y * 8 + from_x;
int to_p = to_y * 8 + to_x;
char promotion_option = play->promotion_option;
char from_piece = 'p';
char to_piece = identify_piece_white(board, to_p);
int64_t hash = *out_hash;
hash = update_hash_with_piece_black(hash, from_p, from_piece);
if (to_piece != ' ') {
hash = update_hash_with_piece_white(hash, to_p, to_piece);
}
hash ^= zobrist_depth[depth];
hash ^= zobrist_depth[depth + 1];
uint64_t from_mask = 1ULL << from_p;
uint64_t to_mask = 1ULL << to_p;
// Remove captured piece, at destination
switch (to_piece) {
case 'P':
board->white_pawns ^= to_mask;
break;
case 'N':
board->white_knights ^= to_mask;
break;
case 'B':
board->white_bishops ^= to_mask;
break;
case 'R':
board->white_rooks ^= to_mask;
break;
case 'Q':
board->white_queens ^= to_mask;
break;
case 'K':
board->white_kings ^= to_mask;
break;
}
board->black_pawns ^= from_mask;
if (en_passant_x != NO_EN_PASSANT) {
if (en_passant_x == to_x && from_y == 4) {
hash = update_hash_with_piece_white(hash, 4 * 8 + en_passant_x, 'P');
board->white_pawns ^= 1ULL << (4 * 8 + en_passant_x);
}
hash ^= zobrist_en_passant[en_passant_x];
board->en_passant_x = NO_EN_PASSANT;
}
if (to_y == 7) {
if (to_x == 0) {
if (board->white_left_castling) {
board->white_left_castling = 0;
hash ^= zobrist_castling[CASTLING_WHITE_LEFT];
}
} else if (to_x == 7) {
if (board->white_right_castling) {
board->white_right_castling = 0;
hash ^= zobrist_castling[CASTLING_WHITE_RIGHT];
}
}
if (promotion_option == PROMOTION_QUEEN) {
board->black_queens ^= to_mask;
hash = update_hash_with_piece(hash, to_p, 'q');
} else if (promotion_option == PROMOTION_KNIGHT) {
board->black_knights ^= to_mask;
hash = update_hash_with_piece(hash, to_p, 'n');
} else if (promotion_option == PROMOTION_BISHOP) {
board->black_bishops ^= to_mask;
hash = update_hash_with_piece(hash, to_p, 'b');
} else {
board->black_rooks ^= to_mask;
hash = update_hash_with_piece(hash, to_p, 'r');
}
} else {
board->black_pawns ^= to_mask;
hash = update_hash_with_piece(hash, to_p, 'p');
if (from_y == 1 && to_y == 3) {
board->en_passant_x = from_x;
hash ^= zobrist_en_passant[from_x];
}
}