-
Notifications
You must be signed in to change notification settings - Fork 0
/
chess.c
1742 lines (1614 loc) · 49.2 KB
/
chess.c
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 <stdlib.h>
#include <string.h>
typedef unsigned long long U64;
#define EMPTY 0b00000
#define BORDER 0b00001
#define WHITE 0b00010
#define BLACK 0b00011
#define PAWN 0b00100
#define KNIGHT 0b01000
#define BISHOP 0b01100
#define ROOK 0b10000
#define QUEEN 0b10100
#define KING 0b11000
#define create_figure(color, value) (color | value)
#define get_color(figure) (figure & 0b00011)
#define get_value(figure) (figure & 0b11100)
#define K_castling 0b1000
#define Q_castling 0b0100
#define k_castling 0b0010
#define q_castling 0b0001
#define make_K_castling_is_incorrect() ply->castlings &= 0b0111
#define make_Q_castling_is_incorrect() ply->castlings &= 0b1011
#define make_k_castling_is_incorrect() ply->castlings &= 0b1101
#define make_q_castling_is_incorrect() ply->castlings &= 0b1110
#define make_white_castlings_is_incorrect() ply->castlings &= 0b0011
#define make_black_castlings_is_incorrect() ply->castlings &= 0b1100
#define not_turn_to_move (turn_to_move ^ 1)
#define DRAW 0
#define LOSING -300000
int turn_figures[4] = {QUEEN, ROOK, BISHOP, KNIGHT};
int directions_of_rook[4] = {1, 10, -1, -10};
int directions_of_bishop[4] = {9, 11, -9, -11};
int directions_of_queen[8] = {1, 10, -1, -10, 9, 11, -9, -11};
int moves_of_king[8] = {1, 10, -1, -10, 9, 11, -9, -11};
int moves_of_knight[8] = {21, 19, 12, 8, -21, -19, -12, -8};
#define A1 91
#define B1 92
#define C1 93
#define D1 94
#define E1 95
#define F1 96
#define G1 97
#define H1 98
#define A2 81
#define B2 82
#define C2 83
#define D2 84
#define E2 85
#define F2 86
#define G2 87
#define H2 88
#define A3 71
#define B3 72
#define C3 73
#define D3 74
#define E3 75
#define F3 76
#define G3 77
#define H3 78
#define A4 61
#define B4 62
#define C4 63
#define D4 64
#define E4 65
#define F4 66
#define G4 67
#define H4 68
#define A5 51
#define B5 52
#define C5 53
#define D5 54
#define E5 55
#define F5 56
#define G5 57
#define H5 58
#define A6 41
#define B6 42
#define C6 43
#define D6 44
#define E6 45
#define F6 46
#define G6 47
#define H6 48
#define A7 31
#define B7 32
#define C7 33
#define D7 34
#define E7 35
#define F7 36
#define G7 37
#define H7 38
#define A8 21
#define B8 22
#define C8 23
#define D8 24
#define E8 25
#define F8 26
#define G8 27
#define H8 28
int board64[64] = {
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,
};
int board[120];
int place_of_white_king, place_of_black_king;
int turn_to_move;
struct _flags {
int en_passant;
int castlings;
int number_of_insignificant_plies;
U64 hash;
} begin_ply[1000], *ply;
void print_position(int is_reversed)
{
for(int i = 0; i < 8; i += 1)
{
for(int j = 0; j < 8; j += 1)
{
int index = is_reversed ? (7 - i) * 8 + 7 - j: i * 8 + j;
switch(board[board64[index]])
{
case EMPTY: printf(".");
break; case create_figure(WHITE, KING) : printf("K");
break; case create_figure(WHITE, QUEEN) : printf("Q");
break; case create_figure(WHITE, ROOK) : printf("R");
break; case create_figure(WHITE, BISHOP): printf("B");
break; case create_figure(WHITE, KNIGHT): printf("N");
break; case create_figure(WHITE, PAWN) : printf("P");
break; case create_figure(BLACK, KING) : printf("k");
break; case create_figure(BLACK, QUEEN) : printf("q");
break; case create_figure(BLACK, ROOK) : printf("r");
break; case create_figure(BLACK, BISHOP): printf("b");
break; case create_figure(BLACK, KNIGHT): printf("n");
break; case create_figure(BLACK, PAWN) : printf("p");
}
}
printf("\n");
}
printf("\n");
}
typedef int Move;
#define create_move(from, to, broken, turn) (from | (to << 7) | (broken << 14) | (turn << 19))
#define move_from(move) (move & 0b1111111)
#define move_to(move) ((move >> 7) & 0b1111111)
#define move_broken(move) ((move >> 14) & 0b11111)
#define move_turn(move) ((move >> 19) & 0b11111)
U64 zobrist_piecesquare[32][120];
U64 zobrist_color;
U64 zobrist_castlings[16];
U64 zobrist_en_passant[120];
U64 rand64()
{
return rand() ^ ((U64)rand() << 15) ^ ((U64)rand() << 30) ^ ((U64)rand() << 45) ^ ((U64)rand() << 60);
}
void zobrist_init()
{
for(int i = 0; i < 32; i += 1)
for(int j = 0; j < 120; j += 1)
zobrist_piecesquare[i][j] = rand64();
zobrist_color = rand64();
for(int i = 0; i < 16; i += 1)
zobrist_castlings[i] = rand64();
for(int i = 0; i < 120; i += 1)
zobrist_en_passant[i] = rand64();
}
#define hash_table_size 1048575 /*(1 << 20) - 1*/
typedef struct
{
U64 hash;
int depth;
int eval;
Move bestmove;
int flag;
} Entry;
Entry hash_table[hash_table_size + 1];
#define LESS_THAN_ALPHA 0
#define BETWEEN_ALPHA_AND_BETA 1
#define MORE_THAN_BETA 2
void hash_save_entry(int depth, int eval, Move bestmove, int flag)
{
Entry *entry = &hash_table[ply->hash & hash_table_size];
if(!(entry->hash == ply->hash && entry->depth > depth &&
entry->flag == BETWEEN_ALPHA_AND_BETA))
{
entry->hash = ply->hash;
entry->depth = depth;
entry->eval = eval;
entry->bestmove = bestmove;
entry->flag = flag;
}
}
Entry* hash_get_entry()
{
Entry *entry = &hash_table[ply->hash & hash_table_size];
if(entry->hash == ply->hash)
return entry;
return NULL;
}
void setup_hash()
{
ply->hash = 0;
if(turn_to_move == WHITE) ply->hash ^= zobrist_color;
for(int i = 0; i < 64; i += 1)
{
int place = board64[i];
int figure = board[place];
if(figure != EMPTY)
ply->hash ^= zobrist_piecesquare[figure][place];
}
ply->hash ^= zobrist_castlings[ply->castlings];
ply->hash ^= zobrist_en_passant[ply->en_passant];
}
#define start_fen "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1"
void setup_position(char* fen)
{
ply = begin_ply;
turn_to_move = WHITE;
ply->en_passant = 0;
ply->castlings = 0;
ply->number_of_insignificant_plies = 0;
for(int i = 0; i < 120; i += 1) board[i] = BORDER;
for(int i = 0; i < 64; i += 1) board[board64[i]] = EMPTY;
int i = 0;
int current_cell = 0;
for(; fen[i] != ' ' && fen[i] != '\0'; i += 1, current_cell += 1)
{
int index_of_figure = board64[current_cell];
switch(fen[i])
{
case '/':
current_cell -= 1;
break; case 'K':
place_of_white_king = index_of_figure;
board[index_of_figure] = create_figure(WHITE, KING);
break; case 'Q':
board[index_of_figure] = create_figure(WHITE, QUEEN);
break; case 'R':
board[index_of_figure] = create_figure(WHITE, ROOK);
break; case 'B':
board[index_of_figure] = create_figure(WHITE, BISHOP);
break; case 'N':
board[index_of_figure] = create_figure(WHITE, KNIGHT);
break; case 'P':
board[index_of_figure] = create_figure(WHITE, PAWN);
break; case 'k':
place_of_black_king = index_of_figure;
board[index_of_figure] = create_figure(BLACK, KING);
break; case 'q':
board[index_of_figure] = create_figure(BLACK, QUEEN);
break; case 'r':
board[index_of_figure] = create_figure(BLACK, ROOK);
break; case 'b':
board[index_of_figure] = create_figure(BLACK, BISHOP);
break; case 'n':
board[index_of_figure] = create_figure(BLACK, KNIGHT);
break; case 'p':
board[index_of_figure] = create_figure(BLACK, PAWN);
break; default:
current_cell += fen[i] - '0' - 1;
}
}
if(fen[i] == '\0')
{
setup_hash();
return;
}
i += 1;
if(fen[i] == 'w') turn_to_move = WHITE;
else if(fen[i] == 'b') turn_to_move = BLACK;
i += 1;
if(fen[i] == '\0')
{
setup_hash();
return;
}
i += 1;
for(; fen[i] != ' ' && fen[i] != '\0'; i += 1)
{
switch(fen[i])
{
case 'K': ply->castlings |= K_castling; break;
case 'Q': ply->castlings |= Q_castling; break;
case 'k': ply->castlings |= k_castling; break;
case 'q': ply->castlings |= q_castling; break;
}
}
if(fen[i] == '\0')
{
setup_hash();
return;
}
i += 1;
if(fen[i] == '-') i += 1;
else
{
ply->en_passant = ('8' - fen[i + 1] + 2) * 10 + fen[i] - 'a' + 1;
i += 2;
}
if(fen[i] == '\0')
{
setup_hash();
return;
}
i += 1;
if(fen[i + 1] == ' ' || fen[i + 1] == '\0')
ply->number_of_insignificant_plies = fen[i] - '0';
else
ply->number_of_insignificant_plies = ((fen[i] - '0') * 10 + fen[i + 1] - '0');
setup_hash();
}
void make_null_move()
{
turn_to_move = not_turn_to_move;
ply += 1;
ply->castlings = (ply - 1)->castlings;
ply->en_passant = 0;
ply->number_of_insignificant_plies = (ply - 1)->number_of_insignificant_plies;
ply->hash = (ply - 1)->hash ^ zobrist_color;
if((ply - 1)->en_passant)
ply->hash ^= zobrist_en_passant[(ply - 1)->en_passant];
}
void unmake_null_move()
{
turn_to_move = not_turn_to_move;
ply -= 1;
}
int in_check(int turn_to_move)
{
int direction_of_pawns = turn_to_move == WHITE? -10: 10;
int captures_of_pawns[2] = {direction_of_pawns + 1, direction_of_pawns - 1};
int place_of_king = turn_to_move == WHITE? place_of_white_king:
place_of_black_king;
for(int i = 0; i < 2; i += 1)
{
int tmp = place_of_king + captures_of_pawns[i];
if(board[tmp] == create_figure(not_turn_to_move, PAWN))
return 1;
}
for(int i = 0; i < 8; i += 1)
{
int tmp = place_of_king + moves_of_king[i];
if(board[tmp] == create_figure(not_turn_to_move, KING))
return 1;
}
for(int i = 0; i < 8; i += 1)
{
int tmp = place_of_king + moves_of_knight[i];
if(board[tmp] == create_figure(not_turn_to_move, KNIGHT))
return 1;
}
for(int i = 0; i < 4; i += 1)
{
int inc = directions_of_bishop[i];
int x = place_of_king + inc;
while(board[x] == EMPTY)
x += inc;
if(board[x] == create_figure(not_turn_to_move, QUEEN) ||
board[x] == create_figure(not_turn_to_move, BISHOP))
return 1;
}
for(int i = 0; i < 4; i += 1)
{
int inc = directions_of_rook[i];
int x = place_of_king + inc;
while(board[x] == EMPTY)
x += inc;
if(board[x] == create_figure(not_turn_to_move, QUEEN) ||
board[x] == create_figure(not_turn_to_move, ROOK))
return 1;
}
return 0;
}
void make_move(Move move)
{
U64 hash = ply->hash;
int direction_of_pawns = turn_to_move == WHITE? -10: 10;
int from = move_from(move);
int figure = board[from];
int to = move_to(move);
int turn = move_turn(move);
int broken = move_broken(move);
board[from] = EMPTY;
hash ^= zobrist_piecesquare[figure][from];
if(turn)
{
board[to] = turn;
hash ^= zobrist_piecesquare[turn][to];
}
else
{
board[to] = figure;
hash ^= zobrist_piecesquare[figure][to];
}
if(get_value(figure) == PAWN && to == ply->en_passant)
{
int tmp = to - direction_of_pawns;
board[tmp] = EMPTY;
hash ^= zobrist_piecesquare[create_figure(not_turn_to_move, PAWN)][tmp];
}
else if(broken)
{
hash ^= zobrist_piecesquare[broken][to];
}
if(ply->en_passant)
{
hash ^= zobrist_en_passant[ply->en_passant];
}
ply += 1;
if(get_value(figure) == PAWN && to - from == direction_of_pawns * 2)
{
ply->en_passant = from + direction_of_pawns;
hash ^= zobrist_en_passant[ply->en_passant];
}
else
{
ply->en_passant = 0;
}
ply->castlings = (ply - 1)->castlings;
if(get_value(figure) == KING)
{
int horizontal;
if(turn_to_move == WHITE)
{
place_of_white_king = to;
make_white_castlings_is_incorrect();
horizontal = 90;
}
else
{
place_of_black_king = to;
make_black_castlings_is_incorrect();
horizontal = 20;
}
if(from - to == 2)
{
board[horizontal + 4] = board[horizontal + 1];
board[horizontal + 1] = EMPTY;
}
else if(to - from == 2)
{
board[horizontal + 6] = board[horizontal + 8];
board[horizontal + 8] = EMPTY;
}
}
if(board[98] != create_figure(WHITE, ROOK))
make_K_castling_is_incorrect();
if(board[91] != create_figure(WHITE, ROOK))
make_Q_castling_is_incorrect();
if(board[28] != create_figure(BLACK, ROOK))
make_k_castling_is_incorrect();
if(board[21] != create_figure(BLACK, ROOK))
make_q_castling_is_incorrect();
if(broken || get_value(figure) == PAWN)
ply->number_of_insignificant_plies = 0;
else
ply->number_of_insignificant_plies =
(ply - 1)->number_of_insignificant_plies + 1;
turn_to_move = not_turn_to_move;
hash ^= zobrist_color;
hash ^= zobrist_castlings[(ply - 1)->castlings];
hash ^= zobrist_castlings[ply->castlings];
ply->hash = hash;
}
void unmake_move(Move move)
{
turn_to_move = not_turn_to_move;
ply -= 1;
int direction_of_pawns = turn_to_move == WHITE? -10: 10;
int from = move_from(move);
int to = move_to(move);
int figure = board[move_to(move)];
int broken = move_broken(move);
int turn = move_turn(move);
if(turn)
board[from] = create_figure(turn_to_move, PAWN);
else
board[from] = figure;
if(get_value(figure) == PAWN && to == ply->en_passant)
{
board[to] = EMPTY;
board[to - direction_of_pawns] = create_figure(not_turn_to_move, PAWN);
}
else
board[to] = broken;
if(get_value(figure) == KING)
{
int horizontal;
if(turn_to_move == WHITE)
{
place_of_white_king = from;
horizontal = 90;
}
else
{
place_of_black_king = from;
horizontal = 20;
}
if(from - to == 2)
{
board[horizontal + 1] = board[horizontal + 4];
board[horizontal + 4] = EMPTY;
}
else if(to - from == 2)
{
board[horizontal + 8] = board[horizontal + 6];
board[horizontal + 6] = EMPTY;
}
}
}
int generate_moves(Move *movelist)
{
int horizontal2 = turn_to_move == WHITE? 8 : 3;
int horizontal7 = turn_to_move == WHITE? 3 : 8;
int direction_of_pawns = turn_to_move == WHITE? -10: 10;
int captures_of_pawns[2] = {direction_of_pawns + 1, direction_of_pawns - 1};
int place_of_king = turn_to_move == WHITE? place_of_white_king:
place_of_black_king;
int n = 0;
int king_castling, queen_castling;
if(turn_to_move == WHITE)
{
king_castling = ply->castlings & K_castling;
queen_castling = ply->castlings & Q_castling;
}
else
{
king_castling = ply->castlings & k_castling;
queen_castling = ply->castlings & q_castling;
}
int is_in_check = in_check(turn_to_move);
for(int i = 0; i < 8; i += 1)
{
int i_move = moves_of_king[i];
int tmp = place_of_king + i_move;
if(board[tmp] == EMPTY || get_color(board[tmp]) == not_turn_to_move)
{
int i_move_is_possible = 0;
Move tmp_move = create_move(place_of_king, tmp, board[tmp], 0);
make_move(tmp_move);
if(!in_check(not_turn_to_move))
{
i_move_is_possible = 1;
movelist[n] = tmp_move;
n += 1;
}
unmake_move(tmp_move);
if(is_in_check || !i_move_is_possible || board[tmp] != EMPTY)
continue;
if(i_move == 1 && king_castling && board[place_of_king + 2] == EMPTY)
{
movelist[n] = create_move(place_of_king, place_of_king + 2, 0, 0);
n += 1;
}
if(i_move == -1 && queen_castling &&
board[place_of_king - 2] == EMPTY &&
board[place_of_king - 3] == EMPTY)
{
movelist[n] = create_move(place_of_king, place_of_king - 2, 0, 0);
n += 1;
}
}
}
if(ply->en_passant)
{
for(int i = 0; i < 2; i += 1)
{
int tmp = ply->en_passant - captures_of_pawns[i];
if(board[tmp] == create_figure(turn_to_move, PAWN))
{
movelist[n] = create_move(tmp, ply->en_passant, 0, 0);
n += 1;
}
}
}
for(int i64 = 0; i64 < 64; i64 += 1)
{
int current_cell = board64[i64];
int figure = board[current_cell];
if(get_color(figure) != turn_to_move)
continue;
switch(get_value(figure))
{
case QUEEN:
for(int i = 0; i < 8; i += 1)
{
int inc = directions_of_queen[i];
int x = current_cell + inc;
while(board[x] == EMPTY)
{
movelist[n] = create_move(current_cell, x, 0, 0);
n += 1;
x += inc;
}
if(get_color(board[x]) == not_turn_to_move )
{
movelist[n] = create_move(current_cell, x, board[x], 0);
n += 1;
}
}
break;
case ROOK:
for(int i = 0; i < 4; i += 1)
{
int inc = directions_of_rook[i];
int x = current_cell + inc;
while(board[x] == EMPTY)
{
movelist[n] = create_move(current_cell, x, 0, 0);
n += 1;
x += inc;
}
if(get_color(board[x]) == not_turn_to_move)
{
movelist[n] = create_move(current_cell, x, board[x], 0);
n += 1;
}
}
break;
case BISHOP:
for(int i = 0; i < 4; i += 1)
{
int inc = directions_of_bishop[i];
int x = current_cell + inc;
while(board[x] == EMPTY)
{
movelist[n] = create_move(current_cell, x, 0, 0);
n += 1;
x += inc;
}
if(get_color(board[x]) == not_turn_to_move)
{
movelist[n] = create_move(current_cell, x, board[x], 0);
n += 1;
}
}
break;
case KNIGHT:
for(int i = 0; i < 8; i += 1)
{
int tmp = current_cell + moves_of_knight[i];
if(board[tmp] == EMPTY ||
get_color(board[tmp]) == not_turn_to_move)
{
movelist[n] = create_move(current_cell, tmp, board[tmp], 0);
n += 1;
}
}
break;
case PAWN:
;int tmp = current_cell + direction_of_pawns;
if(board[tmp] == EMPTY)
{
if(current_cell/10 == horizontal7)
{
for(int j = 0; j < 4; j += 1)
{
movelist[n] = create_move(current_cell, tmp, 0,
create_figure(turn_to_move, turn_figures[j]));
n += 1;
}
}
else
{
movelist[n] = create_move(current_cell, tmp, 0, 0);
n += 1;
}
tmp += direction_of_pawns;
if(board[tmp] == EMPTY && current_cell/10 == horizontal2)
{
movelist[n] = create_move(current_cell, tmp, 0, 0);
n += 1;
}
}
for(int i = 0; i < 2; i += 1)
{
int tmp = current_cell + captures_of_pawns[i];
if(get_color(board[tmp]) == not_turn_to_move)
{
if(current_cell/10 == horizontal7)
{
for(int j = 0; j < 4; j += 1)
{
movelist[n] = create_move(current_cell, tmp, board[tmp],
create_figure(turn_to_move, turn_figures[j]));
n += 1;
}
}
else
{
movelist[n] = create_move(current_cell, tmp, board[tmp], 0);
n += 1;
}
}
}
}
}
for(int i = 0; i < n; i += 1)
{
Move i_move = movelist[i];
make_move(i_move);
if(in_check(not_turn_to_move))
{
movelist[i] = movelist[n - 1];
n -= 1;
i -= 1;
}
unmake_move(i_move);
}
return n;
}
int generate_captures(Move *movelist)
{
int horizontal2 = turn_to_move == WHITE? 8 : 3;
int horizontal7 = turn_to_move == WHITE? 3 : 8;
int direction_of_pawns = turn_to_move == WHITE? -10: 10;
int captures_of_pawns[2] = {direction_of_pawns + 1, direction_of_pawns - 1};
int place_of_king = turn_to_move == WHITE? place_of_white_king:
place_of_black_king;
int n = 0;
for(int i = 0; i < 8; i += 1)
{
int tmp = place_of_king + moves_of_king[i];
if(get_color(board[tmp]) == not_turn_to_move)
{
movelist[n] = create_move(place_of_king, tmp, board[tmp], 0);
n += 1;
}
}
for(int i64 = 0; i64 < 64; i64 += 1)
{
int current_cell = board64[i64];
int figure = board[current_cell];
if(get_color(figure) != turn_to_move)
continue;
switch(get_value(figure))
{
case QUEEN:
for(int i = 0; i < 8; i += 1)
{
int inc = directions_of_queen[i];
int x = current_cell + inc;
while(board[x] == EMPTY)
x += inc;
if(get_color(board[x]) == not_turn_to_move)
{
movelist[n] = create_move(current_cell, x, board[x], 0);
n += 1;
}
}
break;
case ROOK:
for(int i = 0; i < 4; i += 1)
{
int inc = directions_of_rook[i];
int x = current_cell + inc;
while(board[x] == EMPTY)
x += inc;
if(get_color(board[x]) == not_turn_to_move)
{
movelist[n] = create_move(current_cell, x, board[x], 0);
n += 1;
}
}
break;
case BISHOP:
for(int i = 0; i < 4; i += 1)
{
int inc = directions_of_bishop[i];
int x = current_cell + inc;
while(board[x] == EMPTY)
x += inc;
if(get_color(board[x]) == not_turn_to_move)
{
movelist[n] = create_move(current_cell, x, board[x], 0);
n += 1;
}
}
break;
case KNIGHT:
for(int i = 0; i < 8; i += 1)
{
int tmp = current_cell + moves_of_knight[i];
if(get_color(board[tmp]) == not_turn_to_move)
{
movelist[n] = create_move(current_cell, tmp, board[tmp], 0);
n += 1;
}
}
break;
case PAWN:
for(int i = 0; i < 2; i += 1)
{
int tmp = current_cell + captures_of_pawns[i];
if(get_color(board[tmp]) == not_turn_to_move)
{
movelist[n] = create_move(current_cell, tmp, board[tmp], 0);
n += 1;
}
}
}
}
for(int i = 0; i < n; i += 1)
{
Move i_move = movelist[i];
make_move(i_move);
if(in_check(not_turn_to_move))
{
movelist[i] = movelist[n - 1];
n -= 1;
i -= 1;
}
unmake_move(i_move);
}
return n;
}
U64 perft(int depth)
{
Move movelist[256];
int n;
U64 result = 0;
if(depth == 0) return 1;
n = generate_moves(movelist);
for(int i = 0; i < n; i += 1)
{
Move i_move = movelist[i];
make_move(i_move);
result += perft(depth - 1);
unmake_move(i_move);
}
return result;
}
#define check_perft_on_position(depth, fen) \
setup_position(fen);\
printf("%lld\n", perft(depth))
//Now all piece-square tables and material are from
// http://chessprogramming.wikispaces.com/Simplified+evaluation+function
int material[7] = {-1, 100, 300, 300, 500, 900, 0};
#define get_material_value(color, value) (color == WHITE ? material[value >> 2]:\
-material[value >> 2])
#define MAX_FIGURE_MATERIAL 3100
int PST_QUEEN[64] = {
-20,-10,-10, -5, -5,-10,-10,-20,
-10, 0, 0, 0, 0, 0, 0,-10,
-10, 0, 5, 5, 5, 5, 0,-10,
-5, 0, 5, 5, 5, 5, 0, -5,
0, 0, 5, 5, 5, 5, 0, -5,
-10, 5, 5, 5, 5, 5, 0,-10,
-10, 0, 5, 0, 0, 0, 0,-10,
-20,-10,-10, -5, -5,-10,-10,-20
};
int PST_ROOK[64] = {
0, 0, 0, 0, 0, 0, 0, 0,
5, 10, 10, 10, 10, 10, 10, 5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
-5, 0, 0, 0, 0, 0, 0, -5,
0, 0, 0, 5, 5, 0, 0, 0
};
int PST_BISHOP[64] = {
-20,-10,-10,-10,-10,-10,-10,-20,
-10, 0, 0, 0, 0, 0, 0,-10,
-10, 0, 5, 10, 10, 5, 0,-10,
-10, 5, 5, 10, 10, 5, 5,-10,
-10, 0, 10, 10, 10, 10, 0,-10,
-10, 10, 10, 10, 10, 10, 10,-10,
-10, 5, 0, 0, 0, 0, 5,-10,
-20,-10,-10,-10,-10,-10,-10,-20,
};
int PST_KNIGHT[64] = {
-50,-40,-30,-30,-30,-30,-40,-50,
-40,-20, 0, 0, 0, 0,-20,-40,
-30, 0, 10, 15, 15, 10, 0,-30,
-30, 5, 15, 20, 20, 15, 5,-30,
-30, 0, 15, 20, 20, 15, 0,-30,
-30, 5, 10, 15, 15, 10, 5,-30,
-40,-20, 0, 5, 5, 0,-20,-40,
-50,-40,-30,-30,-30,-30,-40,-50,
};
int PST_PAWN[64] = {
0, 0, 0, 0, 0, 0, 0, 0,
50, 50, 50, 50, 50, 50, 50, 50,
10, 10, 20, 30, 30, 20, 10, 10,
5, 5, 10, 25, 25, 10, 5, 5,
0, 0, 0, 20, 20, 0, 0, 0,
5, -5,-10, 0, 0,-10, -5, 5,
5, 10, 10,-20,-20, 10, 10, 5,
0, 0, 0, 0, 0, 0, 0, 0,
};
int PST_KING[64] = {
-30,-40,-40,-50,-50,-40,-40,-30,
-30,-40,-40,-50,-50,-40,-40,-30,
-30,-40,-40,-50,-50,-40,-40,-30,
-30,-40,-40,-50,-50,-40,-40,-30,
-20,-30,-30,-40,-40,-30,-30,-20,
-10,-20,-20,-20,-20,-20,-20,-10,
20, 20, 0, 0, 0, 0, 20, 20,
20, 30, 10, 0, 0, 10, 30, 20
};
int PST_KING_end[64] = {
-50,-40,-30,-20,-20,-30,-40,-50,
-30,-20,-10, 0, 0,-10,-20,-30,
-30,-10, 20, 30, 30, 20,-10,-30,
-30,-10, 30, 40, 40, 30,-10,-30,
-30,-10, 30, 40, 40, 30,-10,-30,
-30,-10, 20, 30, 30, 20,-10,-30,
-30,-30, 0, 0, 0, 0,-30,-30,
-50,-30,-30,-30,-30,-30,-30,-50
};
int reversed64[64] = {
57,58,59,60,61,62,63,64,
49,50,51,52,53,54,55,56,
41,42,43,44,45,46,47,48,
33,34,35,36,37,38,39,40,
25,26,27,28,29,30,31,32,
17,18,19,20,21,22,23,24,
9,10,11,12,13,14,15,16,
1, 2, 3, 4, 5, 6, 7, 8,
};
int get_PST_value(int figure, int square)
{
int k = 1;
if(get_color(figure) == BLACK)
{
square = reversed64[square];
k = -1;