-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmovegen.cpp
324 lines (297 loc) · 12.6 KB
/
movegen.cpp
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
#include <iostream>
#include "bitmanip.h"
#include "movegen.h"
#include "precomp.h"
/* Add a move at the end of a move array */
inline void pushMove(Move *m, int *numMoves, byte piece, byte fromSquare, byte toSquare, byte promotion) {
m[*numMoves].piece = piece;
m[*numMoves].from = fromSquare;
m[*numMoves].to = toSquare;
m[*numMoves].promotion = promotion;
(*numMoves)++;
}
/* Get all legal knight moves (destMask = BB_EMPTY) or captures (destMask = BB_WALL / BB_BALL) */
inline void getKnightMoves(u64 pieceSet, u64 destMask, Move *m, int *numMoves) {
u64 fromSq, toSq, mask;
while (pieceSet) {
GET_BIT_AND_CLEAR(pieceSet, fromSq);
mask = knightAttacks[fromSq] & destMask;
while (mask) {
GET_BIT_AND_CLEAR(mask, toSq);
pushMove(m, numMoves, KNIGHT, fromSq, toSq, 0);
}
}
}
/* Get all legal king moves (destMask = BB_EMPTY) or captures (destMask = BB_WALL / BB_BALL) */
inline void getKingMoves(u64 pieceSet, u64 destMask, Move *m, int *numMoves) {
u64 fromSq, toSq, mask;
while (pieceSet) {
GET_BIT_AND_CLEAR(pieceSet, fromSq);
mask = kingAttacks[fromSq] & destMask;
while (mask) {
GET_BIT_AND_CLEAR(mask, toSq);
pushMove(m, numMoves, KING, fromSq, toSq, 0);
}
}
}
/* Get all legal rank moves / captures for queens and rooks */
inline void getRankMoves(int pieceType, bool capture, u64 pieceSet, u64 enemy, u64 empty, Move *m, int *numMoves) {
u64 fromSq, toSq, mask;
byte file, rank, rankOccupancy;
while (pieceSet) {
GET_BIT_AND_CLEAR(pieceSet, fromSq);
file = fromSq & 7;
rank = fromSq & ~7; /* 8 * rank, actually */
rankOccupancy = ((~empty) >> (rank + 1)) & 63;
mask = (u64)isoMove[file][rankOccupancy] << rank;
mask &= capture ? enemy : empty; /* These are all the legal destinations */
while (mask) {
GET_BIT_AND_CLEAR(mask, toSq);
pushMove(m, numMoves, pieceType, fromSq, toSq, 0);
}
}
}
/* Get all legal file moves / captures for queens and rooks */
inline void getFileMoves(int pieceType, bool capture, u64 pieceSet, u64 enemy, u64 empty, Move *m, int *numMoves) {
u64 fromSq, toSq, mask;
byte file, rank, fileOccupancy, moves;
while (pieceSet) {
GET_BIT_AND_CLEAR(pieceSet, fromSq);
file = fromSq & 7;
rank = fromSq >> 3;
// - Shift by file to bring the relevant bits on file A
// - And with FILE_A to discard everything else
// - Multiply with FILE_MAGIC to bring the relevant bits on the 8th rank
// - Shift and and to capture the six relevant occupancy bits.
fileOccupancy = (((((~empty) >> file) & FILE_A) * FILE_MAGIC) >> 57) & 63;
moves = isoMove[7 - rank][fileOccupancy];
mask = fileConfiguration[moves] << file;
mask &= capture ? enemy : empty; /* These are all the legal destinations */
while (mask) {
GET_BIT_AND_CLEAR(mask, toSq);
pushMove(m, numMoves, pieceType, fromSq, toSq, 0);
}
}
}
/* Get all legal diagonal / antidiagonal moves / captures for queens and bishops */
inline void getDiagonalMoves(int pieceType, u64 *diagArray, bool capture, u64 pieceSet, u64 enemy, u64 empty, Move *m, int *numMoves) {
u64 fromSq, toSq, mask;
byte file, occupancy, moves;
while (pieceSet) {
GET_BIT_AND_CLEAR(pieceSet, fromSq);
file = fromSq & 7;
// - Isolate the diagonal
// - Shift it up to the 8th rank
// - Shift down to capture the relevant occupancy bits
occupancy = ((((~empty) & diagArray[fromSq]) * DIAG_MAGIC) >> 57) & 63;
// - Look up the possible moves
// - Multiply it across all the columns
// - Apply the diagonal mask again to isolate the diagonal
moves = isoMove[file][occupancy];
mask = (moves * DIAG_MAGIC) & diagArray[fromSq];
mask &= capture ? enemy : empty; /* These are all the legal destinations */
while (mask) {
GET_BIT_AND_CLEAR(mask, toSq);
pushMove(m, numMoves, pieceType, fromSq, toSq, 0);
}
}
}
inline void getWhitePawnCaptures(Board *b, u64 ignoredFile, int shift, Move *m, int *numMoves) {
u64 mask = ((b->bb[BB_WP] & ~ignoredFile) << shift) & (b->bb[BB_BALL] | b->bb[BB_EP]);
byte fromSq, toSq;
while (mask) {
GET_BIT_AND_CLEAR(mask, toSq);
fromSq = toSq - shift;
if (toSq < 56) {
pushMove(m, numMoves, PAWN, fromSq, toSq, 0);
} else {
for (byte prom = KNIGHT; prom <= KING; prom++) {
pushMove(m, numMoves, PAWN, fromSq, toSq, prom);
}
}
}
}
inline void getBlackPawnCaptures(Board *b, u64 ignoredFile, int shift, Move *m, int *numMoves) {
u64 mask = ((b->bb[BB_BP] & ~ignoredFile) >> shift) & (b->bb[BB_WALL] | b->bb[BB_EP]);
byte fromSq, toSq;
while (mask) {
GET_BIT_AND_CLEAR(mask, toSq);
fromSq = toSq + shift;
if (toSq >= 8) {
pushMove(m, numMoves, PAWN, fromSq, toSq, 0);
} else {
for (byte prom = KNIGHT; prom <= KING; prom++) {
pushMove(m, numMoves, PAWN, fromSq, toSq, prom);
}
}
}
}
/* Get all legal non-capturing moves on the given board if White is to move */
int getWhiteNonCaptures(Board *b, Move *m, bool direction) {
int numMoves = 0, fromSq, toSq;
u64 mask;
if (direction == BACKWARD && b->bb[BB_EP]) {
// special case: the only move we can take back is the double push pawn
int epSq = ctz(b->bb[BB_EP]);
pushMove(m, &numMoves, PAWN, epSq + 8, epSq - 8, 0);
return numMoves;
}
if (direction == FORWARD) {
// Pawn pushes (one-step)
mask = (b->bb[BB_WP] << 8) & b->bb[BB_EMPTY];
u64 oneStep = mask;
while (mask) {
GET_BIT_AND_CLEAR(mask, toSq);
fromSq = toSq - 8;
if (toSq < 56) {
pushMove(m, &numMoves, PAWN, fromSq, toSq, 0);
} else {
for (byte prom = KNIGHT; prom <= KING; prom++) {
pushMove(m, &numMoves, PAWN, fromSq, toSq, prom);
}
}
}
// Pawn pushes (two-step)
mask = ((oneStep & RANK_3) << 8) & b->bb[BB_EMPTY];
while (mask) {
GET_BIT_AND_CLEAR(mask, toSq);
pushMove(m, &numMoves, PAWN, toSq - 16, toSq, 0);
}
} else {
// Pawn pushes (one-step)
mask = (b->bb[BB_WP] >> 8) & ~RANK_1 & b->bb[BB_EMPTY];
u64 oneStep = mask;
while (mask) {
GET_BIT_AND_CLEAR(mask, toSq);
pushMove(m, &numMoves, PAWN, toSq + 8, toSq, 0);
}
// see comment in movegen.h about two-step pawn unmoves
u64 blockers = b->bb[BB_BP] & RANK_4;
u64 blockerCaptures = ((blockers & ~FILE_A) >> 9) | ((blockers & ~FILE_H) >> 7);
mask = ((oneStep & RANK_3 & ~blockerCaptures) >> 8) & b->bb[BB_EMPTY];
while (mask) {
GET_BIT_AND_CLEAR(mask, toSq);
pushMove(m, &numMoves, PAWN, toSq + 16, toSq, 0);
}
}
getKnightMoves(b->bb[BB_WN], b->bb[BB_EMPTY], m, &numMoves);
getDiagonalMoves(BISHOP, diagonal, false, b->bb[BB_WB], 0, b->bb[BB_EMPTY], m, &numMoves);
getDiagonalMoves(BISHOP, antidiagonal, false, b->bb[BB_WB], 0, b->bb[BB_EMPTY], m, &numMoves);
getRankMoves(ROOK, false, b->bb[BB_WR], 0, b->bb[BB_EMPTY], m, &numMoves);
getFileMoves(ROOK, false, b->bb[BB_WR], 0, b->bb[BB_EMPTY], m, &numMoves);
getRankMoves(QUEEN, false, b->bb[BB_WQ], 0, b->bb[BB_EMPTY], m, &numMoves);
getFileMoves(QUEEN, false, b->bb[BB_WQ], 0, b->bb[BB_EMPTY], m, &numMoves);
getDiagonalMoves(QUEEN, diagonal, false, b->bb[BB_WQ], 0, b->bb[BB_EMPTY], m, &numMoves);
getDiagonalMoves(QUEEN, antidiagonal, false, b->bb[BB_WQ], 0, b->bb[BB_EMPTY], m, &numMoves);
getKingMoves(b->bb[BB_WK], b->bb[BB_EMPTY], m, &numMoves);
return numMoves;
}
/* Get all legal captures on the given board if White is to move */
int getWhiteCaptures(Board *b, Move *m) {
int numMoves = 0;
getWhitePawnCaptures(b, FILE_H, 9, m, &numMoves);
getWhitePawnCaptures(b, FILE_A, 7, m, &numMoves);
getKnightMoves(b->bb[BB_WN], b->bb[BB_BALL], m, &numMoves);
getDiagonalMoves(BISHOP, diagonal, true, b->bb[BB_WB], b->bb[BB_BALL], b->bb[BB_EMPTY], m, &numMoves);
getDiagonalMoves(BISHOP, antidiagonal, true, b->bb[BB_WB], b->bb[BB_BALL], b->bb[BB_EMPTY], m, &numMoves);
getRankMoves(ROOK, true, b->bb[BB_WR], b->bb[BB_BALL], b->bb[BB_EMPTY], m, &numMoves);
getFileMoves(ROOK, true, b->bb[BB_WR], b->bb[BB_BALL], b->bb[BB_EMPTY], m, &numMoves);
getRankMoves(QUEEN, true, b->bb[BB_WQ], b->bb[BB_BALL], b->bb[BB_EMPTY], m, &numMoves);
getFileMoves(QUEEN, true, b->bb[BB_WQ], b->bb[BB_BALL], b->bb[BB_EMPTY], m, &numMoves);
getDiagonalMoves(QUEEN, diagonal, true, b->bb[BB_WQ], b->bb[BB_BALL], b->bb[BB_EMPTY], m, &numMoves);
getDiagonalMoves(QUEEN, antidiagonal, true, b->bb[BB_WQ], b->bb[BB_BALL], b->bb[BB_EMPTY], m, &numMoves);
getKingMoves(b->bb[BB_WK], b->bb[BB_BALL], m, &numMoves);
return numMoves;
}
/* Get all legal non-capturing moves on the given board if Black is to move */
int getBlackNonCaptures(Board *b, Move *m, bool direction) {
int numMoves = 0, fromSq, toSq;
u64 mask;
if (direction == BACKWARD && b->bb[BB_EP]) {
// special case: the only move we can take back is the double push pawn
int epSq = ctz(b->bb[BB_EP]);
pushMove(m, &numMoves, PAWN, epSq - 8, epSq + 8, 0);
return numMoves;
}
if (direction == FORWARD) {
// Pawn pushes (one-step)
mask = (b->bb[BB_BP] >> 8) & b->bb[BB_EMPTY];
u64 oneStep = mask;
while (mask) {
GET_BIT_AND_CLEAR(mask, toSq);
fromSq = toSq + 8;
if (toSq >= 8) {
pushMove(m, &numMoves, PAWN, fromSq, toSq, 0);
} else {
for (byte prom = KNIGHT; prom <= KING; prom++) {
pushMove(m, &numMoves, PAWN, fromSq, toSq, prom);
}
}
}
// Pawn pushes (two-step)
mask = ((oneStep & RANK_6) >> 8) & b->bb[BB_EMPTY];
while (mask) {
GET_BIT_AND_CLEAR(mask, toSq);
pushMove(m, &numMoves, PAWN, toSq + 16, toSq, 0);
}
} else {
// Pawn pushes (one-step)
mask = (b->bb[BB_BP] << 8) & ~RANK_8 & b->bb[BB_EMPTY];
u64 oneStep = mask;
while (mask) {
GET_BIT_AND_CLEAR(mask, toSq);
pushMove(m, &numMoves, PAWN, toSq - 8, toSq, 0);
}
// see comment in movegen.h about two-step pawn unmoves
u64 blockers = b->bb[BB_WP] & RANK_5;
u64 blockerCaptures = ((blockers & ~FILE_A) << 7) | ((blockers & ~FILE_H) << 9);
mask = ((oneStep & RANK_6 & ~blockerCaptures) << 8) & b->bb[BB_EMPTY];
while (mask) {
GET_BIT_AND_CLEAR(mask, toSq);
pushMove(m, &numMoves, PAWN, toSq - 16, toSq, 0);
}
}
getKnightMoves(b->bb[BB_BN], b->bb[BB_EMPTY], m, &numMoves);
getDiagonalMoves(BISHOP, diagonal, false, b->bb[BB_BB], 0, b->bb[BB_EMPTY], m, &numMoves);
getDiagonalMoves(BISHOP, antidiagonal, false, b->bb[BB_BB], 0, b->bb[BB_EMPTY], m, &numMoves);
getRankMoves(ROOK, false, b->bb[BB_BR], 0, b->bb[BB_EMPTY], m, &numMoves);
getFileMoves(ROOK, false, b->bb[BB_BR], 0, b->bb[BB_EMPTY], m, &numMoves);
getRankMoves(QUEEN, false, b->bb[BB_BQ], 0, b->bb[BB_EMPTY], m, &numMoves);
getFileMoves(QUEEN, false, b->bb[BB_BQ], 0, b->bb[BB_EMPTY], m, &numMoves);
getDiagonalMoves(QUEEN, diagonal, false, b->bb[BB_BQ], 0, b->bb[BB_EMPTY], m, &numMoves);
getDiagonalMoves(QUEEN, antidiagonal, false, b->bb[BB_BQ], 0, b->bb[BB_EMPTY], m, &numMoves);
getKingMoves(b->bb[BB_BK], b->bb[BB_EMPTY], m, &numMoves);
return numMoves;
}
/* Get all legal moves on the given board if Black is to move */
int getBlackCaptures(Board *b, Move *m) {
int numMoves = 0;
getBlackPawnCaptures(b, FILE_H, 7, m, &numMoves);
getBlackPawnCaptures(b, FILE_A, 9, m, &numMoves);
getKnightMoves(b->bb[BB_BN], b->bb[BB_WALL], m, &numMoves);
getDiagonalMoves(BISHOP, diagonal, true, b->bb[BB_BB], b->bb[BB_WALL], b->bb[BB_EMPTY], m, &numMoves);
getDiagonalMoves(BISHOP, antidiagonal, true, b->bb[BB_BB], b->bb[BB_WALL], b->bb[BB_EMPTY], m, &numMoves);
getRankMoves(ROOK, true, b->bb[BB_BR], b->bb[BB_WALL], b->bb[BB_EMPTY], m, &numMoves);
getFileMoves(ROOK, true, b->bb[BB_BR], b->bb[BB_WALL], b->bb[BB_EMPTY], m, &numMoves);
getRankMoves(QUEEN, true, b->bb[BB_BQ], b->bb[BB_WALL], b->bb[BB_EMPTY], m, &numMoves);
getFileMoves(QUEEN, true, b->bb[BB_BQ], b->bb[BB_WALL], b->bb[BB_EMPTY], m, &numMoves);
getDiagonalMoves(QUEEN, diagonal, true, b->bb[BB_BQ], b->bb[BB_WALL], b->bb[BB_EMPTY], m, &numMoves);
getDiagonalMoves(QUEEN, antidiagonal, true, b->bb[BB_BQ], b->bb[BB_WALL], b->bb[BB_EMPTY], m, &numMoves);
getKingMoves(b->bb[BB_BK], b->bb[BB_WALL], m, &numMoves);
return numMoves;
}
int getAllMoves(Board *b, Move *m, bool direction) {
if (direction == BACKWARD) {
// Notice the color inversion -- if White is to move, then Black must have made the last move
return (b->side == WHITE)
? getBlackNonCaptures(b, m, direction)
: getWhiteNonCaptures(b, m, direction);
}
int numMoves = (b->side == WHITE) ? getWhiteCaptures(b, m) : getBlackCaptures(b, m);
if (!numMoves) {
numMoves = (b->side == WHITE)
? getWhiteNonCaptures(b, m, direction)
: getBlackNonCaptures(b, m, direction);
}
return numMoves;
}