-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine.cpp
More file actions
1253 lines (1041 loc) · 31 KB
/
engine.cpp
File metadata and controls
1253 lines (1041 loc) · 31 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 "engine.hpp"
#include <stdlib.h>
#include <unordered_map>
#include <cmath>
#include <strings.h>
Engine::Engine()
{
init_position();
init_engine();
}
Engine::Engine(U64* board_data)
{
init_position(board_data);
init_engine();
}
void Engine::init_engine()
{
max_move_length = 200; // game depth
move_arr_size = 64; // This assumes there are only 64 possible legal moves at any one time (affects move array intilization)
board_stack = (U64*) malloc(3 * (max_move_length + 1) * sizeof(U64));
board_stack_index = 0;
move_list = (int*) malloc(move_arr_size * sizeof(int));
init_masks();
init_lsb_lookup();
fill_square_masks();
}
void Engine::reset_engine()
{
board_stack_index = 0;
pos.pass_counter = 0;
init_position();
}
void Engine::clean_up()
{
free(move_list);
free(board_stack);
free(row_mask);
free(col_mask);
free(inv_row_mask);
free(inv_col_mask);
free(diag_left_mask);
free(diag_right_mask);
}
void Engine::init_position()
{
pos.black_board = 0b0000000000000000000000000001000000001000000000000000000000000000ULL;
pos.white_board = 0b0000000000000000000000000000100000010000000000000000000000000000ULL;
}
void Engine::init_position(U64 *board_data)
{
pos.black_board = board_data[0];
pos.white_board = board_data[1];
}
void Engine::init_lsb_lookup()
{
for(int i=0; i < 64; i++)
{
lsb_lookup.insert({(U64) std::pow(2,i),i});
}
}
void Engine::fill_square_masks()
{
U64 temp;
for(int i = 0; i < 64; i++)
{
temp = 1ULL << i;
int diag = get_diag(get_rank(temp), get_file(temp));
int left_diag = diag >> 5;
int right_diag = diag & 0x000000000000000F;
square_masks[i].left_diag_mask_excluded = ~temp & diag_left_mask[left_diag];
square_masks[i].right_diag_mask_excluded = ~temp & diag_right_mask[right_diag];
square_masks[i].file_mask_excluded = ~temp & col_mask[get_file(temp)];
square_masks[i].file_mask = col_mask[get_file(temp)];
square_masks[i].rank_mask = row_mask[get_rank(temp)];
// printf("\n");
// print_bit_rep(square_masks[i].left_diag_mask_excluded);
}
}
void Engine::init_masks()
{
row_mask = (U64*) malloc(8 * sizeof(U64));
fill_row_mask_arr();
col_mask = (U64*) malloc(8 * sizeof(U64));
fill_col_mask_arr();
inv_row_mask = (U64*) malloc(8 * sizeof(U64));
inv_col_mask = (U64*) malloc(8 * sizeof(U64));
for(int i = 0; i < 8; i++)
{
inv_row_mask[i] = ~row_mask[i];
}
for(int i = 0; i < 8; i++)
{
inv_col_mask[i] = ~col_mask[i];
}
// Diag left masks start on left side and moves from left to right, top to bottom
// [0] corresponds to bottom left corner
// [0]-[7] moves up y axis along x=0
// [7] is top left corner
// [7]-[14] moves across x-axis along y=7
// [14] is top right corner
diag_left_mask = (U64*) malloc(15 * sizeof(U64));
fill_diag_left_mask_arr();
// Diag right masks start on bottom side and moves from left to right, bottom to top
// [0] corresponds to bottom right corner
// [0]-[7] moves down the x axis along y=0
// [7] is bottom left corner
// [7]-[14] moves up the y-axis along x=0
// [14] is top left corner
diag_right_mask = (U64*) malloc(15 * sizeof(U64));
fill_diag_right_mask_arr();
}
U64 Engine::make_col_mask(U64 mask)
{
for(int i = 0; i < 7; i++)
{
mask = mask | mask << 8;
}
return(mask);
}
void Engine::fill_col_mask_arr()
{
for(int i = 0; i < 8; i++)
{
col_mask[i] = make_col_mask(1ULL << i);
}
}
U64 Engine::make_row_mask(U64 mask)
{
for(int i = 0; i < 7; i++)
{
mask = mask | mask << 1;
}
return(mask);
}
void Engine::fill_row_mask_arr()
{
for(int i = 0; i < 8; i++)
{
row_mask[i] = make_row_mask(1ULL << 8*i);
}
}
U64 Engine::make_diag_left_mask(U64 mask)
{
U64 BR_mask = ~(row_mask[0] | col_mask[7]);
for(int i = 0; i < 8; i++)
{
mask = mask | ((mask & BR_mask) >> 7);
}
return(mask);
}
void Engine::fill_diag_left_mask_arr()
{
U64 start = 1;
for(int i = 0; i < 8; i++)
{
diag_left_mask[i] = make_diag_left_mask(start);
if(i != 7)
{
start = start << 8;
}
}
start = start << 1;
for(int j = 8; j < 15; j++)
{
diag_left_mask[j] = make_diag_left_mask(start);
start = start << 1;
}
}
U64 Engine::make_diag_right_mask(U64 mask)
{
U64 TR_mask = ~((row_mask[7]) | (col_mask[7]));
for(int i = 0; i < 8; i++)
{
mask = mask | ((mask & TR_mask) << 9);
}
return(mask);
}
void Engine::fill_diag_right_mask_arr()
{
U64 start = 1ULL << 7;
for(int i = 0; i < 8; i++)
{
diag_right_mask[i] = make_diag_right_mask(start);
if(i != 7)
{
start = start >> 1;
}
}
start = start << 8;
for(int j = 8; j < 15; j++)
{
diag_right_mask[j] = make_diag_right_mask(start);
start = start << 8;
}
}
int Engine::get_max_move_length()
{
return max_move_length;
}
U64 Engine::get_color(int color)
{
if(color)
{
return pos.white_board;
}
return pos.black_board;
}
std::string Engine::color_to_string(int color)
{
if(color == 1)
{
return "White";
}
else if(color == 0)
{
return "Black";
}
else
{
return "Draw";
}
}
U64 Engine::get_all()
{
return pos.white_board | pos.black_board;
}
void Engine::print_move_info(int move)
{
// std::cout << "moving " << piece_name << " from " << decode_from(move) << " to " << decode_to(move);
}
// Takes in a move to be added to the move stack
// Returns nothing
// Alters the move stack and stack_index value
void Engine::stack_push()
{
board_stack[board_stack_index] = pos.black_board;
board_stack[board_stack_index+1] = pos.white_board;
board_stack[board_stack_index+2] = pos.pass_counter;
board_stack_index += 3;
}
// Takes in nothing
// Returns the last move in the move stack
// Alters the stack_index value
void Engine::stack_pop()
{
board_stack_index -= 3;
pos.black_board = board_stack[board_stack_index];
pos.white_board = board_stack[board_stack_index+1];
pos.pass_counter = board_stack[board_stack_index+2];
}
// Takes in a move, alters the BitboardEngine's representation to the NEXT state based on the CURRENT move action
void Engine::push_white_move(int move)
{
if(move == -1)
{
pos.pass_counter++;
stack_push();
return;
}
pos.pass_counter = 0ULL;
stack_push();
U64 stone = square_to_bitboard(move);
pos.white_board = stone | pos.white_board; // put move on board
flip_white_stones(stone, move, pos.white_board, pos.black_board);
}
void Engine::push_black_move(int move)
{
if(move == -1)
{
pos.pass_counter++;
stack_push();
return;
}
pos.pass_counter = 0ULL;
stack_push();
U64 stone = square_to_bitboard(move);
pos.black_board = stone | pos.black_board; // put move on board
flip_black_stones(stone, move, pos.black_board, pos.white_board);
}
// Takes in a move, alters the BitboardEngine's representation to the PREVIOUS state based on the LAST move action
void Engine::pop_move()
{
stack_pop();
}
// void Engine::flip_stones(U64 stone, U64 own_occ, U64 opp_occ, int color)
// {
// U64 flippers = 0;
// U64 init_rook_attacks = one_rook_attacks(stone, own_occ);
// U64 init_bishop_attacks = one_bishop_attacks(stone, own_occ);
// // printf("init attacks\n");
// // print_bit_rep(init_attacks);
// U64 card_los = init_rook_attacks & own_occ;
// U64 diag_los = init_bishop_attacks & own_occ;
// U64 other_attacks;
// U64 popped_board;
// while(card_los)
// {
// popped_board = lsb_board(card_los);
// other_attacks = one_rook_attacks(popped_board, own_occ);
// card_los = card_los - popped_board;
// flippers = flippers | (other_attacks & init_rook_attacks);
// }
// while(diag_los)
// {
// popped_board = lsb_board(diag_los);
// other_attacks = one_bishop_attacks(popped_board, own_occ);
// diag_los = diag_los - popped_board;
// flippers = flippers | (other_attacks & init_bishop_attacks);
// }
// // printf("\nflippers\n");
// // print_bit_rep(flippers);
// pos.board[color] = pos.board[color] | (flippers & opp_occ);
// pos.board[1-color] = pos.board[1-color] & ~flippers;
// }
// void Engine::flip_stones(U64 stone, U64 own_occ, U64 opp_occ, int color)
// {
// U64 flippers = 0;
// U64 inv_opp_occ = ~opp_occ;
// U64 init_rook_attacks = one_rook_attacks(stone, inv_opp_occ);
// U64 init_bishop_attacks = one_bishop_attacks(stone, inv_opp_occ);
// U64 card_los = init_rook_attacks & own_occ;
// U64 diag_los = init_bishop_attacks & own_occ;
// // other_attacks = all_rook_attacks(card_los, ~own_occ);
// flippers |= all_rook_attacks(card_los, opp_occ) & init_rook_attacks;
// // other_attacks = all_bishop_attacks(diag_los, ~own_occ);
// flippers |= all_bishop_attacks(diag_los, opp_occ) & init_bishop_attacks;
// pos.board[color] |= flippers & opp_occ;
// pos.board[1-color] &= ~flippers;
// }
void Engine::flip_white_stones(U64 stone, int square, U64 own_occ, U64 opp_occ)
{
U64 flippers = 0;
U64 inv_opp_occ = ~opp_occ;
// U64 init_rook_attacks = one_rook_attacks(stone, inv_opp_occ, square);
U64 init_rook_attacks = all_rook_attacks(stone, opp_occ);
U64 init_bishop_attacks = one_bishop_attacks(stone, inv_opp_occ);
U64 card_los = init_rook_attacks & own_occ;
U64 diag_los = init_bishop_attacks & own_occ;
// other_attacks = all_rook_attacks(card_los, ~own_occ);
flippers |= all_rook_attacks(card_los, opp_occ) & init_rook_attacks;
// other_attacks = all_bishop_attacks(diag_los, ~own_occ);
flippers |= all_bishop_attacks(diag_los, opp_occ) & init_bishop_attacks;
pos.white_board |= flippers & opp_occ;
pos.black_board &= ~flippers;
}
void Engine::flip_black_stones(U64 stone, int square, U64 own_occ, U64 opp_occ)
{
U64 flippers = 0;
U64 inv_opp_occ = ~opp_occ;
// U64 init_rook_attacks = one_rook_attacks(stone, inv_opp_occ, square);
U64 init_rook_attacks = all_rook_attacks(stone, opp_occ);
U64 init_bishop_attacks = one_bishop_attacks(stone, inv_opp_occ);
U64 card_los = init_rook_attacks & own_occ;
U64 diag_los = init_bishop_attacks & own_occ;
// other_attacks = all_rook_attacks(card_los, ~own_occ);
flippers |= all_rook_attacks(card_los, opp_occ) & init_rook_attacks;
// other_attacks = all_bishop_attacks(diag_los, ~own_occ);
flippers |= all_bishop_attacks(diag_los, opp_occ) & init_bishop_attacks;
pos.black_board |= flippers & opp_occ;
pos.white_board &= ~flippers;
}
void Engine::print_bit_rep(U64 board)
{
int shifter;
U64 to_print = horizontal_flip(board);
U64 row;
for(int i = 7; i > -1; i--)
{
shifter = i * 8;
row = (to_print & row_mask[i]) >> shifter;
std::bitset<8> one_row(row);
std::cout << one_row << std::endl;
}
}
void Engine::print_char()
{
char* b = (char*) malloc(8*8*sizeof(char));
for(int i = 0; i < 8; i++)
{
for(int j = 0; j < 8; j++)
{
b[j+i*8] = '-';
}
}
U64 p;
U64 one_p;
p = horizontal_flip(pos.white_board); // white
while(p)
{
one_p = lsb_board(p);
p = p & ~one_p;
b[(7-get_file(one_p))+8*(7-get_rank(one_p))] = 'W';
}
p = horizontal_flip(pos.black_board); // black
while(p)
{
one_p = lsb_board(p);
p = p & ~one_p;
b[(7-get_file(one_p))+8*(7-get_rank(one_p))] = 'B';
}
for(int i = 0; i < 8; i++)
{
char* tmp = (char *) calloc(30, sizeof(char));
sprintf(tmp, "%d", 7-i);
std::cout << tmp << " ";
free(tmp);
for(int j = 0; j < 8; j++)
{
std::cout << b[j+i*8] << " ";
}
std::cout << std::endl;
}
std::cout << " A B C D E F G H" << std::endl;
free(b);
}
// East << 1
// Southeast >> 7
// South >> 8
// Southwest >> 9
// West >> 1
// Northwest << 7
// North << 8
// Northeast << 9
int Engine::bitboard_to_square(U64 piece)
{
return(lsb_digit(piece));
}
U64 Engine::square_to_bitboard(int square)
{
return(1ULL << square);
}
// doesnt take into account passing, or whos turn, maybe generate random val for passing and turn?
U64 Engine::hash_board()
{
// maybe just ~ the black board each time?
return pos.black_board | pos.white_board;
}
// Generates and returns a list of legal moves for a color
int* Engine::generate_white_moves()
{
move_list[0] = 0; // encode index in array
U64 possible_moves = cardinal_white_moves() | diag_white_moves();
U64 temp;
while(possible_moves)
{
temp = lsb_board(possible_moves);
move_list[move_list[0]+1] = bitboard_to_square(temp);
move_list[0]++;
possible_moves = possible_moves - temp;
}
return move_list;
}
int* Engine::generate_black_moves()
{
move_list[0] = 0; // encode index in array
U64 possible_moves = cardinal_black_moves() | diag_black_moves();
U64 temp;
while(possible_moves)
{
temp = lsb_board(possible_moves);
move_list[move_list[0]+1] = bitboard_to_square(temp);
move_list[0]++;
possible_moves = possible_moves - temp;
}
return move_list;
}
// int Engine::score_board()
// {
// int total = 0;
// U64 temp;
// U64 white = pos.white_board;
// U64 black = pos.black_board;
// while(white)
// {
// temp = lsb_board(white);
// total++;
// white = white - temp;
// }
// while(black)
// {
// temp = lsb_board(black);
// total--;
// black = black - temp;
// }
// return total;
// }
// alternative method to scoring (probably faster)
// http://chessprogramming.wikispaces.com/Population+Count#SWAR-Popcount-The%20PopCount%20routine
int Engine::score_board()
{
U64 x = pos.white_board;
x = x - ((x >> 1) & k1); /* put count of each 2 bits into those 2 bits */
x = (x & k2) + ((x >> 2) & k2); /* put count of each 4 bits into those 4 bits */
x = (x + (x >> 4)) & k4 ; /* put count of each 8 bits into those 8 bits */
x = (x * kf) >> 56; /* returns 8 most significant bits of x + (x<<8) + (x<<16) + (x<<24) + ... */
U64 y = pos.black_board;
y = y - ((y >> 1) & k1); /* put count of each 2 bits into those 2 bits */
y = (y & k2) + ((y >> 2) & k2); /* put count of each 4 bits into those 4 bits */
y = (y + (y >> 4)) & k4 ; /* put count of each 8 bits into those 8 bits */
y = (y * kf) >> 56; /* returns 8 most significant bits of x + (x<<8) + (x<<16) + (x<<24) + ... */
return (int) x - (int) y;
}
int Engine::get_winner()
{
int score = score_board();
if(score == 0)
{
return 2;
}
return score > 0;
}
// 0 for not over
// other numerals centered around 100
int Engine::is_not_terminal(int* move_list, int color)
{
if(move_list[0] == 0)
{
if(pos.pass_counter == 2ULL)
{
return 0;
}
}
return 1;
}
int Engine::is_terminal(int* move_list, int color)
{
if(move_list[0] == 0)
{
if(pos.pass_counter == 2ULL)
{
return 1;
}
}
return 0;
}
U64 Engine::one_rook_attacks(U64 rook, U64 o, int square)
{
U64 o_rev = reverse_64_bits(o);
U64 s_rev = reverse_64_bits(rook);
U64 hori = (o - 2*rook) ^ reverse_64_bits(o_rev - 2*s_rev);
hori = hori & square_masks[square].rank_mask;
U64 o_mask = o & square_masks[square].file_mask;
U64 o_rev_mask = reverse_64_bits(o_mask);
U64 vert = (o_mask - 2*rook) ^ reverse_64_bits(o_rev_mask - 2*s_rev);
vert = vert & square_masks[square].file_mask;
return(hori | vert);
}
U64 Engine::one_bishop_attacks_ANTI(U64 bishop, int square, U64 occ)
{
U64 line_mask = square_masks[square].right_diag_mask_excluded; // excludes square of slider
U64 forward = occ & line_mask; // also performs the first subtraction by clearing the s in o
U64 reverse = vertical_flip(forward); // o'-s'
forward = forward - bishop; // o -2s
reverse = reverse - vertical_flip(bishop); // o'-2s'
forward = forward ^ vertical_flip(reverse);
return forward & line_mask; // mask the line again
}
U64 Engine::one_bishop_attacks(U64 bishop, U64 occ)
{
int square = bitboard_to_square(bishop);
// printf("you are being called\n");
U64 line_mask = square_masks[square].left_diag_mask_excluded; // excludes square of slider
U64 forward = occ & line_mask; // also performs the first subtraction by clearing the s in o
U64 reverse = vertical_flip(forward); // o'-s'
forward = forward - bishop; // o -2s
reverse = reverse - vertical_flip(bishop); // o'-2s'
forward = forward ^ vertical_flip(reverse);
U64 attks = one_bishop_attacks_ANTI(bishop, square, occ) | (forward & line_mask);
return attks; // mask the line again
}
// U64 Engine::one_rook_attacks(U64 rook, U64 occ)
// {
// int square = bitboard_to_square(rook);
// U64 forward, reverse;
// forward = occ & square_masks[square].file_mask_excluded;
// // print_bit_rep(occ);
// // printf("mask\n");
// // print_bit_rep(square_masks[square].file_mask_excluded);
// printf("occ\n");
// print_bit_rep(occ);
// printf("\n");
// reverse = vertical_flip(forward);
// forward -= rook;
// reverse -= vertical_flip(rook);
// forward ^= vertical_flip(reverse);
// forward &= square_masks[square].file_mask_excluded;
// print_bit_rep(forward);
// exit(0);
// return forward;
// }
// Takes in a 64 bit number with single bit
// Returns the rank piece is on 0-7, bottom to top
int Engine::get_rank(U64 num)
{
U64 max0 = 128ULL; // 2^7
if(num <= max0)
{
return 0;
}
U64 max1 = 32768ULL; // 2^15
if(num <= max1)
{
return 1;
}
U64 max2 = 8388608ULL; // 2^23
if(num <= max2)
{
return 2;
}
U64 max3 = 2147483648ULL; // 2^31
if(num <= max3)
{
return 3;
}
U64 max4 = 549755813888ULL; // 2^39
if(num <= max4)
{
return 4;
}
U64 max5 = 140737488355328ULL; // 2^47
if(num <= max5)
{
return 5;
}
U64 max6 = 36028797018963968ULL; // 2^55
if(num <= max6)
{
return 6;
}
U64 max7 = 9223372036854775808ULL; // 2^63
if(num <= max7)
{
return 7;
}
return -1;
}
// Takes in a 64 bit number with single bit
// Returns the file piece is on 0-7, left to right
int Engine::get_file(U64 num)
{
switch(num)
{
//2^[0, 8, 16, 24, 32, 40, 48, 56]
case 1ULL:
case 256ULL:
case 65536ULL:
case 16777216ULL:
case 4294967296ULL:
case 1099511627776ULL:
case 281474976710656ULL:
case 72057594037927936ULL:
return 0;
//2^[1,9,17,25,33,41,49,57]
case 2ULL:
case 512ULL:
case 131072ULL:
case 33554432ULL:
case 8589934592ULL:
case 2199023255552ULL:
case 562949953421312ULL:
case 144115188075855872ULL:
return 1;
// 2^[2,10,18,26,34,42,50,58]
case 4ULL:
case 1024ULL:
case 262144ULL:
case 67108864ULL:
case 17179869184ULL:
case 4398046511104ULL:
case 1125899906842624ULL:
case 288230376151711744ULL:
return 2;
// 2^[3,11,19,27,35,43,51,59]
case 8ULL:
case 2048ULL:
case 524288ULL:
case 134217728ULL:
case 34359738368ULL:
case 8796093022208ULL:
case 2251799813685248ULL:
case 576460752303423488ULL:
return 3;
// 2^[4,12,20,28,36,44,52,60]
case 16ULL:
case 4096ULL:
case 1048576ULL:
case 268435456ULL:
case 68719476736ULL:
case 17592186044416ULL:
case 4503599627370496ULL:
case 1152921504606846976ULL:
return 4;
// 2^[5,13,21,29,37,45,53,61]
case 32ULL:
case 8192ULL:
case 2097152ULL:
case 536870912ULL:
case 137438953472ULL:
case 35184372088832ULL:
case 9007199254740992ULL:
case 2305843009213693952ULL:
return 5;
// 2^[6, 14, 22, 30, 38, 46, 54, 62]
case 64ULL:
case 16384ULL:
case 4194304ULL:
case 1073741824ULL:
case 274877906944ULL:
case 70368744177664ULL:
case 18014398509481984ULL:
case 4611686018427387904ULL:
return 6;
// 2^[7,15,23,31,39,47,55,63]
case 128ULL:
case 32768ULL:
case 8388608ULL:
case 2147483648ULL:
case 549755813888ULL:
case 140737488355328ULL:
case 36028797018963968ULL:
case 9223372036854775808ULL:
return 7;
default:
return -1;
}
}
int Engine::get_diag(int rank, int file)
{
int total_val = rank+file;
int right;
//Total val also equals left diag index
if(rank > file) //Above the middle diagonal line r = 7
{
right = 7+(total_val-2*file);
}
else //Below middle line
{
right = 7-(total_val-2*rank);
}
return (total_val << 5) | right;
}
// Takes in a bitboard and will return the bitboard representing only the least significant bit.
// therefore simply return ((lots of zeros)00000000000010)
// YOU MAY ASSUME A 1 EXISTS, (0000000000000000000) will not be given
#ifdef __linux__ // much faster than WIN32
int Engine::lsb_digit(unsigned long long board)
{
// return(ffsll(board)); // i think only works on linux
return __builtin_ffsll (board) - 1;
}
#else
int Engine::lsb_digit(unsigned long long board)
{
return(lsb_lookup[lsb_board(board)]);
}
#endif
// Takes in a bitboard
// Returns a bitboard with soley the least significant bit = 1
// All other bits = 0
U64 Engine::lsb_board(U64 board)
{
return(board & (-board));
}
// See above, except return the move_list significant bit bitboard
U64 Engine::msb_board(U64 board)
{
U64 sol = pow(2, msb_digit(board));
return(sol);
}
// See above, except return the move_list significant bit bitboard
// Returns the index (right = 0 left = 63) corresponding to the most significant 1
// Probably a speed up to be had by checking if its above 32 bit
U64 Engine::msb_digit(U64 board)
{
uint32_t first = board >> 32;
uint32_t second = board & 0xFFFFFFFFLL;
if(first == 0)
return(63-(32 + __builtin_clz(second)));
else
return(63-__builtin_clz(first));
}
// https://stackoverflow.com/questions/25802605/what-does-performing-a-byteswap-mean
U64 Engine::reverse_8_bits(U64 x)
{
return (x * 0x0202020202 & 0x010884422010) % 1023;
}
U64 Engine::reverse_64_bits(U64 x)
{
return vertical_flip(horizontal_flip(x));
}
U64 Engine::horizontal_flip(U64 x)
{
x = ((x >> 1) & 0x5555555555555555) + 2 * (x & 0x5555555555555555);
x = ((x >> 2) & 0x3333333333333333) + 4 * (x & 0x3333333333333333);
x = ((x >> 4) & 0x0f0f0f0f0f0f0f0f) + 16 * (x & 0x0f0f0f0f0f0f0f0f);
return x;
}
U64 Engine::vertical_flip(U64 x)
{
return __builtin_bswap64(x);
}
// move gen
// U64 Engine::cardinal_moves(int color)
// {
// return north_moves(get_color(color), get_color(1-color), ~get_all()) | south_moves(get_color(color), get_color(1-color), ~get_all()) |
// east_moves(get_color(color), get_color(1-color), ~get_all()) | west_moves(get_color(color), get_color(1-color), ~get_all());
// }
U64 Engine::cardinal_white_moves()
{
U64 inv_get_all = ~get_all();
return north_moves(pos.white_board, pos.black_board, inv_get_all) | south_moves(pos.white_board, pos.black_board, inv_get_all) |
east_moves(pos.white_board, pos.black_board, inv_get_all) | west_moves(pos.white_board, pos.black_board, inv_get_all);
}
U64 Engine::cardinal_black_moves()
{
U64 inv_get_all = ~get_all();
return north_moves(pos.black_board, pos.white_board, inv_get_all) | south_moves(pos.black_board, pos.white_board, inv_get_all) |
east_moves(pos.black_board, pos.white_board, inv_get_all) | west_moves(pos.black_board, pos.white_board, inv_get_all);
}
U64 Engine::north_moves(U64 mine, U64 prop, U64 empty)
{
U64 moves = prop & (mine << 8);
moves |= prop & (moves << 8);
moves |= prop & (moves << 8);
moves |= prop & (moves << 8);
moves |= prop & (moves << 8);
moves |= prop & (moves << 8);
return empty & (moves << 8);