-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.c
More file actions
1830 lines (1323 loc) · 55.8 KB
/
engine.c
File metadata and controls
1830 lines (1323 loc) · 55.8 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 <stdio.h>
#include <string.h>
#include <stdlib.h>
// neagtion of unsigned bit gives 2 compliment
// LEFT SHIFT GO DOWN <<
// RIGHT SHIFT GO UP >>
// i = rank , j = file
// define bitboard data type
#define U64 unsigned long long
// FEN debug position after board, who will move, castle, enpassant, halfmove(Draw after 50 fullmove so 100), fullmove ( how many total move is made)
#define empty_board "8/8/8/8/8/8/8/8 b - - "
#define start_position "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1 "
#define tricky_position "r3k2r/p1ppqpb1/bn2pnp1/3PN3/1p2P3/2N2Q1p/PPPBBPPP/R3K2R w KQq h4 0 1 "
#define killer_position "rnbqkb1r/pp1p1pPp/8/2p1pP2/1P1P4/3P3P/P1P1P3/RNBQKBNR w Qkq e6 0 1"
#define cmk_position "r2q1rk1/ppp2ppp/2n1bn2/2b1p3/3pP3/3P1NPP/PPP1NPB1/R1BQ1RK1 b kQ c4 0 9 "
// positions
enum {
a8, b8, c8, d8, e8, f8, g8, h8,
a7, b7, c7, d7, e7, f7, g7, h7,
a6, b6, c6, d6, e6, f6, g6, h6,
a5, b5, c5, d5, e5, f5, g5, h5,
a4, b4, c4, d4, e4, f4, g4, h4,
a3, b3, c3, d3, e3, f3, g3, h3,
a2, b2, c2, d2, e2, f2, g2, h2,
a1, b1, c1, d1, e1, f1, g1, h1, no_sqr
};
// encodde pieces | upper case : WHITE | lower case : BLACK *******************************************
enum { P, N, B, R, Q, K, p, n, b, r, q, k};
// castling bits **********************************************************************
/*
0001 1 white King King can Castle to king side
0010 1 white King King can Castle tu queen side
0100 1 Black King King can Castle tu king side
1000 1 Black King King can Castle tu queen side
-----
1111 any king Kings Castle in any direction
1001 Black King, King Castle to queen side and white King can Castle to King side
*/
enum { wk = 1, wq = 2, bk = 4, bq = 8};
// sides to move
const char *square_to_coordinates[] = {
"a8", "b8", "c8", "d8", "e8", "f8", "g8", "h8",
"a7", "b7", "c7", "d7", "e7", "f7", "g7", "h7",
"a6", "b6", "c6", "d6", "e6", "f6", "g6", "h6",
"a5", "b5", "c5", "d5", "e5", "f5", "g5", "h5",
"a4", "b4", "c4", "d4", "e4", "f4", "g4", "h4",
"a3", "b3", "c3", "d3", "e3", "f3", "g3", "h3",
"a2", "b2", "c2", "d2", "e2", "f2", "g2", "h2",
"a1", "b1", "c1", "d1", "e1", "f1", "g1", "h1"
};
// ascii pieces ************************************************************************
char ascii_pieces[12] = "PNBRQKpnbrqk";
// unicode pieces
char *unicode_pieces[12] = { "♟︎", "♞", "♝", "♜", "♛", "♚", "♙", "♘", "♗", "♖", "♕", "♔"};
// "♟︎", "♞", "♝", "♜", "♛", "♚", "♙", "♘", "♗", "♖", "♕", "♔"
// convert to ascii character to encoded constants | as peer enum
int char_pieces[] = {
['P'] = P,
['N'] = N,
['B'] = B,
['R'] = R,
['Q'] = Q,
['K'] = K,
['p'] = p,
['n'] = n,
['b'] = b,
['r'] = r,
['q'] = q,
['k'] = k,
};
// promoted pieces
char promoted_pieces[] = {
[N] = 'n',
[B] = 'b',
[R] = 'r',
[Q] = 'q',
[K] = 'K',
[n] = 'n',
[b] = 'b',
[r] = 'r',
[q] = 'q',
};
// *******************************************************************************
enum {white, black, both}; // 0,1
enum {rook, bishop}; // 0,1
// DEFINE BITBORADS ************************************************************************
//define piece bit board : all 12 types of pieces
U64 bitboards[12];
//occupancies bit board : white, black, all
U64 occupancies[3];
// side to move
int side;
// init enpassant square
int enpassant = no_sqr;
// castling right variables
int castle;
// RANDOM NUMBERS
// generate pseudo random number state *************************************************************************************************
// pseudo random number state
unsigned int random_state = 1804289383;
unsigned int get_random_U32_number(){
unsigned int number = random_state; // 32 bit
number ^= number << 13;
number ^= number >> 17;
number ^= number << 5;
//update state
random_state = number;
return number;
}
// generate pseudo random number state
U64 get_random_U64_number(){
//define 4 random numbers
U64 n1, n2, n3, n4;
//init number
n1 = (U64)(get_random_U32_number()) & 0xFFFF; // slice first 16 bit
n2 = (U64)(get_random_U32_number()) & 0xFFFF; // slice first 16 bit
n3 = (U64)(get_random_U32_number()) & 0xFFFF; // slice first 16 bit
n4 = (U64)(get_random_U32_number()) & 0xFFFF; // slice first 16 bit
// return number n1 | n2 | n3 | n4
return n1 | (n2 << 16) | (n3 << 32) | (n4 << 48);
}
//generat magic number
U64 generate_magic_number(){
return (get_random_U64_number() & get_random_U64_number() & get_random_U64_number());
}
// macors Bit manipulation ***************************************************************************************************************************************
#define get_bit(BitBoard,square) ((BitBoard) & ( 1ULL << (square))) // return zero, if Bit does not exist
#define set_bit(BitBoard,position) ((BitBoard) |= (1Ull << (position))) //
#define pop_bit(BitBoard,position) (((BitBoard) &= ~(1ULL << (position))))
//count bits
#define count_bits(bitboard) __builtin_popcountll(bitboard)
// static inline int count_bits(U64 bitboard){
// int count = 0;
// while(bitboard){
// bitboard &= (bitboard - 1);
// count ++;
// }
// return count;
// }
// printBitBoard((block & -block) - 1); // get inde of first significant bit from 0
#define get_ls1b_index(bitboard) ffsll(bitboard) - 1
// INPUT & OUTPUT *****************************************************************
//func to print
void printBitBoard(U64 BitBoard){
printf("\n");
for (int i = 0; i < 8; i++)
{
printf(" %d ",8 - i);
for (int j = 0; j < 8; j++)
{
int square = i * 8 + j;
printf(" %d ", get_bit(BitBoard, square) ? 1 : 0);
}
printf("\n");
}
printf("\n a b c d e f g h \n\n");
printf(" BitBoard : %llu decimal \n",BitBoard);
}
// print board
void print_board(){
// loop over board
printf("\n");
for (int rank = 0; rank < 8; rank++)
{
printf(" %d ", 8 - rank);
for (int file = 0; file < 8; file++)
{
int square = rank * 8 + file;
//define piece var
int piece = -1;
// loop over all 12 / pieces bitboards
for (int bb_piece = P; bb_piece <= k; bb_piece++)
{
if (get_bit(bitboards[bb_piece],square))
piece = bb_piece;
}
#ifdef WIN64
printf(" %c ", (piece == -1) ? '.' : ascii_pieces[piece]);
#else
printf(" %s ", (piece == -1) ? "." : unicode_pieces[piece]);
#endif
}
printf("\n");
}
printf("\n a b c d e f g h \n\n");
// print side to move
printf(" Side : %s\n", !side ? "White" : "Black");
// print enpassant
printf(" Enpassant : %s\n", ( enpassant != no_sqr) ? square_to_coordinates[enpassant] : "No");
// print castling rights
printf(" Castling : %c%c%c%c\n\n", (castle & wk) ? 'K' : '-', (castle & wq) ? 'Q' : '-', (castle & bk) ? 'k' : '-', (castle & bq) ? 'q' : '-');
}
// parse FEN string
void parse_fen(char *fen){
//reset all board position
memset(bitboards, 0ULL, sizeof(bitboards));
memset(occupancies, 0ULL, sizeof(occupancies));
// state varaible
side = 0;
enpassant = no_sqr;
castle = 0;
// loop over board rank
for (int rank = 0; rank < 8; rank++)
{
for (int file = 0; file < 8; file++)
{
int square = rank * 8 + file;
// match ascii pieces with fen
if( (*fen >= 'a' && *fen <= 'z') || (*fen >= 'A' && *fen <= 'Z')){
//init piece type
int piece = char_pieces[*fen];
//set piece on bitboard
set_bit(bitboards[piece], square);
// increment the pointer
fen++;
}
//match number
if (*fen >= '0' && *fen <= '9'){
// init offest ( convert char '0' to int 0)
int offset = *fen - '0';
int piece = -1;
// loop over all 12 / pieces bitboards
for (int bb_piece = P; bb_piece <= k; bb_piece++)
{
// if a piece is found on square that means no decrement
if (get_bit(bitboards[bb_piece],square))
piece = bb_piece;
}
if (piece == -1) file--;
//adjust file count
file += offset;
//inc pointer
fen++;
}
if (*fen == '/')
{
fen++;
}
}
// fen++;
}
// going to side variable
fen++; // blank space
// parse side to move
side = (*fen == 'w') ? white : black;
// go to parsing castling rights
fen += 2; // char and blank space
// parsing castling rights
while (*fen != ' ')
{
switch (*fen)
{
case 'K': castle |= wk; break;
case 'Q': castle |= wq; break;
case 'k': castle |= bk; break;
case 'q': castle |= bq; break;
case '-': break;
default:
break;
}
fen++;
}
// go to enpassant
fen++;
// parse enpassant
if (*fen != '-')
{
// parse enpassant file & rank
int file = fen[0] - 'a';
int rank = 8 - (fen[1] - '0');
enpassant = rank * 8 + file;
}
else
enpassant = no_sqr;
// init white occupancies : loop over white pieces bit board
for(int piece = P; piece <= K; piece++){
//populate white occupancy
occupancies[white] |= bitboards[piece];
}
// loop over white pieces bit board
for(int piece = p; piece <= k; piece++){
//populate black occupancy
occupancies[black] |= bitboards[piece];
}
// all occupancies
occupancies[both] = occupancies[white] | occupancies[black];
}
// ATTACKS *****************************************************************************
/* Not A FILE
8 0 1 1 1 1 1 1 1
7 0 1 1 1 1 1 1 1
6 0 1 1 1 1 1 1 1
5 0 1 1 1 1 1 1 1
4 0 1 1 1 1 1 1 1
3 0 1 1 1 1 1 1 1
2 0 1 1 1 1 1 1 1
1 0 1 1 1 1 1 1 1
NOT H FILE
8 1 1 1 1 1 1 1 0
7 1 1 1 1 1 1 1 0
6 1 1 1 1 1 1 1 0
5 1 1 1 1 1 1 1 0
4 1 1 1 1 1 1 1 0
3 1 1 1 1 1 1 1 0
2 1 1 1 1 1 1 1 0
1 1 1 1 1 1 1 1 0
a b c d e f g h
NOT AB FILE
8 0 0 1 1 1 1 1 1
7 0 0 1 1 1 1 1 1
6 0 0 1 1 1 1 1 1
5 0 0 1 1 1 1 1 1
4 0 0 1 1 1 1 1 1
3 0 0 1 1 1 1 1 1
2 0 0 1 1 1 1 1 1
1 0 0 1 1 1 1 1 1
a b c d e f g h
NOT GH FILE
8 1 1 1 1 1 1 0 0
7 1 1 1 1 1 1 0 0
6 1 1 1 1 1 1 0 0
5 1 1 1 1 1 1 0 0
4 1 1 1 1 1 1 0 0
3 1 1 1 1 1 1 0 0
2 1 1 1 1 1 1 0 0
1 1 1 1 1 1 1 0 0
a b c d e f g h */
const U64 not_A_file = 18374403900871474942ULL;
const U64 not_H_file = 9187201950435737471ULL;
const U64 not_AB_file = 18229723555195321596ULL;
const U64 not_GH_file = 4557430888798830399ULL;
// a b c d e f g h
// Relevant occupancy bit count for every square on board for bishop // How many squares is a piece attacking if it's on a particular square
const int bishop_relevant_bits[64] = {
6, 5, 5, 5, 5, 5, 5, 6,
5, 5, 5, 5, 5, 5, 5, 5,
5, 5, 7, 7, 7, 7, 5, 5,
5, 5, 7, 9, 9, 7, 5, 5,
5, 5, 7, 9, 9, 7, 5, 5,
5, 5, 7, 7, 7, 7, 5, 5,
5, 5, 5, 5, 5, 5, 5, 5,
6, 5, 5, 5, 5, 5, 5, 6,
};
// Relevant occupancy bit count for every square on board for rook
const int rook_relevant_bits[64] = {
12, 11, 11, 11, 11, 11, 11, 12,
11, 10, 10, 10, 10, 10, 10, 11,
11, 10, 10, 10, 10, 10, 10, 11,
11, 10, 10, 10, 10, 10, 10, 11,
11, 10, 10, 10, 10, 10, 10, 11,
11, 10, 10, 10, 10, 10, 10, 11,
11, 10, 10, 10, 10, 10, 10, 11,
12, 11, 11, 11, 11, 11, 11, 12,
};
// rook magic number
U64 rook_magic_number[64] = {
0x8a80104000800020Ull,
0x140002000100040Ull,
0x2801880a0017001Ull,
0x100081001000420Ull,
0x200020010080420Ull,
0x3001c0002010008Ull,
0x8480008002000100Ull,
0x2080088004402900Ull,
0x800098204000Ull,
0x2024401000200040Ull,
0x100802000801000Ull,
0x120800800801000Ull,
0x208808088000400Ull,
0x2802200800400Ull,
0x2200800100020080Ull,
0x801000060821100Ull,
0x80044006422000Ull,
0x100808020004000Ull,
0x12108a0010204200Ull,
0x140848010000802Ull,
0x481828014002800Ull,
0x8094004002004100Ull,
0x4010040010010802Ull,
0x20008806104Ull,
0x100400080208000Ull,
0x2040002120081000Ull,
0x21200680100081Ull,
0x20100080080080Ull,
0x2000a00200410Ull,
0x20080800400Ull,
0x80088400100102Ull,
0x80004600042881Ull,
0x4040008040800020Ull,
0x440003000200801Ull,
0x4200011004500Ull,
0x188020010100100Ull,
0x14800401802800Ull,
0x2080040080800200Ull,
0x124080204001001Ull,
0x200046502000484Ull,
0x480400080088020Ull,
0x1000422010034000Ull,
0x30200100110040Ull,
0x100021010009Ull,
0x2002080100110004Ull,
0x202008004008002Ull,
0x20020004010100Ull,
0x2048440040820001Ull,
0x101002200408200Ull,
0x40802000401080Ull,
0x4008142004410100Ull,
0x2060820c0120200Ull,
0x1001004080100Ull,
0x20c020080040080Ull,
0x2935610830022400Ull,
0x44440041009200Ull,
0x280001040802101Ull,
0x2100190040002085Ull,
0x80c0084100102001Ull,
0x4024081001000421Ull,
0x20030a0244872Ull,
0x12001008414402Ull,
0x2006104900a0804Ull,
0x1004081002402Ull,
};
// bishop magic number
U64 bishop_magic_number[64] = {
0x40040844404084Ull,
0x2004208a004208Ull,
0x10190041080202Ull,
0x108060845042010Ull,
0x581104180800210Ull,
0x2112080446200010Ull,
0x1080820820060210Ull,
0x3c0808410220200Ull,
0x4050404440404Ull,
0x21001420088Ull,
0x24d0080801082102Ull,
0x1020a0a020400Ull,
0x40308200402Ull,
0x4011002100800Ull,
0x401484104104005Ull,
0x801010402020200Ull,
0x400210c3880100Ull,
0x404022024108200Ull,
0x810018200204102Ull,
0x4002801a02003Ull,
0x85040820080400Ull,
0x810102c808880400Ull,
0xe900410884800Ull,
0x8002020480840102Ull,
0x220200865090201Ull,
0x2010100a02021202Ull,
0x152048408022401Ull,
0x20080002081110Ull,
0x4001001021004000Ull,
0x800040400a011002Ull,
0xe4004081011002Ull,
0x1c004001012080Ull,
0x8004200962a00220Ull,
0x8422100208500202Ull,
0x2000402200300c08Ull,
0x8646020080080080Ull,
0x80020a0200100808Ull,
0x2010004880111000Ull,
0x623000a080011400Ull,
0x42008c0340209202Ull,
0x209188240001000Ull,
0x400408a884001800Ull,
0x110400a6080400Ull,
0x1840060a44020800Ull,
0x90080104000041Ull,
0x201011000808101Ull,
0x1a2208080504f080Ull,
0x8012020600211212Ull,
0x500861011240000Ull,
0x180806108200800Ull,
0x4000020e01040044Ull,
0x300000261044000aUll,
0x802241102020002Ull,
0x20906061210001Ull,
0x5a84841004010310Ull,
0x4010801011c04Ull,
0xa010109502200Ull,
0x4a02012000Ull,
0x500201010098b028Ull,
0x8040002811040900Ull,
0x28000010020204Ull,
0x6000020202d0240Ull,
0x8918844842082200Ull,
0x4010011029020020Ull,
};
//pawn attack table [ side to move ] [pawn available on current given, square]
U64 pawn_attacks[2][64];
// KNIGHT ATTACK 17,15,10,6
U64 knight_attacks[64];
// KING ATTACK
U64 king_attacks[64];
// BISHOP ATTACK mask
U64 bishop_masks[64];
// ROOK ATTACK mask
U64 rook_masks[64];
// BISHOP ATTACK table [square] [occupancy] // occupancy : 2^(highest relevant bit)
U64 bishop_attacks[64][512];
// ROOK ATTACK table [square] [occupancy] // occupancy : 2^(highest relevant bit)
U64 rook_attacks[64][4096];
// QUEEN ATTACK
U64 queen_attacks[64];
//GENERATE ATTACKS ********************************************************************************************************************
//genrate pawn attack
U64 mask_pawn_attacks(int side, int square){
// result attack bitboar
U64 attack = 0ULL;
// piece bitboard
U64 bitboard = 0ULL;
//set piece on board
set_bit(bitboard,square);
//white
if (!side){
// white
if( bitboard & not_H_file ) attack |= (bitboard >> 7);
if( bitboard & not_A_file ) attack |= (bitboard >> 9);
}else{
if( bitboard & not_H_file ) attack |= (bitboard << 9);
if( bitboard & not_A_file ) attack |= (bitboard << 7);
}
//return attack
return attack;
}
// Generate knight attack
U64 mask_knight_attacks(int square){
// result attack bitboar
U64 attack = 0ULL;
// piece bitboard
U64 bitboard = 0ULL;
//set piece on board
set_bit(bitboard,square);
if( bitboard & not_A_file ) attack |= (bitboard >> 17); //right
if( bitboard & not_AB_file ) attack |= (bitboard >> 10);
if( bitboard & not_GH_file ) attack |= (bitboard >> 6);
if( bitboard & not_H_file ) attack |= (bitboard >> 15);
if( bitboard & not_H_file ) attack |= (bitboard << 17); //left d
if( bitboard & not_GH_file ) attack |= (bitboard << 10);
if( bitboard & not_AB_file ) attack |= (bitboard << 6);
if( bitboard & not_A_file ) attack |= (bitboard << 15);
//return attack
return attack;
}
// Generate king attack
U64 mask_king_attacks(int square){
// result attack bitboar
U64 attack = 0ULL;
// piece bitboard
U64 bitboard = 0ULL;
//set piece on board
set_bit(bitboard,square);
if( bitboard & not_H_file ) attack |= (bitboard >> 7); // not h
attack |= (bitboard >> 8);
if( bitboard & not_A_file ) attack |= (bitboard >> 9); // not a
if( bitboard & not_A_file ) attack |= (bitboard >> 1); // not a
if( bitboard & not_A_file ) attack |= (bitboard << 7); // not a
attack |= (bitboard << 8);
if( bitboard & not_H_file ) attack |= (bitboard << 9); // not h
if( bitboard & not_H_file ) attack |= (bitboard << 1); // not h
//return attack
return attack;
}
// mask bishop
U64 mask_bishop_attacks(int square){
// result attack bitboar
U64 attack = 0ULL;
// init rank, file
int r,f;
//init target rank,file
int tr = square / 8;
int tf = square % 8;
//mask relevent bishop squares
for(r = tr + 1, f = tf + 1 ; r <= 6 && f <= 6 ; r++, f++ ) attack |= ( 1ULL << (r * 8 + f));
for(r = tr - 1, f = tf - 1 ; r >= 1 && f >= 1 ; r--, f-- ) attack |= ( 1ULL << (r * 8 + f));
for(r = tr + 1, f = tf - 1 ; r <= 6 && f >= 1 ; r++, f-- ) attack |= ( 1ULL << (r * 8 + f));
for(r = tr - 1, f = tf + 1 ; r >= 1 && f <= 6 ; r--, f++ ) attack |= ( 1ULL << (r * 8 + f));
//return attack
return attack;
}
// mask rook
U64 mask_rook_attacks(int square){
// result attack bitboar
U64 attack = 0ULL;
// init rank, file
int r,f;
//init target rank,file
int tr = square / 8;
int tf = square % 8;
//mask relevent bishop squares
for(r = tr + 1 ; r <= 6 ; r++) attack |= ( 1ULL << (r * 8 + tf));
for(r = tr - 1 ; r >= 1 ; r--) attack |= ( 1ULL << (r * 8 + tf));
for(f = tf + 1 ; f <= 6 ; f++) attack |= ( 1ULL << (tr * 8 + f));
for(f = tf - 1 ; f >= 1 ; f--) attack |= ( 1ULL << (tr * 8 + f));
//return attack
return attack;
}
// Generate Bishop attacks on the fly *****************************************************************************************************
U64 bishop_attacks_on_the_fly(int square, U64 block){ // block are the pieces blocking it
// result attack bitboar
U64 attack = 0ULL;
// init rank, file
int r,f;
//init target rank,file
int tr = square / 8;
int tf = square % 8;
//generate bishop attacks
for(r = tr + 1, f = tf + 1 ; r <= 7 && f <= 7 ; r++, f++ ){
attack |= ( 1ULL << (r * 8 + f));
if((1ULL << (r * 8 + f)) & block) break;
}
for(r = tr - 1, f = tf - 1 ; r >= 0 && f >= 0 ; r--, f-- ) {
attack |= ( 1ULL << (r * 8 + f));
if((1ULL << (r * 8 + f)) & block) break;
}
for(r = tr + 1, f = tf - 1 ; r <= 7 && f >= 0 ; r++, f-- ) {
attack |= ( 1ULL << (r * 8 + f));
if((1ULL << (r * 8 + f)) & block) break;
}
for(r = tr - 1, f = tf + 1 ; r >= 0 && f <= 7 ; r--, f++ ) {
attack |= ( 1ULL << (r * 8 + f));
if((1ULL << (r * 8 + f)) & block) break;
}
//return attack
return attack;
}
// Generate Bishop attacks on the fly
U64 rook_attacks_on_the_fly(int square, U64 block){
// result attack bitboar
U64 attack = 0ULL;
// init rank, file
int r,f;
//init target rank,file
int tr = square / 8;
int tf = square % 8;
//generate bishop attacks
for(r = tr + 1 ; r <= 7; r++){
attack |= ( 1ULL << (r * 8 + tf));
if((1ULL << (r * 8 + tf)) & block) break;
}
for(r = tr - 1 ; r >= 0 ; r--) {
attack |= ( 1ULL << (r * 8 + tf));
if((1ULL << (r * 8 + tf)) & block) break;
}
for(f = tf - 1 ; f >=0 ; f--) {
attack |= ( 1ULL << (tr * 8 + f));
if((1ULL << (tr * 8 + f)) & block) break;
}
for(f = tf + 1 ; f <= 7 ; f++) {
attack |= ( 1ULL << (tr * 8 + f));
if((1ULL << (tr * 8 + f)) & block) break;
}
//return attack
return attack;
}
// initialize leaper pieces attack ************************************************************************************
void init_leapers_attacks(){
for (int i = 0; i < 64; i++) // i = square
{
//init pawn attacks
pawn_attacks[white][i] = mask_pawn_attacks(white,i);
pawn_attacks[black][i] = mask_pawn_attacks(black,i);
//init knight attacks
knight_attacks[i] = mask_knight_attacks(i);
//init king attacks
king_attacks[i] = mask_king_attacks(i);
// //init bishop attacks
// bishop_attacks[i] = mask_bishop_attacks(i);
// //init rook attacks
// rook_attacks[i] = mask_rook_attacks(i);
}
}
// set occupancy // It gives all the possible occupancies on attack mask
U64 set_occupancy(int index, int bits_in_mask, U64 attack_mask){ // represent the number "index" in bit wise( 3 = 11) among the bits in attack mask
// init occupany map
U64 occupancy = 0ULL;
// Over the range of beats within attack mask
for (int count = 0; count < bits_in_mask; count++){
//get LS1B index of attack mask
int square = get_ls1b_index(attack_mask);
//pop LS1B index of attack mask
pop_bit(attack_mask,square);
//set occupancy
if (index & (1 << count)){
set_bit(occupancy,square);
}
}
//return occupancy
return occupancy;
}
// MAGIC ************************************************************************************************************************
// find appropriate magic number
U64 find_magic_number(int square, int relevant_bits, int bishop){ //relevant bits: number of sqaure, a piece can see when it is on "square"
//init occupancy
U64 occupancies[4096];
// init attack tables
U64 attacks[4096];
// init used attack tables
U64 used_attacks[4096];
// init attack mask for current piece
U64 attack_mask = bishop ? mask_bishop_attacks(square) : mask_rook_attacks(square); // moves piece can make from "sqaure"
// init occupancy indicies : 2^relevant_bits
int occupancy_indicies = 1 << relevant_bits;
//loop over occupany indicies
for (int index = 0; index < occupancy_indicies; index++)
{
//init occupancies : Store all variation of occupancy in "occupancies"
occupancies[index] = set_occupancy(index, relevant_bits, attack_mask);
//init attacks : Store all variation of attacks in "attacks"
attacks[index] = bishop ? bishop_attacks_on_the_fly(square, occupancies[index]) : rook_attacks_on_the_fly(square, occupancies[index]);
}
// test magic number loop
for (int random_count = 0; random_count < 99999999; random_count++)
{
//genrate magic num
U64 magic_number = generate_magic_number();
//skip useless/inappropriate number : less bits
if (count_bits((attack_mask * magic_number) & 0xFF00000000000000) < 6){
continue;
}
//init used attack arrays
memset(used_attacks, 0ULL, sizeof(used_attacks));
// init index and fail flag
int index, fail;
//test magic index
for(index = 0, fail = 0; !fail && index < occupancy_indicies; index++){
//init magic index
printBitBoard((occupancies[index] * magic_number) >> (64 - relevant_bits));;
int magic_index = (int)((occupancies[index] * magic_number) >> (64 - relevant_bits));
// if magic index works
if (used_attacks[magic_index] == 0ULL){
used_attacks[magic_index] = attacks[index];
}
else if (used_attacks[magic_index] != attacks[index]){ // magic index does not work
fail = 1;
}
}
if(!fail) return magic_number;
}
// if magic works
printf("Magic number fails");
return 0ULL;
}
// inint magic numbers
void init_magic_numbers(){
//loop over 64 square
for (int square = 0; square < 64; square++)
{