-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMovegenerators.h
381 lines (360 loc) · 12.5 KB
/
Movegenerators.h
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
#ifndef MOVEGENERATORS_H
#define MOVEGENERATORS_H
// Extends Board.h
template<moveGenType genType> void Board::generateMoveList(MoveList& moveList, color side)
{
/*
This method generates a list of all possible moves for a player.
Aggressive and special moves are generated first and preferably stored
at the front of the movelist
*/
ulong pos = nullSquare;
U64 attackMask = 0x0;
U64 pieceAttacks = 0x0, attackingPieces = 0x0;
// Generate all capturing and normal moves
updatePinnedPieces(side);
if (side == black) {
/*
::::::::::::::::::::::::::::::::::
:: BLACK MOVE GENERATION ::
::::::::::::::::::::::::::::::::::
*/
wasInCheck = (pieces[bk] & whiteAtt) > 0;
for_black(b) { // Loop through black pieces
attackingPieces = pieces[b];
if (attackingPieces) { // Only consider non-empty boards
switch (b) {
case bp: pawnMoves<genType, black>(moveList); break;
case br: rookMoves<genType, black>(moveList); break;
case bn: knightMoves<genType, black>(moveList); break;
case bb: queen_and_bishopMoves<genType, black, false>(moveList); break;
case bq: queen_and_bishopMoves<genType, black, true>(moveList); break;
case bk: kingMoves<genType, black>(moveList); break;
}
}
}
if (genType != CAPTURES_ONLY) {
// Generate castling moves
// Black King can castle if there are no pieces between king and rook, both havent moved yet and king
// does not cross attacked squares during castling, same for white
if ((castlingRights & castle_k) && !(allPos & 0x600000000000000ull) && !(whiteAtt & 0xE00000000000000ull)) {
moveList.emplace_back(castlingRights, BCASTLE);
}
if ((castlingRights & castle_q) && !(allPos & 0x7000000000000000ull) && !(whiteAtt & 0x3800000000000000ull)) { // Black King can castle (big)
moveList.emplace_back(castlingRights, BCASTLE_2);
}
}
}
else {
/*
::::::::::::::::::::::::::::::::::
:: WHITE MOVE GENERATION ::
::::::::::::::::::::::::::::::::::
*/
wasInCheck = (pieces[wk] & blackAtt) > 0;
for_white(w) { // Loop through white pieces
attackingPieces = pieces[w];
if (attackingPieces) {
switch (w) {
case wp: pawnMoves<genType, white>(moveList); break;
case wr: rookMoves<genType, white>(moveList); break;
case wn: knightMoves<genType, white>(moveList); break;
case wb: queen_and_bishopMoves<genType, white, false>(moveList); break;
case wq: queen_and_bishopMoves<genType, white, true>(moveList); break;
case wk: kingMoves<genType, white>(moveList); break;
}
}
}
if (genType != CAPTURES_ONLY) {
// Castling permission King-rook path is not obstructed and not under attack
if (castlingRights & castle_K && !(allPos & 0x6ull) && !(blackAtt & 0xEull)) { // White King can castle
moveList.emplace_back(castlingRights, WCASTLE);
}
if (castlingRights & castle_Q && !(allPos & 0x70ull) && !(blackAtt & 0x38ull)) { // White King can castle queenside
moveList.emplace_back(castlingRights, WCASTLE_2);
}
}
}
// If opponent rook has been captured, he looses castling rights.
// TODO: Needs nicer solution
//if (isKingInCheck(side))
// reduceMoveList(moveList, side);
if (castlingRights & (side == black ? 0b1100 : 0b0011)) {
for (auto& move : moveList) {
if (move.targetPiece() == wr) {
if (move.mtype() == CAPTURE || move.mtype() == C_PROMOTION) {
if (move.to == a1)
move.flags |= (castlingRights & castle_Q) << 4;
else if (move.to == h1)
move.flags |= (castlingRights & castle_K) << 4;
}
}
else if (move.targetPiece() == br) {
if (move.mtype() == CAPTURE || move.mtype() == C_PROMOTION) {
if (move.to == a8)
move.flags |= (castlingRights & castle_q) << 4;
else if (move.to == h8)
move.flags |= (castlingRights & castle_k) << 4;
}
}
}
}
}
template<moveGenType mgt, color side>
void inline Board::pawnMoves(MoveList& moveList) const
{
U64 attackMask = 0x0, pieceAttacks = 0x0;
piece p = side == white ? wp : bp;
U64 attackingPieces = pieces[p];
if(side == black){
if (mgt & CAPTURES_ONLY) {
// Find normal captures:
for_bits(pos, attackingPieces) {
attackMask = ((bit_at(pos) >> 9) & ~_left) & whitePos;
attackMask |= ((bit_at(pos) >> 7) & ~_right) & whitePos;
for_white(candidate) {
pieceAttacks = pieces[candidate] & attackMask;
for_bits(target, pieceAttacks) {
if (target < 8) {
moveList.emplace_back(pos, target, C_PROMOTION, piece_pair(candidate, bq));
moveList.emplace_back(pos, target, C_PROMOTION, piece_pair(candidate, bn));
moveList.emplace_back(pos, target, C_PROMOTION, piece_pair(candidate, br));
moveList.emplace_back(pos, target, C_PROMOTION, piece_pair(candidate, bb));
}
else {
moveList.emplace_back(pos, target, CAPTURE, piece_pair(bp, candidate));
}
}
}
}
// Enpassent
if (b_enpassant) {
// There surely exists an enpassant move
if ((bit_at(24 + b_enpassant) & pieces[bp]) & (_row << 24)) {
// black pawn right of ep square
moveList.emplace_back(24 + b_enpassant, 15 + b_enpassant, ENPASSANT, bp);
}
if (bit_at(22 + b_enpassant) & pieces[bp] & (_row << 24)) {
// black pawn left of ep square
moveList.emplace_back(22 + b_enpassant, 15 + b_enpassant, ENPASSANT, bp);
}
}
}
if (mgt & QUIET_ONLY) {
// Find normal upwards moves and double pawn steps:
attackingPieces = (pieces[bp] >> 8) & bpMove;
for_bits(pos, attackingPieces) {
if (pos > 7) {
moveList.emplace_back(pos + 8, pos, MOVE, bp);
}
else {
moveList.emplace_back(pos + 8, pos, PROMOTION, piece_pair(bp, bq));
moveList.emplace_back(pos + 8, pos, PROMOTION, piece_pair(bp, bn));
moveList.emplace_back(pos + 8, pos, PROMOTION, piece_pair(bp, br));
moveList.emplace_back(pos + 8, pos, PROMOTION, piece_pair(bp, bb));
}
}
// Double pawn move
attackingPieces = ((((0x00FF000000000000 & pieces[bp]) >> 8) & bpMove) >> 8) & bpMove;
for_bits(pos, attackingPieces) {
moveList.emplace_back(pos + 16, pos, PAWN2, bp);
}
}
}
else {
if (mgt & CAPTURES_ONLY) {
// Find normal captures:
// attackingPieces stands for attacked squares in this case
for_bits(pos, attackingPieces) {
attackMask = (bit_at(pos) << 9 & ~_right) & blackPos;
attackMask |= (bit_at(pos) << 7 & ~_left) & blackPos;
for_black(candidate) {
pieceAttacks = pieces[candidate] & attackMask;
for_bits(target, pieceAttacks) {
if (target > 55) {
moveList.emplace_back(pos, target, C_PROMOTION, piece_pair(candidate, wq));
moveList.emplace_back(pos, target, C_PROMOTION, piece_pair(candidate, wn));
moveList.emplace_back(pos, target, C_PROMOTION, piece_pair(candidate, wr));
moveList.emplace_back(pos, target, C_PROMOTION, piece_pair(candidate, wb));
}
else {
moveList.emplace_back(pos, target, CAPTURE, piece_pair(wp, candidate));
}
}
}
}
if (w_enpassant) {
// There surely exists an enpassant move
if ((bit_at(30 + w_enpassant) & pieces[wp]) & (_row << 32)) {
// white pawn right of ep square
moveList.emplace_back(30 + w_enpassant, 39 + w_enpassant, ENPASSANT, wp);
}
if (bit_at(32 + w_enpassant) & pieces[wp] & (_row << 32)) {
// white pawn left of ep square
moveList.emplace_back(32 + w_enpassant, 39 + w_enpassant, ENPASSANT, wp);
}
}
}
if (mgt & QUIET_ONLY) {
// Find normal upwards moves and double pawn steps:
attackingPieces = (pieces[wp] << 8) & wpMove;
for_bits(pos, attackingPieces) {
if (pos < 56) {
moveList.emplace_back(pos - 8, pos, MOVE, wp);
}
else {
moveList.emplace_back(pos - 8, pos, PROMOTION, piece_pair(wp, wq));
moveList.emplace_back(pos - 8, pos, PROMOTION, piece_pair(wp, wn));
moveList.emplace_back(pos - 8, pos, PROMOTION, piece_pair(wp, wr));
moveList.emplace_back(pos - 8, pos, PROMOTION, piece_pair(wp, wb));
}
}
attackingPieces = ((((0xFF00 & pieces[wp]) << 8) & wpMove) << 8) & wpMove;
for_bits(pos, attackingPieces) {
moveList.emplace_back(pos - 16, pos, PAWN2, wp);
}
}
}
}
template<moveGenType mgt, color side>
void inline Board::knightMoves(MoveList& moveList) const
{
U64 attackingPieces = pieces[side == white ? wn : bn];
piece p = side == white ? wn : bn;
U64 attackMask = 0x0, pieceAttacks = 0x0;
for_bits(pos, attackingPieces) {
attackMask = KNIGHT_ATTACKS[pos] & (side == black ? whitePos : blackPos);
if (mgt & CAPTURES_ONLY) {
for_color(candidate, !side) {
pieceAttacks = pieces[candidate] & attackMask;
if (pieceAttacks) {
for_bits(target, pieceAttacks) {
moveList.emplace_back(pos, target, CAPTURE, piece_pair(p, candidate));
}
}
}
}
if (mgt & QUIET_ONLY) {
attackMask ^= KNIGHT_ATTACKS[pos] & attacks[p];
for_bits(target, attackMask) {
moveList.emplace_back(pos, target, MOVE, p);
}
}
}
}
template<moveGenType mgt, color side, bool Q>
void inline Board::queen_and_bishopMoves(MoveList& moveList) const
{
U64 attackMask = 0x0, pieceAttacks = 0x0;
piece p = Q ? (side == white ? wq : bq) : (side == white ? wb : bb);
U64 attackingPieces = pieces[p];
for_bits(pos, attackingPieces) {
attackMask = (Q ? QUEEN_ATTACKS : BISHOP_ATTACKS)[pos] & attacks[p] & (side == black ? whitePos : blackPos);
if (mgt & CAPTURES_ONLY) {
for_color(candidate, !side) {
pieceAttacks = pieces[candidate] & attackMask;
if (pieceAttacks) {
for_bits(target, pieceAttacks) {
if (!(CONNECTIONS[pos][target] & allPos)) {
moveList.emplace_back(pos, target, CAPTURE, piece_pair(p, candidate));
}
}
}
}
}
if(mgt & QUIET_ONLY){
attackMask ^= (Q ? QUEEN_ATTACKS : BISHOP_ATTACKS)[pos] & attacks[p];
for_bits(target, attackMask) {
//moveList.reserve(moveList.size() + popcount(attackMask));
if (!(CONNECTIONS[pos][target] & allPos)) {
moveList.emplace_back(pos, target, MOVE, p);
}
}
}
}
}
template<moveGenType mgt, color side>
void inline Board::kingMoves(MoveList& moveList) const
{
piece king = side == white ? wk : bk;
U64 attackMask = 0x0, pieceAttacks = 0x0;
ulong pos = msb(pieces[king]);
attackMask = ((attacks[king] & (side == black ? whitePos : blackPos)) & ~(side == black ? whiteAtt : blackAtt));
if (mgt & CAPTURES_ONLY) {
for_color(candidate, !side) {
pieceAttacks = pieces[candidate] & attackMask;
if (pieceAttacks) {
for_bits(target, pieceAttacks) {
moveList.emplace_back(pos, target, move_metadata(CAPTURE, castlingRights & (side == black ? 0x3 : 0xC)), piece_pair(king, candidate));
}
}
}
}
if (mgt & QUIET_ONLY) {
attackMask ^= (KING_ATTACKS[pos] & attacks[king]) & ~(side == black ? whiteAtt : blackAtt);
for_bits(target, attackMask) {
moveList.emplace_back(pos, target, move_metadata(MOVE, castlingRights & (side == black ? 0x3 : 0xC)), king);
}
}
}
template<moveGenType mgt, color side>
void inline Board::rookMoves(MoveList& moveList) const
{
piece rook = side == white ? wr : br;
U64 attackingPieces = pieces[rook];
U64 attackMask = 0x0, pieceAttacks = 0x0;
ulong a_square, h_square, qCastRight, kCastRight;
if (side == black) {
a_square = a8;
h_square = h8;
qCastRight = castle_q;
kCastRight = castle_k;
}
else {
a_square = a1;
h_square = h1;
qCastRight = castle_Q;
kCastRight = castle_K;
}
// Calculate attacked pieces
for_bits(pos, attackingPieces) {
attackMask = ROOK_ATTACKS[pos] & attacks[rook] & (side == black ? whitePos : blackPos);
if (CAPTURES_ONLY) {
if (attackMask) {
for_color(candidate, !side) {
pieceAttacks = pieces[candidate] & attackMask;
for_bits(target, pieceAttacks) {
if (!(CONNECTIONS[pos][target] & allPos)) {
if (pos == a_square) {
moveList.emplace_back(pos, target, move_metadata(CAPTURE, castlingRights & qCastRight), piece_pair(rook, candidate));
}
else if (pos == h_square) {
moveList.emplace_back(pos, target, move_metadata(CAPTURE, castlingRights & kCastRight), piece_pair(rook, candidate));
}
else {
moveList.emplace_back(pos, target, CAPTURE, piece_pair(rook, candidate));
}
}
}
}
}
}
if (mgt & QUIET_ONLY) {
attackMask ^= ROOK_ATTACKS[pos] & attacks[rook];
for_bits(target, attackMask) {
if (!(CONNECTIONS[pos][target] & allPos)) {
if (pos == a_square) {
moveList.emplace_back(pos, target, move_metadata(MOVE, castlingRights & qCastRight), rook);
}
else if (pos == h_square) {
moveList.emplace_back(pos, target, move_metadata(MOVE, castlingRights & kCastRight), rook);
}
else {
moveList.emplace_back(pos, target, MOVE, rook);
}
}
}
}
}
}
#endif