-
Notifications
You must be signed in to change notification settings - Fork 0
/
MoveGeneration.c
780 lines (653 loc) · 28.4 KB
/
MoveGeneration.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
#include <stdio.h>
#include <stdbool.h>
#include <limits.h>
#include <stdlib.h>
#include <assert.h>
#include "Constants.h"
#include "Move.h"
#include "Board.h"
#include "Utilities.h"
#include "LegalityChecks.h"
#include "AttackMaps.h"
// includes moves, not captures
typedef struct moveTable {
u64 moveBitMap;
u32 from : 6;
u32 pieceType : 2; // 2 == king, 1 == pawn, 0 == other
} MoveTable;
// includes captures, not moves
typedef struct captureTable {
u64 captureBitMap;
u32 from : 6;
u32 pieceType : 2; // 2 == king, 1 == pawn, 0 == other
} CaptureTable;
// max 18 pieces per side
MoveTable moveTables[18];
CaptureTable captureTables[18];
void add_white_pawn_patterns(Board gameState, int square, int patternIndex) {
CaptureTable captureTable = {.from = square, .captureBitMap = 0, .pieceType = 1};
MoveTable moveTable = {.from = square, .moveBitMap = 0, .pieceType = 1};
u64 blackPieces = gameState.blackPieces;
u64 movePattern = get_white_pawn_move_maps(gameState, square);
u64 attackPattern = get_white_pawn_attack_maps(gameState, square);
attackPattern &= blackPieces;
captureTable.captureBitMap = attackPattern;
moveTable.moveBitMap = movePattern;
captureTables[patternIndex] = captureTable;
moveTables[patternIndex] = moveTable;
}
void add_black_pawn_patterns(Board gameState, int square, int patternIndex) {
CaptureTable captureTable = {.from = square, .captureBitMap = 0, .pieceType = 1};
MoveTable moveTable = {.from = square, .moveBitMap = 0, .pieceType = 1};
u64 whitePieces = gameState.whitePieces;
u64 movePattern = get_black_pawn_move_maps(gameState, square);
u64 attackPattern = get_black_pawn_attack_maps(gameState, square);
attackPattern &= whitePieces;
captureTable.captureBitMap = attackPattern;
moveTable.moveBitMap = movePattern;
captureTables[patternIndex] = captureTable;
moveTables[patternIndex] = moveTable;
}
void add_white_knight_patterns(Board gameState, int square, int patternIndex) {
CaptureTable captureTable = {.from = square, .captureBitMap = 0, .pieceType = 0};
MoveTable moveTable = {.from = square, .moveBitMap = 0, .pieceType = 0};
u64 whitePieces = gameState.whitePieces;
u64 blackPieces = gameState.blackPieces;
u64 attackPattern;
u64 movePattern;
u64 pattern = get_knight_maps(gameState, square);
attackPattern = pattern & blackPieces;
movePattern = pattern ^ attackPattern;
movePattern &= (~whitePieces);
captureTable.captureBitMap = attackPattern;
moveTable.moveBitMap = movePattern;
captureTables[patternIndex] = captureTable;
moveTables[patternIndex] = moveTable;
}
void add_black_knight_patterns(Board gameState, int square, int patternIndex) {
CaptureTable captureTable = {.from = square, .captureBitMap = 0, .pieceType = 0};
MoveTable moveTable = {.from = square, .moveBitMap = 0, .pieceType = 0};
u64 whitePieces = gameState.whitePieces;
u64 blackPieces = gameState.blackPieces;
u64 attackPattern;
u64 movePattern;
u64 pattern = get_knight_maps(gameState, square);
attackPattern = pattern & whitePieces;
movePattern = pattern ^ attackPattern;
movePattern &= (~blackPieces);
captureTable.captureBitMap = attackPattern;
moveTable.moveBitMap = movePattern;
captureTables[patternIndex] = captureTable;
moveTables[patternIndex] = moveTable;
}
void add_white_king_patterns(Board gameState, int square, int patternIndex) {
CaptureTable captureTable = {.from = square, .captureBitMap = 0, .pieceType = 2};
MoveTable moveTable = {.from = square, .moveBitMap = 0, .pieceType = 2};
u64 whitePieces = gameState.whitePieces;
u64 blackPieces = gameState.blackPieces;
u64 attackPattern;
u64 movePattern;
u64 pattern = get_king_maps(gameState, square);
attackPattern = pattern & blackPieces;
movePattern = pattern ^ attackPattern;
movePattern &= (~whitePieces);
captureTable.captureBitMap = attackPattern;
moveTable.moveBitMap = movePattern;
captureTables[patternIndex] = captureTable;
moveTables[patternIndex] = moveTable;
}
void add_black_king_patterns(Board gameState, int square, int patternIndex) {
CaptureTable captureTable = {.from = square, .captureBitMap = 0, .pieceType = 2};
MoveTable moveTable = {.from = square, .moveBitMap = 0, .pieceType = 2};
u64 whitePieces = gameState.whitePieces;
u64 blackPieces = gameState.blackPieces;
u64 attackPattern;
u64 movePattern;
u64 pattern = get_king_maps(gameState, square);
attackPattern = pattern & whitePieces;
movePattern = pattern ^ attackPattern;
movePattern &= (~blackPieces);
captureTable.captureBitMap = attackPattern;
moveTable.moveBitMap = movePattern;
captureTables[patternIndex] = captureTable;
moveTables[patternIndex] = moveTable;
}
void add_white_diagonal_patterns(Board gameState, int square, int patternIndex) {
CaptureTable captureTable = {.from = square, .captureBitMap = 0, .pieceType = 0};
MoveTable moveTable = {.from = square, .moveBitMap = 0, .pieceType = 0};
u64 whitePieces = gameState.whitePieces;
u64 blackPieces = gameState.blackPieces;
u64 attackPattern;
u64 movePattern;
u64 pattern = get_diagonal_maps(gameState, square);
attackPattern = pattern & blackPieces;
movePattern = pattern ^ attackPattern;
movePattern &= (~whitePieces);
captureTable.captureBitMap = attackPattern;
moveTable.moveBitMap = movePattern;
captureTables[patternIndex] = captureTable;
moveTables[patternIndex] = moveTable;
}
void add_black_diagonal_patterns(Board gameState, int square, int patternIndex) {
CaptureTable captureTable = {.from = square, .captureBitMap = 0, .pieceType = 0};
MoveTable moveTable = {.from = square, .moveBitMap = 0, .pieceType = 0};
u64 whitePieces = gameState.whitePieces;
u64 blackPieces = gameState.blackPieces;
u64 attackPattern;
u64 movePattern;
u64 pattern = get_diagonal_maps(gameState, square);
attackPattern = pattern & whitePieces;
movePattern = pattern ^ attackPattern;
movePattern &= (~blackPieces);
captureTable.captureBitMap = attackPattern;
moveTable.moveBitMap = movePattern;
captureTables[patternIndex] = captureTable;
moveTables[patternIndex] = moveTable;
}
void add_white_linear_patterns(Board gameState, int square, int patternIndex) {
CaptureTable captureTable = {.from = square, .captureBitMap = 0, .pieceType = 0};
MoveTable moveTable = {.from = square, .moveBitMap = 0, .pieceType = 0};
u64 whitePieces = gameState.whitePieces;
u64 blackPieces = gameState.blackPieces;
u64 attackPattern;
u64 movePattern;
u64 pattern = get_linear_maps(gameState, square);
attackPattern = pattern & blackPieces;
movePattern = pattern ^ attackPattern;
movePattern &= (~whitePieces);
captureTable.captureBitMap = attackPattern;
moveTable.moveBitMap = movePattern;
captureTables[patternIndex] = captureTable;
moveTables[patternIndex] = moveTable;
}
void add_black_linear_patterns(Board gameState, int square, int patternIndex) {
CaptureTable captureTable = {.from = square, .captureBitMap = 0, .pieceType = 0};
MoveTable moveTable = {.from = square, .moveBitMap = 0, .pieceType = 0};
u64 whitePieces = gameState.whitePieces;
u64 blackPieces = gameState.blackPieces;
u64 attackPattern;
u64 movePattern;
u64 pattern = get_linear_maps(gameState, square);
attackPattern = pattern & whitePieces;
movePattern = pattern ^ attackPattern;
movePattern &= (~blackPieces);
captureTable.captureBitMap = attackPattern;
moveTable.moveBitMap = movePattern;
captureTables[patternIndex] = captureTable;
moveTables[patternIndex] = moveTable;
}
bool short_white_castling_squares_empty() {
Board gameState = g_gameStateStack[g_root + g_ply];
u64 whitePieces = gameState.whitePieces;
u64 blackPieces = gameState.blackPieces;
if (square_occupied(whitePieces, 5) || square_occupied(blackPieces, 5)) return false;
if (square_occupied(whitePieces, 6) || square_occupied(blackPieces, 6)) return false;
return true;
}
bool long_white_castling_squares_empty() {
Board gameState = g_gameStateStack[g_root + g_ply];
u64 whitePieces = gameState.whitePieces;
u64 blackPieces = gameState.blackPieces;
if (square_occupied(whitePieces, 1) || square_occupied(blackPieces, 1)) return false;
if (square_occupied(whitePieces, 2) || square_occupied(blackPieces, 2)) return false;
if (square_occupied(whitePieces, 3) || square_occupied(blackPieces, 3)) return false;
return true;
}
bool short_black_castling_squares_empty() {
Board gameState = g_gameStateStack[g_root + g_ply];
u64 whitePieces = gameState.whitePieces;
u64 blackPieces = gameState.blackPieces;
if (square_occupied(whitePieces, 61) || square_occupied(blackPieces, 61)) return false;
if (square_occupied(whitePieces, 62) || square_occupied(blackPieces, 62)) return false;
return true;
}
bool long_black_castling_squares_empty() {
Board gameState = g_gameStateStack[g_root + g_ply];
u64 whitePieces = gameState.whitePieces;
u64 blackPieces = gameState.blackPieces;
if (square_occupied(whitePieces, 57) || square_occupied(blackPieces, 57)) return false;
if (square_occupied(whitePieces, 58) || square_occupied(blackPieces, 58)) return false;
if (square_occupied(whitePieces, 59) || square_occupied(blackPieces, 59)) return false;
return true;
}
// board coordinates are from 0 to 63 and start from bottom left from
// whites point of view, they go left to right, bottom to top
// a single move: 32-bit unsigned int, 8 bits: original location,
// 8 bits destination location,
// 3 bits promotion (queen, rook, bishop, knight or none)
// 2 bits for castling (queenside, kingside or none)
// in case of castling, the castling king is determined by the original location
// game state: an array consisting of 13 unsigned 64-bit integers (long long)
// first 12 of those use used to contain location of a certain piece
// such as white pawn in the 64 squares of the board
// so if for example 45:th bit is 1, the 44:th (0-index) square of
// the board has a white pawn
// the game state contains pawns, knights, bishops, rooks, queens and king
// in that order first for white then for black
// the last number (13:th) is used to store other useful data about the game
// such as castling rights (2 bits for both sides), en passant (8 bits) and information concerning
// the 3-fold repetition (4 bits) and the 50-move rule (9 bits)
// side: 0 == white, 1 == black
int generate_moves(Move *movesArr, int side) {
int i;
// next free index in moves array
int patternIndex = 0;
int moveIndex = 0;
Board gameState = g_gameStateStack[g_root + g_ply];
u64 whitePieces = gameState.whitePieces;
u64 whitePawns = gameState.pawns & whitePieces;
u64 whiteKnights = gameState.knights & whitePieces;
u64 whiteBishops = gameState.bishops & whitePieces;
u64 whiteRooks = gameState.rooks & whitePieces;
u64 whiteQueens = gameState.queens & whitePieces;
u64 whiteKings = gameState.kings & whitePieces;
u64 blackPieces = gameState.blackPieces;
u64 blackPawns = gameState.pawns & blackPieces;
u64 blackKnights = gameState.knights & blackPieces;
u64 blackBishops = gameState.bishops & blackPieces;
u64 blackRooks = gameState.rooks & blackPieces;
u64 blackQueens = gameState.queens & blackPieces;
u64 blackKings = gameState.kings & blackPieces;
u64 otherGameInfo = gameState.meta;
u64 whiteDiagonals = whiteQueens | whiteBishops;
u64 blackDiagonals = blackQueens | blackBishops;
u64 whiteLinears = whiteQueens | whiteRooks;
u64 blackLinears = blackQueens | blackRooks;
bool isEnPassant = is_enpassant_allowed(otherGameInfo);
u64 attackSet = g_attackSets[g_ply];
if (side == 1) {
while(whitePawns != 0) {
int index = bitscan_forward(whitePawns);
whitePawns &= (whitePawns - 1); // reset the least significant bit
add_white_pawn_patterns(gameState, index, patternIndex);
patternIndex++;
}
while(whiteKnights != 0) {
int index = bitscan_forward(whiteKnights);
whiteKnights &= (whiteKnights - 1); // reset the least significant bit
add_white_knight_patterns(gameState, index, patternIndex);
patternIndex++;
}
while(whiteDiagonals != 0) {
int index = bitscan_forward(whiteDiagonals);
whiteDiagonals &= (whiteDiagonals - 1); // reset the least significant bit
add_white_diagonal_patterns(gameState, index, patternIndex);
patternIndex++;
}
while(whiteLinears != 0) {
int index = bitscan_forward(whiteLinears);
whiteLinears &= (whiteLinears - 1); // reset the least significant bit
add_white_linear_patterns(gameState, index, patternIndex);
patternIndex++;
}
while(whiteKings != 0) {
int index = bitscan_forward(whiteKings);
whiteKings &= (whiteKings - 1); // reset the least significant bit
add_white_king_patterns(gameState, index, patternIndex);
patternIndex++;
}
}
else {
while(blackPawns != 0) {
int index = bitscan_forward(blackPawns);
blackPawns &= (blackPawns - 1); // reset the least significant bit
add_black_pawn_patterns(gameState, index, patternIndex);
patternIndex++;
}
while(blackKnights != 0) {
int index = bitscan_forward(blackKnights);
blackKnights &= (blackKnights - 1); // reset the least significant bit
add_black_knight_patterns(gameState, index, patternIndex);
patternIndex++;
}
while(blackDiagonals != 0) {
int index = bitscan_forward(blackDiagonals);
blackDiagonals &= (blackDiagonals - 1); // reset the least significant bit
add_black_diagonal_patterns(gameState, index, patternIndex);
patternIndex++;
}
while(blackLinears != 0) {
int index = bitscan_forward(blackLinears);
blackLinears &= (blackLinears - 1); // reset the least significant bit
add_black_linear_patterns(gameState, index, patternIndex);
patternIndex++;
}
while(blackKings != 0) {
int index = bitscan_forward(blackKings);
blackKings &= (blackKings - 1); // reset the least significant bit
add_black_king_patterns(gameState, index, patternIndex);
patternIndex++;
}
}
// first create capturing moves
for (i = 0; i < patternIndex; i++) {
CaptureTable captureTable;
int from;
int to;
u64 pattern;
int type;
captureTable = captureTables[i];
from = captureTable.from;
pattern = captureTable.captureBitMap;
type = captureTable.pieceType;
attackSet |= pattern;
// pawn move
if (type == 1) {
int row;
// if row is 6 or 1, there is a possibility of promotion
row = from / 8;
while (pattern != 0) {
to = bitscan_forward(pattern);
pattern &= (pattern - 1);
// promotion
if ((row == 6 && side == 1) || (row == 1 && side == -1)) {
movesArr[moveIndex] = create_move(from, to, KNIGHT_PROMOTION_CAPTURE_MOVE);
moveIndex++;
movesArr[moveIndex] = create_move(from, to, BISHOP_PROMOTION_CAPTURE_MOVE);
moveIndex++;
movesArr[moveIndex] = create_move(from, to, ROOK_PROMOTION_CAPTURE_MOVE);
moveIndex++;
movesArr[moveIndex] = create_move(from, to, QUEEN_PROMOTION_CAPTURE_MOVE);
moveIndex++;
}
// no promotion
else {
movesArr[moveIndex] = create_move(from, to, CAPTURE_MOVE);
moveIndex++;
}
}
}
else {
while (pattern != 0) {
to = bitscan_forward(pattern);
pattern &= (pattern - 1);
movesArr[moveIndex] = create_move(from, to, CAPTURE_MOVE);
moveIndex++;
}
}
}
g_attackSets[g_ply] = attackSet;
// then create non-capturing moves
for (i = 0; i < patternIndex; i++) {
MoveTable moveTable;
int from;
int to;
u64 pattern;
int type;
moveTable = moveTables[i];
from = moveTable.from;
pattern = moveTable.moveBitMap;
type = moveTable.pieceType;
// pawn move
if (type == 1) {
// if row is 6 or 1, there is a possibility of promotion
int row = from / 8;
while (pattern != 0) {
to = bitscan_forward(pattern);
pattern &= (pattern - 1);
// promotion
if ((row == 6 && side == 1) || (row == 1 && side == -1)) {
movesArr[moveIndex] = create_move(from, to, KNIGHT_PROMOTION_MOVE);
moveIndex++;
movesArr[moveIndex] = create_move(from, to, BISHOP_PROMOTION_MOVE);
moveIndex++;
movesArr[moveIndex] = create_move(from, to, ROOK_PROMOTION_MOVE);
moveIndex++;
movesArr[moveIndex] = create_move(from, to, QUEEN_PROMOTION_MOVE);
moveIndex++;
}
// no promotion
else {
movesArr[moveIndex] = create_move(from, to, QUIET_MOVE);
moveIndex++;
}
}
}
else {
while (pattern != 0) {
to = bitscan_forward(pattern);
pattern &= (pattern - 1);
movesArr[moveIndex] = create_move(from, to, QUIET_MOVE);
moveIndex++;
}
}
}
// handle enPassant
if (isEnPassant) {
int enPassantSquare = get_enpassant_square(otherGameInfo);
int enPassantColumn = enPassantSquare % 8;
if (side == 1) {
whitePawns = gameState.pawns & gameState.whitePieces;
if (enPassantColumn != 0 && square_occupied(whitePawns, enPassantSquare - 1)) {
movesArr[moveIndex] = create_move(enPassantSquare - 1, enPassantSquare + 8, EP_CAPTURE_MOVE);
moveIndex++;
}
if (enPassantColumn != 7 && square_occupied(whitePawns, enPassantSquare + 1)) {
movesArr[moveIndex] = create_move(enPassantSquare + 1, enPassantSquare + 8, EP_CAPTURE_MOVE);
moveIndex++;
}
}
else {
blackPawns = gameState.pawns & gameState.blackPieces;
if (enPassantColumn != 0 && square_occupied(blackPawns, enPassantSquare - 1)) {
movesArr[moveIndex] = create_move(enPassantSquare - 1, enPassantSquare - 8, EP_CAPTURE_MOVE);
moveIndex++;
}
if (enPassantColumn != 7 && square_occupied(blackPawns, enPassantSquare + 1)) {
movesArr[moveIndex] = create_move(enPassantSquare + 1, enPassantSquare - 8, EP_CAPTURE_MOVE);
moveIndex++;
}
}
}
// castling move generation
if (side == 1) {
if (can_white_castle_short(otherGameInfo)) {
if (short_white_castling_squares_empty()) {
// king cant be or move through any threatened squares
if (!is_square_threatened(4, side) && !is_square_threatened(5, side) && !is_square_threatened(5, side)) {
movesArr[moveIndex] = create_move(4, 6, KING_CASTLE_MOVE);
moveIndex++;
}
}
}
if (can_white_castle_long(otherGameInfo)) {
if (long_white_castling_squares_empty()) {
// king cant be or move through any threatened squares
if (!is_square_threatened(2, side) && !is_square_threatened(3, side) && !is_square_threatened(4, side)) {
movesArr[moveIndex] = create_move(4, 2, QUEEN_CASTLE_MOVE);
moveIndex++;
}
}
}
}
else {
if (can_black_castle_short(otherGameInfo)) {
if (short_black_castling_squares_empty()) {
// king cant be or move through any threatened squares
if (!is_square_threatened(60, side) && !is_square_threatened(61, side) && !is_square_threatened(62, side)) {
movesArr[moveIndex] = create_move(60, 62, KING_CASTLE_MOVE);
moveIndex++;
}
}
}
if (can_black_castle_long(otherGameInfo)) {
if (long_black_castling_squares_empty()) {
// king cant be or move through any threatened squares
if (!is_square_threatened(58, side) && !is_square_threatened(59, side) && !is_square_threatened(60, side)) {
movesArr[moveIndex] = create_move(60, 58, QUEEN_CASTLE_MOVE);
moveIndex++;
}
}
}
}
return moveIndex;
}
// only generate capturing moves (pseudolegal), used in qsearch
int generate_captures(Move *movesArr, int side) {
int i;
// next free index in moves array
int patternIndex = 0;
int moveIndex = 0;
Board gameState = g_gameStateStack[g_root + g_ply];
u64 whitePieces = gameState.whitePieces;
u64 whitePawns = gameState.pawns & whitePieces;
u64 whiteKnights = gameState.knights & whitePieces;
u64 whiteBishops = gameState.bishops & whitePieces;
u64 whiteRooks = gameState.rooks & whitePieces;
u64 whiteQueens = gameState.queens & whitePieces;
u64 whiteKings = gameState.kings & whitePieces;
u64 blackPieces = gameState.blackPieces;
u64 blackPawns = gameState.pawns & blackPieces;
u64 blackKnights = gameState.knights & blackPieces;
u64 blackBishops = gameState.bishops & blackPieces;
u64 blackRooks = gameState.rooks & blackPieces;
u64 blackQueens = gameState.queens & blackPieces;
u64 blackKings = gameState.kings & blackPieces;
u64 otherGameInfo = gameState.meta;
u64 whiteDiagonals = whiteQueens | whiteBishops;
u64 blackDiagonals = blackQueens | blackBishops;
u64 whiteLinears = whiteQueens | whiteRooks;
u64 blackLinears = blackQueens | blackRooks;
bool isEnPassant = is_enpassant_allowed(otherGameInfo);
u64 attackSet = g_attackSets[g_ply];
if (side == 1) {
while(whitePawns != 0) {
int index = bitscan_forward(whitePawns);
whitePawns &= (whitePawns - 1); // reset the least significant bit
add_white_pawn_patterns(gameState, index, patternIndex);
patternIndex++;
}
while(whiteKnights != 0) {
int index = bitscan_forward(whiteKnights);
whiteKnights &= (whiteKnights - 1); // reset the least significant bit
add_white_knight_patterns(gameState, index, patternIndex);
patternIndex++;
}
while(whiteDiagonals != 0) {
int index = bitscan_forward(whiteDiagonals);
whiteDiagonals &= (whiteDiagonals - 1); // reset the least significant bit
add_white_diagonal_patterns(gameState, index, patternIndex);
patternIndex++;
}
while(whiteLinears != 0) {
int index = bitscan_forward(whiteLinears);
whiteLinears &= (whiteLinears - 1); // reset the least significant bit
add_white_linear_patterns(gameState, index, patternIndex);
patternIndex++;
}
while(whiteKings != 0) {
int index = bitscan_forward(whiteKings);
whiteKings &= (whiteKings - 1); // reset the least significant bit
add_white_king_patterns(gameState, index, patternIndex);
patternIndex++;
}
}
else {
while(blackPawns != 0) {
int index = bitscan_forward(blackPawns);
blackPawns &= (blackPawns - 1); // reset the least significant bit
add_black_pawn_patterns(gameState, index, patternIndex);
patternIndex++;
}
while(blackKnights != 0) {
int index = bitscan_forward(blackKnights);
blackKnights &= (blackKnights - 1); // reset the least significant bit
add_black_knight_patterns(gameState, index, patternIndex);
patternIndex++;
}
while(blackDiagonals != 0) {
int index = bitscan_forward(blackDiagonals);
blackDiagonals &= (blackDiagonals - 1); // reset the least significant bit
add_black_diagonal_patterns(gameState, index, patternIndex);
patternIndex++;
}
while(blackLinears != 0) {
int index = bitscan_forward(blackLinears);
blackLinears &= (blackLinears - 1); // reset the least significant bit
add_black_linear_patterns(gameState, index, patternIndex);
patternIndex++;
}
while(blackKings != 0) {
int index = bitscan_forward(blackKings);
blackKings &= (blackKings - 1); // reset the least significant bit
add_black_king_patterns(gameState, index, patternIndex);
patternIndex++;
}
}
// create capturing moves
for (i = 0; i < patternIndex; i++) {
CaptureTable captureTable;
int from;
int to;
u64 pattern;
int type;
captureTable = captureTables[i];
from = captureTable.from;
pattern = captureTable.captureBitMap;
type = captureTable.pieceType;
attackSet |= pattern;
// pawn move
if (type == 1) {
int row;
// if row is 6 or 1, there is a possibility of promotion
row = from / 8;
while (pattern != 0) {
to = bitscan_forward(pattern);
pattern &= (pattern - 1);
// promotion
if ((row == 6 && side == 1) || (row == 1 && side == -1)) {
movesArr[moveIndex] = create_move(from, to, KNIGHT_PROMOTION_CAPTURE_MOVE);
moveIndex++;
movesArr[moveIndex] = create_move(from, to, BISHOP_PROMOTION_CAPTURE_MOVE);
moveIndex++;
movesArr[moveIndex] = create_move(from, to, ROOK_PROMOTION_CAPTURE_MOVE);
moveIndex++;
movesArr[moveIndex] = create_move(from, to, QUEEN_PROMOTION_CAPTURE_MOVE);
moveIndex++;
}
// no promotion
else {
movesArr[moveIndex] = create_move(from, to, CAPTURE_MOVE);
moveIndex++;
}
}
}
else {
while (pattern != 0) {
to = bitscan_forward(pattern);
pattern &= (pattern - 1);
movesArr[moveIndex] = create_move(from, to, CAPTURE_MOVE);
moveIndex++;
}
}
}
g_attackSets[g_ply] = attackSet;
// handle enPassant
if (isEnPassant) {
int enPassantSquare = get_enpassant_square(otherGameInfo);
int enPassantColumn = enPassantSquare % 8;
if (side == 1) {
whitePawns = gameState.pawns & gameState.whitePieces;
if (enPassantColumn != 0 && square_occupied(whitePawns, enPassantSquare - 1)) {
movesArr[moveIndex] = create_move(enPassantSquare - 1, enPassantSquare + 8, EP_CAPTURE_MOVE);
moveIndex++;
}
if (enPassantColumn != 7 && square_occupied(whitePawns, enPassantSquare + 1)) {
movesArr[moveIndex] = create_move(enPassantSquare + 1, enPassantSquare + 8, EP_CAPTURE_MOVE);
moveIndex++;
}
}
else {
blackPawns = gameState.pawns & gameState.blackPieces;
if (enPassantColumn != 0 && square_occupied(blackPawns, enPassantSquare - 1)) {
movesArr[moveIndex] = create_move(enPassantSquare - 1, enPassantSquare - 8, EP_CAPTURE_MOVE);
moveIndex++;
}
if (enPassantColumn != 7 && square_occupied(blackPawns, enPassantSquare + 1)) {
movesArr[moveIndex] = create_move(enPassantSquare + 1, enPassantSquare - 8, EP_CAPTURE_MOVE);
moveIndex++;
}
}
}
return moveIndex;
}