-
Notifications
You must be signed in to change notification settings - Fork 0
/
movedirectory.cpp
872 lines (778 loc) · 32.9 KB
/
movedirectory.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
//===========================================================//
//
// Maverick Chess Engine
// Copyright 2013-2015 Steve Maughan
//
//===========================================================//
#include <stdlib.h>
#include <stdio.h>
#if defined(_WIN32)
#include <windows.h>
#include <process.h>
#include <conio.h>
#else
#include <unistd.h>
#endif
#include <string.h>
#include <math.h>
#include <assert.h>
#include "defs.h"
#include "data.h"
#include "procs.h"
#include "bittwiddle.h"
#ifndef max
#define max(a,b) (((a) > (b)) ? (a) : (b))
#define min(a,b) (((a) < (b)) ? (a) : (b))
#endif
t_move_record *lookup_move(struct t_board *board, char *move_string) {
t_chess_square from_square = name_to_index(move_string);
t_chess_square to_square = name_to_index(move_string + 2);
t_chess_piece piece = board->square[from_square];
t_chess_piece captured = PIECETYPE(board->square[to_square]);
t_chess_color color = COLOR(piece);
t_chess_piece promote_to = 0;
if (KING == PIECETYPE(piece)) {
// Is it a Chess960 castling move?
if ((captured != BLANK) && (COLOR(board->square[to_square]) == color)) {
if (to_square > from_square) {
return &xmove_list[2 * color];
}
else {
return &xmove_list[2 * color + 1];
}
}
else
return move_directory[from_square][to_square][piece] + captured;
}
else if (PAWN == PIECETYPE(piece) && (RANK(to_square) % 7 == 0)) {
if (move_string[4] == 'q')
promote_to = QUEEN;
else if (move_string[4] == 'b')
promote_to = BISHOP;
else if (move_string[4] == 'r')
promote_to = ROOK;
else if (move_string[4] == 'n')
promote_to = KNIGHT;
return move_directory[from_square][to_square][piece] + (4 * captured) + promote_to - 1;
}
else
return move_directory[from_square][to_square][piece] + captured;
}
BOOL castle_integrity(struct t_board *board)
{
BOOL ok = TRUE;
//-- Loop round for each possible type of castle
for (int i = 0; i < 4; i++)
{
struct t_move_record *move = &xmove_list[i];
//-- Is this type of castling relevant in this position?
if (castle[i].mask & board->castling)
{
//-- Is the king in its square?
if (PIECETYPE(board->square[castle[i].king_from]) != KING)
ok = FALSE;
//-- Is the rook on its square?
if (PIECETYPE(board->square[castle[i].rook_from]) != ROOK)
ok = FALSE;
if (move->from_square != castle[i].king_from)
ok = FALSE;
}
}
if (!ok)
send_info("Castling Messed Up!!");
return ok;
}
void configure_castling()
{
struct t_move_record *move = &xmove_list[0];
// White Kingside
move->captured = BLANK;
move->from_square = E1;
move->to_square = G1;
move->from_to_bitboard = SQUARE64(move->from_square) | SQUARE64(move->to_square);
move->move_type = MOVE_CASTLE;
move->piece = WHITEKING;
move->promote_to = BLANK;
move->castling_delta = (BLACK_CASTLE_OO | BLACK_CASTLE_OOO);
move_directory[move->from_square][move->to_square][move->piece] = move;
move_directory[move->from_square][H1][move->piece] = move;
move++;
// White Queenside
move->captured = BLANK;
move->from_square = E1;
move->to_square = C1;
move->from_to_bitboard = SQUARE64(move->from_square) | SQUARE64(move->to_square);
move->move_type = MOVE_CASTLE;
move->piece = WHITEKING;
move->promote_to = BLANK;
move->castling_delta = (BLACK_CASTLE_OO | BLACK_CASTLE_OOO);
move_directory[move->from_square][move->to_square][move->piece] = move;
move_directory[move->from_square][A1][move->piece] = move;
move++;
// Black Kingside
move->captured = BLANK;
move->from_square = E8;
move->to_square = G8;
move->from_to_bitboard = SQUARE64(move->from_square) | SQUARE64(move->to_square);
move->move_type = MOVE_CASTLE;
move->piece = BLACKKING;
move->promote_to = BLANK;
move->castling_delta = (WHITE_CASTLE_OO | WHITE_CASTLE_OOO);
move_directory[move->from_square][move->to_square][move->piece] = move;
move_directory[move->from_square][H8][move->piece] = move;
move++;
// Black Queenside
move->captured = BLANK;
move->from_square = E8;
move->to_square = C8;
move->from_to_bitboard = SQUARE64(move->from_square) | SQUARE64(move->to_square);
move->move_type = MOVE_CASTLE;
move->piece = BLACKKING;
move->promote_to = BLANK;
move->castling_delta = (WHITE_CASTLE_OO | WHITE_CASTLE_OOO);
move_directory[move->from_square][move->to_square][move->piece] = move;
move_directory[move->from_square][A8][move->piece] = move;
move++;
//-- Initialize Castle Possible
castle[0].possible = SQUARE64(F1) | SQUARE64(G1);
castle[1].possible = SQUARE64(D1) | SQUARE64(C1) | SQUARE64(B1);
castle[2].possible = SQUARE64(F8) | SQUARE64(G8);
castle[3].possible = SQUARE64(D8) | SQUARE64(C8) | SQUARE64(B8);
//-- Initialize Castle Attacks
castle[0].not_attacked = (SQUARE64(F1) | SQUARE64(G1));
castle[1].not_attacked = (SQUARE64(D1) | SQUARE64(C1));
castle[2].not_attacked = (SQUARE64(F8) | SQUARE64(G8));
castle[3].not_attacked = (SQUARE64(D8) | SQUARE64(C8));
//-- Initialize Rook Square
castle[0].rook_from = H1;
castle[1].rook_from = A1;
castle[2].rook_from = H8;
castle[3].rook_from = A8;
castle[0].rook_to = F1;
castle[1].rook_to = D1;
castle[2].rook_to = F8;
castle[3].rook_to = D8;
//-- Initialize King Square
castle[0].king_from = E1;
castle[1].king_from = E1;
castle[2].king_from = E8;
castle[3].king_from = E8;
// Initialize Castle Mask
castle[0].mask = WHITE_CASTLE_OO;
castle[1].mask = WHITE_CASTLE_OOO;
castle[2].mask = BLACK_CASTLE_OO;
castle[3].mask = BLACK_CASTLE_OOO;
for (int i = 0; i < 4; i++) {
castle[i].rook_from_to = (SQUARE64(castle[i].rook_from) | SQUARE64(castle[i].rook_to));
castle[i].rook_piece = PIECEINDEX((i >> 1), ROOK);
}
// Define the changes to the hash
for (int i = 0; i < 4; i++)
{
move = &xmove_list[i];
move->hash_delta = white_to_move_hash;
move->hash_delta ^= hash_value[move->piece][move->from_square] ^ hash_value[move->piece][move->to_square];
move->hash_delta ^= hash_value[castle[i].rook_piece][castle[i].rook_from] ^ hash_value[castle[i].rook_piece][castle[i].rook_to];
move->pawn_hash_delta = white_to_move_hash;
move->pawn_hash_delta ^= hash_value[move->piece][move->from_square] ^ hash_value[move->piece][move->to_square];
}
}
void configure_pawn_push(int *i)
{
int s;
t_chess_piece promote;
struct t_move_record *move = &xmove_list[*i];
// White double pawn push
for (s = A2; s <= H2; s++) {
move->captured = BLANK;
move->from_square = s;
move->to_square = s + 16;
move->move_type = MOVE_PAWN_PUSH2;
move->piece = WHITEPAWN;
move->promote_to = BLANK;
move_directory[move->from_square][move->to_square][move->piece] = move;
(*i)++;
move++;
}
// White non-promoting pawn push
for (s = A2; s <= H6; s++) {
move->captured = BLANK;
move->from_square = s;
move->to_square = s + 8;
move->move_type = MOVE_PAWN_PUSH1;
move->piece = WHITEPAWN;
move->promote_to = BLANK;
move_directory[move->from_square][move->to_square][move->piece] = move;
(*i)++;
move++;
}
// White promotions
for (s = A7; s <= H7; s++) {
move_directory[s][s + 8][WHITEPAWN] = move;
for (promote = WHITEKNIGHT; promote <= WHITEQUEEN; promote++) {
move->captured = BLANK;
move->from_square = s;
move->to_square = s + 8;
move->move_type = MOVE_PROMOTION;
move->piece = WHITEPAWN;
move->promote_to = promote;
(*i)++;
move++;
}
}
// Black non-promoting pawn push
for (s = A7; s <= H7; s++) {
move->captured = BLANK;
move->from_square = s;
move->to_square = s - 16;
move->move_type = MOVE_PAWN_PUSH2;
move->piece = BLACKPAWN;
move->promote_to = BLANK;
move_directory[move->from_square][move->to_square][move->piece] = move;
(*i)++;
move++;
}
// Black non-promoting pawn push
for (s = A3; s <= H7; s++) {
move->captured = BLANK;
move->from_square = s;
move->to_square = s - 8;
move->move_type = MOVE_PAWN_PUSH1;
move->piece = BLACKPAWN;
move->promote_to = BLANK;
move_directory[move->from_square][move->to_square][move->piece] = move;
(*i)++;
move++;
}
// Black pawn promotions
for (s = A2; s <= H2; s++) {
move_directory[s][s - 8][BLACKPAWN] = move;
for (promote = BLACKKNIGHT; promote <= BLACKQUEEN; promote++) {
move->captured = BLANK;
move->from_square = s;
move->to_square = s - 8;
move->move_type = MOVE_PROMOTION;
move->piece = BLACKPAWN;
move->promote_to = promote;
(*i)++;
move++;
}
}
}
void configure_pawn_capture(int *i)
{
int s;
t_chess_piece promote, capture;
struct t_move_record *move = &xmove_list[*i];
// White non-promoting captures
for (s = A2; s <= H6; s++) {
// North East Capture
if (COLUMN(s) < 7) {
// ep capture
if (RANK(s) == 4) {
move->captured = BLACKPAWN;
move->from_square = s;
move->to_square = s + 9;
move->move_type = MOVE_PxP_EP;
move->piece = WHITEPAWN;
move->promote_to = BLANK;
move_directory[move->from_square][move->to_square][move->piece] = move;
(*i)++;
move++;
}
for (capture = BLACKKNIGHT; capture <=BLACKPAWN; capture++) {
move->captured = capture;
move->from_square = s;
move->to_square = s + 9;
if (PIECETYPE(capture) == PAWN) {
move->move_type = MOVE_PxPAWN;
}
else {
move->move_type = MOVE_PxPIECE;
}
move->piece = WHITEPAWN;
move->promote_to = BLANK;
move_directory[move->from_square][move->to_square][move->piece] = move - PIECETYPE(capture);
(*i)++;
move++;
}
}
// North West Capture
if (COLUMN(s) > 0) {
// ep capture
if (RANK(s) == 4) {
move->captured = BLACKPAWN;
move->from_square = s;
move->to_square = s + 7;
move->move_type = MOVE_PxP_EP;
move->piece = WHITEPAWN;
move->promote_to = BLANK;
move_directory[move->from_square][move->to_square][move->piece] = move;
(*i)++;
move++;
}
for (capture = BLACKKNIGHT; capture <=BLACKPAWN; capture++) {
move->captured = capture;
move->from_square = s;
move->to_square = s + 7;
if (PIECETYPE(capture) == PAWN) {
move->move_type = MOVE_PxPAWN;
}
else {
move->move_type = MOVE_PxPIECE;
}
move->piece = WHITEPAWN;
move->promote_to = BLANK;
move_directory[move->from_square][move->to_square][move->piece] = move - PIECETYPE(capture);
(*i)++;
move++;
}
}
}
// White Capture Promotes
for (s = A7; s <= H7; s++) {
if (COLUMN(s) < 7) {
for (capture = BLACKKNIGHT; capture <= BLACKQUEEN; capture++) {
move_directory[s][s + 9][WHITEPAWN] = move - (4 * PIECETYPE(capture));
for (promote = WHITEKNIGHT; promote <= WHITEQUEEN; promote++) {
move->captured = capture;
move->from_square = s;
move->to_square = s + 9;
move->move_type = MOVE_CAPTUREPROMOTE;
move->piece = WHITEPAWN;
move->promote_to = promote;
(*i)++;
move++;
}
}
}
if (COLUMN(s) > 0) {
for (capture = BLACKKNIGHT; capture <= BLACKQUEEN; capture++) {
move_directory[s][s + 7][WHITEPAWN] = move - (4 * PIECETYPE(capture));
for (promote = WHITEKNIGHT; promote <= WHITEQUEEN; promote++) {
move->captured = capture;
move->from_square = s;
move->to_square = s + 7;
move->move_type = MOVE_CAPTUREPROMOTE;
move->piece = WHITEPAWN;
move->promote_to = promote;
(*i)++;
move++;
}
}
}
}
// Black non-promoting captures
for (s = A3; s <= H7; s++) {
// South East Capture
if (COLUMN(s) < 7) {
// ep capture
if (RANK(s) == 3) {
move->captured = WHITEPAWN;
move->from_square = s;
move->to_square = s - 7;
move->move_type = MOVE_PxP_EP;
move->piece = BLACKPAWN;
move->promote_to = BLANK;
move_directory[move->from_square][move->to_square][move->piece] = move;
(*i)++;
move++;
}
for (capture = WHITEKNIGHT; capture <= WHITEPAWN; capture++) {
move->captured = capture;
move->from_square = s;
move->to_square = s - 7;
if (PIECETYPE(capture) == PAWN) {
move->move_type = MOVE_PxPAWN;
}
else {
move->move_type = MOVE_PxPIECE;
}
move->piece = BLACKPAWN;
move->promote_to = BLANK;
move_directory[move->from_square][move->to_square][move->piece] = move - PIECETYPE(capture);
(*i)++;
move++;
}
}
// North West Capture
if (COLUMN(s) > 0) {
// ep capture
if (RANK(s) == 3) {
move->captured = WHITEPAWN;
move->from_square = s;
move->to_square = s - 9;
move->move_type = MOVE_PxP_EP;
move->piece = BLACKPAWN;
move->promote_to = BLANK;
move_directory[move->from_square][move->to_square][move->piece] = move;
(*i)++;
move++;
}
for (capture = WHITEKNIGHT; capture <= WHITEPAWN; capture++) {
move->captured = capture;
move->from_square = s;
move->to_square = s - 9;
if (PIECETYPE(capture) == PAWN) {
move->move_type = MOVE_PxPAWN;
}
else {
move->move_type = MOVE_PxPIECE;
}
move->piece = BLACKPAWN;
move->promote_to = BLANK;
move_directory[move->from_square][move->to_square][move->piece] = move - PIECETYPE(capture);
(*i)++;
move++;
}
}
}
// Black Capture Promotes
for (s = A2; s <= H2; s++) {
if (COLUMN(s) < 7) {
for (capture = WHITEKNIGHT; capture <= WHITEQUEEN; capture++) {
move_directory[s][s - 7][BLACKPAWN] = move - (4 * PIECETYPE(capture));
for (promote = BLACKKNIGHT; promote <= BLACKQUEEN; promote++) {
move->captured = capture;
move->from_square = s;
move->to_square = s - 7;
move->move_type = MOVE_CAPTUREPROMOTE;
move->piece = BLACKPAWN;
move->promote_to = promote;
(*i)++;
move++;
}
}
}
if (COLUMN(s) > 0) {
for (capture = WHITEKNIGHT; capture <= WHITEQUEEN; capture++) {
move_directory[s][s - 9][BLACKPAWN] = move - (4 * PIECETYPE(capture));
for (promote = BLACKKNIGHT; promote <= BLACKQUEEN; promote++) {
move->captured = capture;
move->from_square = s;
move->to_square = s - 9;
move->move_type = MOVE_CAPTUREPROMOTE;
move->piece = BLACKPAWN;
move->promote_to = promote;
(*i)++;
move++;
}
}
}
}
}
void configure_piece_moves(int *i)
{
int delta, d;
t_chess_square s, target;
t_chess_color color, opponent;
t_chess_piece p, move_piece, capture;
struct t_move_record *move = &xmove_list[*i];
for (color = WHITE; color <= BLACK; color++) {
opponent = OPPONENT(color);
for (s = A1; s <= H8; s++) {
for (p = KNIGHT; p <= KING; p++) {
if (PIECETYPE(p) != PAWN) {
move_piece = p + (color * 8);
if (slider[p]) {
d = 0;
for (delta = direction[p][d]; (delta = direction[p][d]) != 0; d++) {
target = x88_SQUARE(s) + delta;
while (x88_ONBOARD(target)) {
move->captured = BLANK;
move->from_square = s;
move->to_square = x88_TO_64(target);
move->move_type = MOVE_PIECE_MOVE;
move->piece = move_piece;
move->promote_to = BLANK;
move_directory[move->from_square][move->to_square][move->piece] = move;
(*i)++;
move++;
for (capture = KNIGHT; capture <= QUEEN; capture++) {
move->captured = capture + (opponent * 8);
move->from_square = s;
move->to_square = x88_TO_64(target);
move->move_type = MOVE_PIECExPIECE;
move->piece = move_piece;
move->promote_to = BLANK;
(*i)++;
move++;
}
if ((x88_TO_64(target) >= A2) && (x88_TO_64(target) <= H7)) {
move->captured = PAWN + (opponent * 8);
move->from_square = s;
move->to_square = x88_TO_64(target);
move->move_type = MOVE_PIECExPAWN;
move->piece = move_piece;
move->promote_to = BLANK;
(*i)++;
move++;
}
target += delta;
}
}
}
else
{
d = 0;
for (delta = direction[p][d]; (delta = direction[p][d]) != 0; d++) {
target = x88_SQUARE(s) + delta;
if (x88_ONBOARD(target)) {
move->captured = BLANK;
move->from_square = s;
move->to_square = x88_TO_64(target);
if (PIECETYPE(move_piece) == KING)
move->move_type = MOVE_KING_MOVE;
else
move->move_type = MOVE_PIECE_MOVE;
move->piece = move_piece;
move->promote_to = BLANK;
move_directory[move->from_square][move->to_square][move->piece] = move;
(*i)++;
move++;
for (capture = KNIGHT; capture <= QUEEN; capture++) {
move->captured = capture + (opponent * 8);
move->from_square = s;
move->to_square = x88_TO_64(target);
if (PIECETYPE(move_piece) == KING)
move->move_type = MOVE_KINGxPIECE;
else
move->move_type = MOVE_PIECExPIECE;
move->piece = move_piece;
move->promote_to = BLANK;
(*i)++;
move++;
}
if ((x88_TO_64(target) >= A2) && (x88_TO_64(target) <= H7)) {
move->captured = PAWN + (opponent * 8);
move->from_square = s;
move->to_square = x88_TO_64(target);
if (PIECETYPE(move_piece) == KING)
move->move_type = MOVE_KINGxPAWN;
else
move->move_type = MOVE_PIECExPAWN;
move->piece = move_piece;
move->promote_to = BLANK;
(*i)++;
move++;
}
}
}
}
}
}
}
}
}
void init_960_castling(struct t_board *board, t_chess_square king_square, t_chess_square rook_square)
{
// Color which is castling
t_chess_color color = RANK(king_square) / 7;
// Local varibles
int castle_index;
t_chess_square king_to;
t_chess_square smin;
t_chess_square smax;
t_chess_square kmin;
t_chess_square kmax;
struct t_move_record *move;
// Is it Kingside castling
if (king_square < rook_square) {
castle_index = (color * 2);
board->castling |= castle[castle_index].mask;
if (castle[castle_index].king_from == king_square && castle[castle_index].rook_from == rook_square)
return;
board->castling_squares_changed = TRUE;
king_to = G1 + (56 * color);
castle[castle_index].rook_to = king_to - 1;
castle[castle_index].possible = 0;
castle[castle_index].not_attacked = 0;
smax = max(rook_square, king_to);
smin = min(king_to - 1, king_square);
kmin = min(king_square, king_to);
kmax = max(king_square, king_to);
for (t_chess_square s = smin; s <= smax; s++)
{
castle[castle_index].possible |= SQUARE64(s);
if (s >= kmin && s <= kmax)
castle[castle_index].not_attacked |= SQUARE64(s);
}
}
// Queenside castling
else {
castle_index = (color * 2) + 1;
board->castling |= castle[castle_index].mask;
if (castle[castle_index].king_from == king_square && castle[castle_index].rook_from == rook_square)
return;
board->castling_squares_changed = TRUE;
king_to = C1 + (56 * color);
castle[castle_index].rook_to = king_to + 1;
castle[castle_index].possible = 0;
castle[castle_index].not_attacked = 0;
smin = min(rook_square, king_to);
smax = max(king_to + 1, king_square);
kmin = min(king_square, king_to);
kmax = max(king_square, king_to);
for (t_chess_square s = smin; s <= smax; s++)
{
castle[castle_index].possible |= SQUARE64(s);
if (s >= kmin && s <= kmax)
castle[castle_index].not_attacked |= SQUARE64(s);
}
}
castle[castle_index].possible &= ~SQUARE64(rook_square);
castle[castle_index].possible &= ~SQUARE64(king_square);
castle[castle_index].not_attacked &= ~SQUARE64(king_square);
castle[castle_index].king_from = king_square;
castle[castle_index].rook_from = rook_square;
castle[castle_index].rook_from_to = (SQUARE64(rook_square) ^ SQUARE64(castle[castle_index].rook_to));
castle[castle_index].rook_piece = PIECEINDEX(color, ROOK);
move = &xmove_list[castle_index];
move->from_square = king_square;
move->to_square = king_to;
move->from_to_bitboard = (SQUARE64(king_square) ^ SQUARE64(king_to));
move->hash_delta = white_to_move_hash;
move->hash_delta ^= hash_value[move->piece][move->from_square] ^ hash_value[move->piece][move->to_square];
move->hash_delta ^= hash_value[castle[castle_index].rook_piece][castle[castle_index].rook_from] ^ hash_value[castle[castle_index].rook_piece][castle[castle_index].rook_to];
move->pawn_hash_delta = white_to_move_hash;
move->pawn_hash_delta ^= hash_value[move->piece][move->from_square] ^ hash_value[move->piece][move->to_square];
}
void init_directory_castling_delta()
{
int i;
struct t_move_record *move;
uchar not_mask;
for (i = 0, move = &xmove_list[0]; i < GLOBAL_MOVE_COUNT; i++, move++) {
// castling mask
move->castling_delta = (WHITE_CASTLE_OO | WHITE_CASTLE_OOO | BLACK_CASTLE_OO | BLACK_CASTLE_OOO);
for (int j = 0; j < 4; j++) {
not_mask = (15 ^ ((uchar)1 << j));
if ((move->from_square == castle[j].king_from) | (move->to_square == castle[j].king_from))
move->castling_delta &= not_mask;
if ((move->from_square == castle[j].rook_from) | (move->to_square == castle[j].rook_from))
move->castling_delta &= not_mask;
}
}
}
void init_move_directory()
{
int i, j;
t_chess_square f, t;
t_chess_piece p;
t_chess_color color, opponent;
static char s[64];
struct t_move_record *move;
uchar not_mask;
// initialize the global move lists
for (f = A1; f <= H8; f++) {
for (t = A1; t <= H8; t++) {
for (p = BLANK; p <= BLACKKING; p++) {
move_directory[f][t][p] = NULL;
}
}
}
configure_castling();
i = 4;
configure_pawn_push(&i);
configure_pawn_capture(&i);
configure_piece_moves(&i);
init_directory_castling_delta();
//-- Fill in the data
for (i = 0, move = &xmove_list[0]; i < GLOBAL_MOVE_COUNT; i++, move++) {
move->index = i;
move->history = 0;
move->refutation = NULL;
move->from_to_bitboard = SQUARE64(move->from_square) | SQUARE64(move->to_square);
move->capture_mask = 0;
if (move->captured && (move->move_type != MOVE_PxP_EP))
move->capture_mask = SQUARE64(move->to_square);
color = COLOR(move->piece);
opponent = OPPONENT(color);
move->hash_delta = white_to_move_hash;
move->pawn_hash_delta = white_to_move_hash;
if (move->captured)
{
assert(move->piece >= 0 && move->piece < 15);
move->mvvlva = see_piece_value[move->captured] * 100 + (see_piece_value[QUEEN] - see_piece_value[move->piece]);
}
else
move->mvvlva = 0;
assert(move->piece >= 0 && move->piece < 16);
assert(move->from_square >= 0 && move->from_square < 64);
assert(move->to_square >= 0 && move->to_square < 64);
switch (move->move_type)
{
case MOVE_CASTLE:
j = move->index;
assert( move->index >= 0 && move->index < 4);
move->hash_delta ^= hash_value[move->piece][move->from_square] ^ hash_value[move->piece][move->to_square];
move->hash_delta ^= hash_value[castle[j].rook_piece][castle[j].rook_from] ^ hash_value[castle[j].rook_piece][castle[j].rook_to];
move->pawn_hash_delta ^= hash_value[move->piece][move->from_square] ^ hash_value[move->piece][move->to_square];
break;
case MOVE_PAWN_PUSH1:
move->hash_delta ^= hash_value[move->piece][move->from_square] ^ hash_value[move->piece][move->to_square];
move->pawn_hash_delta ^= hash_value[move->piece][move->from_square] ^ hash_value[move->piece][move->to_square];
break;
case MOVE_PAWN_PUSH2:
move->hash_delta ^= hash_value[move->piece][move->from_square] ^ hash_value[move->piece][move->to_square];
move->pawn_hash_delta ^= hash_value[move->piece][move->from_square] ^ hash_value[move->piece][move->to_square];
break;
case MOVE_PxPAWN:
assert(PIECEINDEX(opponent, PAWN) >= 0 && PIECEINDEX(opponent, PAWN) < 64);
move->hash_delta ^= hash_value[move->piece][move->from_square] ^ hash_value[move->piece][move->to_square] ^ hash_value[PIECEINDEX(opponent, PAWN)][move->to_square];
move->pawn_hash_delta ^= hash_value[move->piece][move->from_square] ^ hash_value[move->piece][move->to_square] ^ hash_value[PIECEINDEX(opponent, PAWN)][move->to_square];
break;
case MOVE_PxPIECE:
assert(move->captured >= 0 && move->captured < 16);
move->hash_delta ^= hash_value[move->piece][move->from_square] ^ hash_value[move->piece][move->to_square] ^ hash_value[move->captured][move->to_square];
move->pawn_hash_delta ^= hash_value[move->piece][move->from_square] ^ hash_value[move->piece][move->to_square];
break;
case MOVE_PxP_EP:
move->hash_delta ^= hash_value[move->piece][move->from_square] ^ hash_value[move->piece][move->to_square] ^ hash_value[PIECEINDEX(opponent, PAWN)][(move->to_square - 8) + 16 * color];
move->pawn_hash_delta ^= hash_value[move->piece][move->from_square] ^ hash_value[move->piece][move->to_square] ^ hash_value[PIECEINDEX(opponent, PAWN)][(move->to_square - 8) + 16 * color];
break;
case MOVE_PROMOTION:
assert(move->promote_to >= 0 && move->promote_to < 16);
move->hash_delta ^= hash_value[move->piece][move->from_square] ^ hash_value[move->promote_to][move->to_square];
move->pawn_hash_delta ^= hash_value[move->piece][move->from_square];
break;
case MOVE_CAPTUREPROMOTE:
assert(move->captured >= 0 && move->captured < 16);
assert(move->promote_to >= 0 && move->promote_to < 16);
move->hash_delta ^= hash_value[move->piece][move->from_square] ^ hash_value[move->promote_to][move->to_square] ^ hash_value[move->captured][move->to_square];
move->pawn_hash_delta ^= hash_value[move->piece][move->from_square];
break;
case MOVE_PIECE_MOVE:
move->hash_delta ^= hash_value[move->piece][move->from_square] ^ hash_value[move->piece][move->to_square];
break;
case MOVE_PIECExPIECE:
move->hash_delta ^= hash_value[move->piece][move->from_square] ^ hash_value[move->piece][move->to_square] ^ hash_value[move->captured][move->to_square];
break;
case MOVE_PIECExPAWN:
move->hash_delta ^= hash_value[move->piece][move->from_square] ^ hash_value[move->piece][move->to_square] ^ hash_value[move->captured][move->to_square];
move->pawn_hash_delta ^= hash_value[move->captured][move->to_square];
break;
case MOVE_KING_MOVE:
move->hash_delta ^= hash_value[move->piece][move->from_square] ^ hash_value[move->piece][move->to_square];
move->pawn_hash_delta ^= hash_value[move->piece][move->from_square] ^ hash_value[move->piece][move->to_square];
break;
case MOVE_KINGxPIECE:
move->hash_delta ^= hash_value[move->piece][move->from_square] ^ hash_value[move->piece][move->to_square] ^ hash_value[move->captured][move->to_square];
move->pawn_hash_delta ^= hash_value[move->piece][move->from_square] ^ hash_value[move->piece][move->to_square];
break;
case MOVE_KINGxPAWN:
move->hash_delta ^= hash_value[move->piece][move->from_square] ^ hash_value[move->piece][move->to_square] ^ hash_value[move->captured][move->to_square];
move->pawn_hash_delta ^= hash_value[move->captured][move->to_square];
move->pawn_hash_delta ^= hash_value[move->piece][move->from_square] ^ hash_value[move->piece][move->to_square];
break;
}
}
}
void clear_history()
{
struct t_move_record *move = &xmove_list[0];
for (int i = 0; i < GLOBAL_MOVE_COUNT; i++, move++) {
move->history = 0;
move->refutation = NULL;
}
}