-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
movegenerator.h
executable file
·385 lines (340 loc) · 16.6 KB
/
movegenerator.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
382
383
384
385
#ifndef MOVEGENERATOR_H
#define MOVEGENERATOR_H
#include "defines.h"
#include "board.h"
#include "move.h"
#include "pawn.h"
#include "knight.h"
#include "bishop.h"
#include "rook.h"
#include "queen.h"
#include "king.h"
#include "constants.h"
#include "piece.h"
#include "movedatabase.h"
#include "utils.h"
namespace Napoleon
{
namespace MoveGenerator
{
int MoveCount(Board&);
template<bool>
void GetPseudoLegalMoves(Move allMoves[],int& pos, BitBoard attackers, Board& board);
void GetLegalMoves(Move allMoves[],int& pos, Board& board);
void GetAllMoves(Move allMoves[], int& pos, Board& board);
template<bool>
void GetPawnMoves(BitBoard pawns, Board& board, Move moveList[], int& pos, BitBoard target);
void GetKingMoves(BitBoard king, Board& board, Move moveList[], int& pos, BitBoard target);
void GetKnightMoves(BitBoard knights, Board& board, Move moveList[], int& pos, BitBoard target);
void GetRookMoves(BitBoard rooks, Board& board, Move moveList[], int& pos, BitBoard target);
void GetBishopMoves(BitBoard bishops, Board& board, Move moveList[],int& pos, BitBoard target);
void GetQueenMoves(BitBoard queens, Board& board, Move moveList[], int& pos, BitBoard target);
void GetCastleMoves(Board& board, Move moveList[], int& pos);
template<bool>
void GetEvadeMoves(Board& board, BitBoard attackers, Move moveList[], int& pos);
void GetCaptures(Move allMoves[], int& pos, Board& board);
void GetCapturesAndPromotions(Move allMoves[], int& pos, Board& board);
void GetNonCaptures(Move allMoves[], int& pos, Board& board);
}
inline int MoveGenerator::MoveCount(Board& board)
{
int count = 0;
Move moves[Constants::MaxMoves];
GetLegalMoves(moves, count, board);
return count;
}
template<bool onlyCaptures>
INLINE void MoveGenerator::GetPseudoLegalMoves(Move allMoves[], int& pos, BitBoard attackers, Board& board)
{
if (attackers)
{
GetEvadeMoves<onlyCaptures>(board, attackers, allMoves, pos);
board.SetCheckState(true);
}
else if (onlyCaptures)
{
GetCapturesAndPromotions(allMoves, pos, board);
board.SetCheckState(false);
}
else
{
GetAllMoves(allMoves, pos, board);
board.SetCheckState(false);
}
}
INLINE void MoveGenerator::GetAllMoves(Move allMoves[], int& pos, Board& board)
{
GetCaptures(allMoves, pos, board);
GetNonCaptures(allMoves, pos, board);
}
INLINE void MoveGenerator::GetCaptures(Move allMoves[], int& pos, Board& board)
{
BitBoard enemy = board.EnemyPieces();
GetPawnMoves<true>(board.Pieces(board.SideToMove(), PieceType::Pawn), board, allMoves, pos, enemy);
GetKnightMoves(board.Pieces(board.SideToMove(), PieceType::Knight), board, allMoves, pos, enemy);
GetBishopMoves(board.Pieces(board.SideToMove(), PieceType::Bishop), board, allMoves, pos, enemy);
GetQueenMoves(board.Pieces(board.SideToMove(), PieceType::Queen), board, allMoves, pos, enemy);
GetKingMoves(board.Pieces(board.SideToMove(), PieceType::King), board, allMoves, pos, enemy);
GetRookMoves(board.Pieces(board.SideToMove(), PieceType::Rook), board, allMoves, pos, enemy);
}
INLINE void MoveGenerator::GetCapturesAndPromotions(Move allMoves[], int& pos, Board& board)
{
BitBoard enemy = board.EnemyPieces();
GetPawnMoves<true>(board.Pieces(board.SideToMove(), PieceType::Pawn), board, allMoves, pos,
enemy | Constants::Masks::RankMask[Utils::Square::RelativeRank(board.SideToMove(), 7)]);
GetKnightMoves(board.Pieces(board.SideToMove(), PieceType::Knight), board, allMoves, pos, enemy);
GetBishopMoves(board.Pieces(board.SideToMove(), PieceType::Bishop), board, allMoves, pos, enemy);
GetQueenMoves(board.Pieces(board.SideToMove(), PieceType::Queen), board, allMoves, pos, enemy);
GetKingMoves(board.Pieces(board.SideToMove(), PieceType::King), board, allMoves, pos, enemy);
GetRookMoves(board.Pieces(board.SideToMove(), PieceType::Rook), board, allMoves, pos, enemy);
}
INLINE void MoveGenerator::GetNonCaptures(Move allMoves[], int&pos, Board& board)
{
BitBoard enemy = ~board.EnemyPieces();
GetPawnMoves<false>(board.Pieces(board.SideToMove(), PieceType::Pawn), board, allMoves, pos, enemy);
GetKnightMoves(board.Pieces(board.SideToMove(), PieceType::Knight), board, allMoves, pos, enemy);
GetBishopMoves(board.Pieces(board.SideToMove(), PieceType::Bishop), board, allMoves, pos, enemy);
GetQueenMoves(board.Pieces(board.SideToMove(), PieceType::Queen), board, allMoves, pos, enemy);
GetKingMoves(board.Pieces(board.SideToMove(), PieceType::King), board, allMoves, pos, enemy);
GetRookMoves(board.Pieces(board.SideToMove(), PieceType::Rook), board, allMoves, pos, enemy);
GetCastleMoves(board, allMoves, pos);
}
template<bool ep>
INLINE void MoveGenerator::GetPawnMoves(BitBoard pawns, Board& board, Move moveList[], int& pos, BitBoard target)
{
BitBoard targets;
BitBoard epTargets;
Square fromIndex;
Square toIndex;
while (pawns != 0)
{
fromIndex = Utils::BitBoard::BitScanForwardReset(pawns); // search for LS1B and then reset it
targets = Pawn::GetAllTargets(Constants::Masks::SquareMask[fromIndex], board) & target;
if (ep)
{
// en passant
if (board.EnPassantSquare() != Constants::Squares::Invalid)
{
epTargets = MoveDatabase::PawnAttacks[board.SideToMove()][fromIndex];
if ((epTargets & Constants::Masks::SquareMask[board.EnPassantSquare()]) != 0)
moveList[pos++] = Move(fromIndex, board.EnPassantSquare(), EnPassant);
}
}
while (targets != 0)
{
toIndex = Utils::BitBoard::BitScanForwardReset(targets); // search for LS1B and then reset it
// promotions
if ((Utils::Square::GetRankIndex(toIndex) == 7 && board.SideToMove() == PieceColor::White) ||
(Utils::Square::GetRankIndex(toIndex) == 0 && board.SideToMove() == PieceColor::Black))
{
moveList[pos++] = Move(fromIndex, toIndex, QueenPromotion);
moveList[pos++] = Move(fromIndex, toIndex, RookPromotion);
moveList[pos++] = Move(fromIndex, toIndex, BishopPromotion);
moveList[pos++] = Move(fromIndex, toIndex, KnightPromotion);
}
else
{
moveList[pos++] = Move(fromIndex, toIndex); // no promotions
}
}
}
}
INLINE void MoveGenerator::GetKnightMoves(BitBoard knights, Board &board, Move moveList[], int& pos, BitBoard target)
{
BitBoard targets;
Square fromIndex;
Square toIndex;
while (knights != 0)
{
fromIndex = Utils::BitBoard::BitScanForwardReset(knights); // search for LS1B and then reset it
targets = Knight::GetAllTargets(Constants::Masks::SquareMask[fromIndex], board) & target;
while (targets != 0)
{
toIndex = Utils::BitBoard::BitScanForwardReset(targets); // search for LS1B and then reset it
moveList[pos++] = Move(fromIndex, toIndex);
}
}
}
INLINE void MoveGenerator::GetBishopMoves(BitBoard bishops, Board &board, Move moveList[], int& pos, BitBoard target)
{
BitBoard targets;
Square fromIndex;
Square toIndex;
while (bishops != 0)
{
fromIndex = Utils::BitBoard::BitScanForwardReset(bishops); // search for LS1B and then reset it
targets = Bishop::GetAllTargets(Constants::Masks::SquareMask[fromIndex], board) & target;
while (targets != 0)
{
toIndex = Utils::BitBoard::BitScanForwardReset(targets); // search for LS1B and then reset it
moveList[pos++] = Move(fromIndex, toIndex);
}
}
}
INLINE void MoveGenerator::GetKingMoves(BitBoard king, Board &board, Move moveList[], int &pos, BitBoard target)
{
BitBoard targets;
Square fromIndex;
Square toIndex;
while (king != 0)
{
fromIndex = Utils::BitBoard::BitScanForwardReset(king); // search for LS1B and then reset it
targets = King::GetAllTargets(Constants::Masks::SquareMask[fromIndex], board) & target;
while (targets != 0)
{
toIndex = Utils::BitBoard::BitScanForwardReset(targets); // search for LS1B and then reset it
moveList[pos++] = Move(fromIndex, toIndex);
}
}
}
INLINE void MoveGenerator::GetRookMoves(BitBoard rooks, Board &board, Move moveList[], int &pos, BitBoard target)
{
BitBoard targets;
Square fromIndex;
Square toIndex;
while (rooks != 0)
{
fromIndex = Utils::BitBoard::BitScanForwardReset(rooks); // search for LS1B and then reset it
targets = Rook::GetAllTargets(Constants::Masks::SquareMask[fromIndex], board) & target;
while (targets != 0)
{
toIndex = Utils::BitBoard::BitScanForwardReset(targets); // search for LS1B and then reset it
moveList[pos++] = Move(fromIndex, toIndex);
}
}
}
INLINE void MoveGenerator::GetCastleMoves(Board& board, Move moveList[], int &pos)
{
if (board.SideToMove() == PieceColor::White)
{
if (board.CastlingStatus() & Constants::Castle::WhiteCastleOO)
{
if ((Constants::Castle::WhiteCastleMaskOO & board.OccupiedSquares) == 0)
{
if (board.KingSquare(PieceColor::White) == Constants::Squares::E1 &&
!board.IsAttacked(Constants::Castle::WhiteCastleMaskOO, board.SideToMove()))
moveList[pos++] = Constants::Castle::WhiteCastlingOO;
}
}
if (board.CastlingStatus() & Constants::Castle::WhiteCastleOOO)
{
if ((Constants::Castle::WhiteCastleMaskOOO & board.OccupiedSquares) == 0)
{
if (board.KingSquare(PieceColor::White) == Constants::Squares::E1 &&
!board.IsAttacked(Constants::Castle::WhiteCastleMaskOOO
^ Constants::Squares::B1, board.SideToMove()))
moveList[pos++] = Constants::Castle::WhiteCastlingOOO;
}
}
}
else if (board.SideToMove() == PieceColor::Black)
{
if (board.CastlingStatus() & Constants::Castle::BlackCastleOO)
{
if ((Constants::Castle::BlackCastleMaskOO & board.OccupiedSquares) == 0)
{
if (board.KingSquare(PieceColor::Black) == Constants::Squares::E8 &&
!board.IsAttacked(Constants::Castle::BlackCastleMaskOO, board.SideToMove()))
moveList[pos++] = Constants::Castle::BlackCastlingOO;
}
}
if (board.CastlingStatus() & Constants::Castle::BlackCastleOOO)
{
if ((Constants::Castle::BlackCastleMaskOOO & board.OccupiedSquares) == 0)
{
if (board.KingSquare(PieceColor::Black) == Constants::Squares::E8 &&
!board.IsAttacked(Constants::Castle::BlackCastleMaskOOO^ Constants::Squares::B8, board.SideToMove()))
moveList[pos++] = Constants::Castle::BlackCastlingOOO;
}
}
}
}
//thanks stockfish for this
template<bool onlyCaptures>
void MoveGenerator::GetEvadeMoves(Board& board, BitBoard checkers, Move moveList[], int& pos)
{
BitBoard b;
Square to;
Square from, checksq;
Square ksq = board.KingSquare(board.SideToMove());
int checkersCnt = 0;
BitBoard sliderAttacks = 0;
// Find squares attacked by slider checkers, we will remove them from the king
// evasions so to skip known illegal moves avoiding useless legality check later.
b = checkers;
do
{
checkersCnt++;
checksq = Utils::BitBoard::BitScanForwardReset(b);
switch (board.PieceOnSquare(checksq).Type)
{
case PieceType::Bishop: sliderAttacks |= MoveDatabase::PseudoBishopAttacks[checksq]; break;
case PieceType::Rook: sliderAttacks |= MoveDatabase::PseudoRookAttacks[checksq]; break;
case PieceType::Queen:
// If queen and king are far or not on a diagonal line we can safely
// remove all the squares attacked in the other direction becuase are
// not reachable by the king anyway.
if ( MoveDatabase::ObstructedTable[ksq][checksq] || (MoveDatabase::PseudoBishopAttacks[checksq] & Constants::Masks::SquareMask[ksq])==0)
sliderAttacks |= MoveDatabase::PseudoBishopAttacks[checksq]
| MoveDatabase::PseudoRookAttacks[checksq];
// Otherwise we need to use real rook attacks to check if king is safe
// to move in the other direction. For example: king in B2, queen in A1
// a knight in B1, and we can safely move to C1.
else
sliderAttacks |= MoveDatabase::PseudoBishopAttacks[checksq] | MoveDatabase::GetRookAttacks(board.OccupiedSquares, checksq);
break;
default:
break;
}
} while (b);
// Generate evasions for king, capture and non capture moves
if (onlyCaptures)
b = MoveDatabase::KingAttacks[ksq] & checkers;
else
b = MoveDatabase::KingAttacks[ksq] & ~board.PlayerPieces() & ~sliderAttacks;
from = ksq;
while (b)
{
to = Utils::BitBoard::BitScanForwardReset(b); // search for LS1B and then reset it
moveList[pos++] = Move(from, to);
}
// Generate evasions for other pieces only if not under a double check
if (checkersCnt > 1)
return;
// Blocking evasions or captures of the checking piece
BitBoard target;
if (onlyCaptures)
target = checkers;
else
target = MoveDatabase::ObstructedTable[checksq][ksq] | checkers;
GetPawnMoves<true>(board.Pieces(board.SideToMove(), PieceType::Pawn), board, moveList, pos, target);
GetKnightMoves(board.Pieces(board.SideToMove(), PieceType::Knight), board, moveList, pos, target);
GetBishopMoves(board.Pieces(board.SideToMove(), PieceType::Bishop), board, moveList, pos, target);
GetRookMoves(board.Pieces(board.SideToMove(), PieceType::Rook), board, moveList, pos, target);
GetQueenMoves(board.Pieces(board.SideToMove(), PieceType::Queen), board, moveList, pos, target);
}
INLINE void MoveGenerator::GetLegalMoves(Move allMoves[],int& pos, Board& board)
{
BitBoard pinned = board.PinnedPieces();
BitBoard attackers = board.KingAttackers(board.KingSquare(board.SideToMove()), board.SideToMove());
if (attackers)
GetEvadeMoves<false>(board, attackers, allMoves, pos);
else
GetAllMoves(allMoves, pos, board);
int last = pos;
int cur = 0;
while (cur != last)
{
if (!board.IsMoveLegal(allMoves[cur], pinned))
{
allMoves[cur] = allMoves[--last];
}
else
{
cur++;
}
}
pos = last;
}
}
#endif // MOVEGENERATOR_H