-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmove_generation.c
More file actions
488 lines (352 loc) · 16.9 KB
/
move_generation.c
File metadata and controls
488 lines (352 loc) · 16.9 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
#include "move_generation.h"
#include "transposition.h"
// utility for nonpawn move generation
static Move *append_nonpawn_moves_from_attack_set(Move *moveList, SquareCode from, U64 attacks, const U64 *occupied, const U64 *ours);
// functions
Move *generate_castling_moves(Move *moveList, Color color, const Board *board) {
// kingside
if ((board->meta.kingsideCastlingRight[color])
// check if the squares are not occupied by other pieces
&& ((emptySquaresForKingsideCastling[color] & board->occupied) == 0ll)
// check if there is still a rook in the corner i. e. if it has not been captured yet
&& (board->pieceCode[ (color ? h8 : h1) ] == (color ? bRook : wRook))
// check if the squares are not attacked by opponent's pieces
&& !(color ?
/*black*/ (attacks_to_square(board, e8, WHITE) | attacks_to_square(board, f8, WHITE) | attacks_to_square(board, g8, WHITE))
:
/*white*/ (attacks_to_square(board, e1, BLACK) | attacks_to_square(board, f1, BLACK) | attacks_to_square(board, g1, BLACK))
)) {
SquareCode from = (color == WHITE) ? e1 : e8;
SquareCode to = (color == WHITE) ? g1 : g8;
*moveList++ = construct_move(from, to, kingsideCastleFlag);
}
// queenside
if ((board->meta.queensideCastlingRight[color])
// check if the squares are not occupied by other pieces
&& ((emptySquaresForQueensideCastling[color] & board->occupied) == 0ll)
// check if there is still a rook in the corner i. e. if it has not been captured yet
&& (board->pieceCode[ (color ? a8 : a1) ] == (color ? bRook : wRook))
// check if the squares are not attacked by opponent's pieces
&& !(color ?
/*black*/ (attacks_to_square(board, e8, WHITE) | attacks_to_square(board, d8, WHITE) | attacks_to_square(board, c8, WHITE))
:
/*white*/ (attacks_to_square(board, e1, BLACK) | attacks_to_square(board, d1, BLACK) | attacks_to_square(board, c1, BLACK))
)) {
SquareCode from = (color == WHITE) ? e1 : e8;
SquareCode to = (color == WHITE) ? c1 : c8;
*moveList++ = construct_move(from, to, queensideCastleFlag);
}
return moveList;
}
// function for serializing the attack bitboard and appending the generated moves to the move list
// ours is the occupation of our own pieces
static Move *append_nonpawn_moves_from_attack_set(Move *moveList, SquareCode from, U64 attacks, const U64 *occupied, const U64 *ours) {
if (attacks) do {
SquareCode to = bitscan_forward(attacks);
// if to is a piece of our own color move is not legal
if ((1ll << to) & *ours)
continue;
unsigned int flags = ((1ll << to) & *occupied) ? captureFlag : 0;
*moveList++ = construct_move(from, to, flags);
} while (attacks &= (attacks-1)); // resets LSB
return moveList;
}
Move *generate_nonpawn_moves(Move *moveList, Color color, const Board *board) {
// bishops
U64 pieceBB = board->pieceBB[ (color ? bBishop : wBishop) ];
if (pieceBB) do {
int from = bitscan_forward(pieceBB);
moveList = append_nonpawn_moves_from_attack_set(moveList, from,
bishop_attacks(from, board->occupied), &(board->occupied), &(board->piecesByColor[color]));
} while (pieceBB &= (pieceBB-1)); // resets LSB
// rooks
pieceBB = board->pieceBB[ (color ? bRook : wRook) ];
if (pieceBB) do {
SquareCode from = bitscan_forward(pieceBB);
moveList = append_nonpawn_moves_from_attack_set(moveList, from,
rook_attacks(from, board->occupied), &(board->occupied), &(board->piecesByColor[color]));
} while (pieceBB &= (pieceBB-1));
// queens
pieceBB = board->pieceBB[ (color ? bQueen : wQueen) ];
if (pieceBB) do {
SquareCode from = bitscan_forward(pieceBB);
moveList = append_nonpawn_moves_from_attack_set(moveList, from,
queen_attacks(from, board->occupied), &(board->occupied), &(board->piecesByColor[color]));
} while (pieceBB &= (pieceBB-1));
// knights
pieceBB = board->pieceBB[ (color ? bKnight : wKnight) ];
if (pieceBB) do {
SquareCode from = bitscan_forward(pieceBB);
moveList = append_nonpawn_moves_from_attack_set(moveList, from,
knight_attacks(from), &(board->occupied), &(board->piecesByColor[color]));
} while (pieceBB &= (pieceBB-1));
// king
// exactly one king, if is temporary for debugging
{
SquareCode from = board->kingSquare[color];
moveList = append_nonpawn_moves_from_attack_set(moveList, from,
king_attacks(from), &(board->occupied), &(board->piecesByColor[color]));
}
return moveList;
}
Move *generate_pawn_moves(Move *moveList, Color color, const Board *board) {
// pawn pushes
U64 singlePushTargets = color ? bPawn_single_push_targets(board->pieceBB[bPawn], board->occupied)
: wPawn_single_push_targets(board->pieceBB[wPawn], board->occupied);
if (singlePushTargets) do {
SquareCode to = bitscan_forward(singlePushTargets);
SquareCode from = color ? (to + 8) : (to - 8);
// append `quiet` promotions if on the last rank
if ((color && (to < a2)) | (!color && (to > h7))) {
*moveList++ = construct_move(from, to, knightPromotionFlag);
*moveList++ = construct_move(from, to, bishopPromotionFlag);
*moveList++ = construct_move(from, to, rookPromotionFlag);
*moveList++ = construct_move(from, to, queenPromotionFlag);
}
else {
*moveList++ = construct_move(from, to, 0);
}
} while (singlePushTargets &= (singlePushTargets-1));
U64 doublePushTargets = color ? bPawn_double_push_targets(board->pieceBB[bPawn], board->occupied)
: wPawn_double_push_targets(board->pieceBB[wPawn], board->occupied);
if (doublePushTargets) do {
SquareCode to = bitscan_forward(doublePushTargets);
SquareCode from = color ? (to + 16) : (to - 16);
*moveList++ = construct_move(from, to, doublePushFlag);
} while (doublePushTargets &= (doublePushTargets-1));
// pawn attacks
// normal
// later could refactor into a single function w/ nonpawn bitboard serialization
U64 eastAttacks = color ? bPawn_east_attacks(board->pieceBB[bPawn])
: wPawn_east_attacks(board->pieceBB[wPawn]);
eastAttacks &= board->piecesByColor[!color]; // occupied squares by opposite color
if (eastAttacks) do {
SquareCode to = bitscan_forward(eastAttacks);
SquareCode from = color ? (to + 7) : (to - 9);
// check for capture promotions
if ((color && (to < a2)) || (!color && (to > h7))) {
*moveList++ = construct_move(from, to, knightPromotionFlag | captureFlag);
*moveList++ = construct_move(from, to, bishopPromotionFlag | captureFlag);
*moveList++ = construct_move(from, to, rookPromotionFlag | captureFlag);
*moveList++ = construct_move(from, to, queenPromotionFlag | captureFlag);
} else *moveList++ = construct_move(from, to, captureFlag);
} while (eastAttacks &= (eastAttacks-1));
U64 westAttacks = color ? bPawn_west_attacks(board->pieceBB[bPawn])
: wPawn_west_attacks(board->pieceBB[wPawn]);
westAttacks &= board->piecesByColor[!color];
if (westAttacks) do {
SquareCode to = bitscan_forward(westAttacks);
SquareCode from = color ? (to + 9) : (to - 7);
// check for capture promotions
if ((color && (to < a2)) || (!color && (to > h7))) {
*moveList++ = construct_move(from, to, knightPromotionFlag | captureFlag);
*moveList++ = construct_move(from, to, bishopPromotionFlag | captureFlag);
*moveList++ = construct_move(from, to, rookPromotionFlag | captureFlag);
*moveList++ = construct_move(from, to, queenPromotionFlag | captureFlag);
} else *moveList++ = construct_move(from, to, captureFlag);
} while (westAttacks &= (westAttacks-1));
// en passant captures
// en passant capture can't be a promotion
U64 eastEnPassantAttacks = color ? bPawn_east_attacks(board->pieceBB[bPawn])
: wPawn_east_attacks(board->pieceBB[wPawn]);
eastEnPassantAttacks &= board->meta.enPassantTargets;// & possibleEnPassantTargets[color];
if (eastEnPassantAttacks) do {
SquareCode to = bitscan_forward(eastEnPassantAttacks);
SquareCode from = color ? (to + 7) : (to - 9);
*moveList++ = construct_move(from, to, enPassantCaptureFlag);
} while (eastEnPassantAttacks &= (eastEnPassantAttacks-1));
U64 westEnPassantAttacks = color ? bPawn_west_attacks(board->pieceBB[bPawn])
: wPawn_west_attacks(board->pieceBB[wPawn]);
westEnPassantAttacks &= board->meta.enPassantTargets;// & possibleEnPassantTargets[color];
if (westEnPassantAttacks) do {
SquareCode to = bitscan_forward(westEnPassantAttacks);
SquareCode from = color ? (to + 9) : (to - 7);
*moveList++ = construct_move(from, to, enPassantCaptureFlag);
} while (westEnPassantAttacks &= (westEnPassantAttacks-1));
return moveList;
}
Move *generate_pseudolegal_moves(Move *moveList, Color color, const Board* board) {
moveList = generate_pawn_moves(moveList, color, board);
moveList = generate_nonpawn_moves(moveList, color, board);
moveList = generate_castling_moves(moveList, color, board);
return moveList;
}
// make move
void make_move(Board *board, Move move, Stack *stack) {
// create the object for pushing the present metadata into the stack
StackObject stObj;
stObj.meta = board -> meta;
stObj.capturedPiece = none; // no capture in the general case
Color color = board -> meta.sideToMove;
SquareCode from = get_from(move), to = get_to(move);
PieceType pt = board -> pieceCode[from];
// flip side to move in hash
board->hash ^= zobristKeys.flipSideToMove;
// turn off castling ability after a rook or king move
// first flip current rights in hash
board->hash ^= zobristKeys.flipKingsideCastlingRight[board -> meta.kingsideCastlingRight[color]];
board->hash ^= zobristKeys.flipQueensideCastlingRight[board -> meta.queensideCastlingRight[color]];
if (pt == (color ? bKing : wKing))
board -> meta.kingsideCastlingRight[color] = board -> meta.queensideCastlingRight[color] = 0;
else if (from == (color ? a8 : a1))
board -> meta.queensideCastlingRight[color] = 0;
else if (from == (color ? h8 : h1))
board -> meta.kingsideCastlingRight[color] = 0;
// now hash current values
board->hash ^= zobristKeys.flipKingsideCastlingRight[board -> meta.kingsideCastlingRight[color]];
board->hash ^= zobristKeys.flipQueensideCastlingRight[board -> meta.queensideCastlingRight[color]];
// flip old en passant squares in hash, note this bitboard gets discarded so we can modify
if (board->meta.enPassantTargets) do {
SquareCode epSquare = bitscan_forward(board->meta.enPassantTargets);
board->hash ^= zobristKeys.enPassantSquare[epSquare];
} while (board->meta.enPassantTargets &= (board->meta.enPassantTargets - 1));
// setting the en passant target square after double push
board -> meta.enPassantTargets = ((move & moveKindFlag) == doublePushFlag) ?
(1ll << ((get_from(move) + get_to(move)) / 2)) : 0ll;
// flip new en passant squares in hash, note we can't currently modify the bitboard
U64 newEpTargets = board->meta.enPassantTargets;
if (newEpTargets) do {
SquareCode epSquare = bitscan_forward(newEpTargets);
board->hash ^= zobristKeys.enPassantSquare[epSquare];
} while (newEpTargets &= (newEpTargets - 1));
// handling castles
if ((move & moveKindFlag) == kingsideCastleFlag) {
remove_piece(board,
(color ? bKing : wKing),
(color ? e8 : e1)
);
add_piece(board,
(color ? bKing : wKing),
(color ? g8 : g1)
);
remove_piece(board,
(color ? bRook : wRook),
(color ? h8 : h1)
);
add_piece(board,
(color ? bRook : wRook),
(color ? f8 : f1)
);
} else if ((move & moveKindFlag) == queensideCastleFlag) {
remove_piece(board,
(color ? bKing : wKing),
(color ? e8 : e1)
);
add_piece(board,
(color ? bKing : wKing),
(color ? c8 : c1)
);
remove_piece(board,
(color ? bRook : wRook),
(color ? a8 : a1)
);
add_piece(board,
(color ? bRook : wRook),
(color ? d8 : d1)
);
}
else {
remove_piece(board, pt, from);
if (move & captureFlag) {
// handling en passant moves as well
int isEnPassantCapture = ((move & moveKindFlag) == enPassantCaptureFlag);
stObj.capturedPiece = isEnPassantCapture ? (color ? wPawn : bPawn) : board -> pieceCode[to];
remove_piece(board, stObj.capturedPiece, (isEnPassantCapture ? (color ? (to+8) : (to-8)) : to));
}
// handling promotions; if a move is promotion, it can't be an en passant capture or double push
pt = (move & promotionFlag) ?
(
((move & promotionKindFlag) == knightPromotionFlag) ? (color ? bKnight : wKnight) :
(
((move & promotionKindFlag) == bishopPromotionFlag) ? (color ? bBishop : wBishop) :
(
((move & promotionKindFlag) == rookPromotionFlag) ? (color ? bRook : wRook) :
(color ? bQueen : wQueen)
)
)
) : pt;
add_piece(board, pt, to);
}
// push current state into the stack
push(stObj, stack);
// update board state
board->meta.sideToMove = !color;
board->meta.fullmoveCounter++;
if ((move & captureFlag) || (pt == (color ? bPawn : wPawn))) { // TODO: this might be buggy, check logic
board->meta.halfmoveClock++;
}
}
// unmake move
void unmake_move(Board *board, Move move, Stack *stack) { // color is whose move is being unmade
// pop the board metadata to be restored from stack
StackObject stObj = pop(stack);
Color color = stObj.meta.sideToMove;
SquareCode from = get_from(move), to = get_to(move);
PieceType pt = board -> pieceCode[to];
// flip side to move in hash
board->hash ^= zobristKeys.flipSideToMove;
// flip new en passant squares in hash
// note new current en passant fields are discarded so modification is allowed
if (board->meta.enPassantTargets) do {
SquareCode epSquare = bitscan_forward(board->meta.enPassantTargets);
board->hash ^= zobristKeys.enPassantSquare[epSquare];
} while (board->meta.enPassantTargets &= (board->meta.enPassantTargets - 1));
// flip old en passant squares in hash, note modification is not allowed
U64 oldEpTargets = stObj.meta.enPassantTargets;
if (oldEpTargets) do {
SquareCode epSquare = bitscan_forward(oldEpTargets);
board->hash ^= zobristKeys.enPassantSquare[epSquare];
} while (oldEpTargets &= (oldEpTargets - 1));
// reset castling right hashes
board->hash ^= zobristKeys.flipKingsideCastlingRight[board -> meta.kingsideCastlingRight[color]];
board->hash ^= zobristKeys.flipQueensideCastlingRight[board -> meta.queensideCastlingRight[color]];
board->hash ^= zobristKeys.flipKingsideCastlingRight[stObj.meta.kingsideCastlingRight[color]];
board->hash ^= zobristKeys.flipQueensideCastlingRight[stObj.meta.queensideCastlingRight[color]];
if ((move & moveKindFlag) == kingsideCastleFlag) {
remove_piece(board,
(color ? bKing : wKing),
(color ? g8 : g1)
);
add_piece(board,
(color ? bKing : wKing),
(color ? e8 : e1)
);
remove_piece(board,
(color ? bRook : wRook),
(color ? f8 : f1)
);
add_piece(board,
(color ? bRook : wRook),
(color ? h8 : h1)
);
} else if ((move & moveKindFlag) == queensideCastleFlag) {
remove_piece(board,
(color ? bKing : wKing),
(color ? c8 : c1)
);
add_piece(board,
(color ? bKing : wKing),
(color ? e8 : e1)
);
remove_piece(board,
(color ? bRook : wRook),
(color ? d8 : d1)
);
add_piece(board,
(color ? bRook : wRook),
(color ? a8 : a1)
);
}
else {
remove_piece(board, pt, to);
if (move & captureFlag) {
// handling en passant moves as well
add_piece(board, stObj.capturedPiece,
((move & moveKindFlag) == enPassantCaptureFlag) ? (color ? (to+8) : (to-8)) : to);
}
pt = (move & promotionFlag) ? (color ? bPawn : wPawn) : pt;
add_piece(board, pt, from);
}
// restoring board state from stack
board -> meta = stObj.meta;
}