-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMoveGeneration.cs
More file actions
385 lines (323 loc) · 16.5 KB
/
MoveGeneration.cs
File metadata and controls
385 lines (323 loc) · 16.5 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
using System.Collections.Generic;
using System.Reflection.Metadata;
using chessBot.ui;
namespace chessBot
{
public class MoveGeneration
{
public static List<Move> generateMoves(Board board)
{
List<Move> moves = [];
ulong whitePiecesBitboard = board.getWhitePiecesBitboard();
ulong blackPiecesBitboard = board.getBlackPiecesBitboard();
Side side = board.sideToMove;
bool canCastleKingSide = side.Equals(Side.White) ? board.whiteCanCastleKingSide : board.blackCanCastleKingSide;
bool canCastleQueenSide = side.Equals(Side.White) ? board.whiteCanCastleQueenSide : board.blackCanCastleQueenSide;
Piece pawn = side.Equals(Side.White) ? Piece.WhitePawn : Piece.BlackPawn;
Piece knight = side.Equals(Side.White) ? Piece.WhiteKnight : Piece.BlackKnight;
Piece bishop = side.Equals(Side.White) ? Piece.WhiteBishop : Piece.BlackBishop;
Piece rook = side.Equals(Side.White) ? Piece.WhiteRook : Piece.BlackRook;
Piece queen = side.Equals(Side.White) ? Piece.WhiteQueen : Piece.BlackQueen;
Piece king = side.Equals(Side.White) ? Piece.WhiteKing : Piece.BlackKing;
Piece opposingKing = side.Equals(Side.White) ? Piece.BlackKing : Piece.WhiteKing;
ulong opposingKingBitboard = board.bitboards[(byte)opposingKing];
moves.AddRange(generatePseudoLegalKnightMoves(board.bitboards[(byte)knight], whitePiecesBitboard, blackPiecesBitboard, side, opposingKingBitboard));
moves.AddRange(generatePseudoLegalPawnMoves(board.bitboards[(byte)pawn], whitePiecesBitboard, blackPiecesBitboard, side, board.enPassantSquare, opposingKingBitboard));
moves.AddRange(generatePseudoLegalKingMoves(board.bitboards[(byte)king], whitePiecesBitboard, blackPiecesBitboard, side, canCastleKingSide, canCastleQueenSide, opposingKingBitboard, board));
moves.AddRange(generatePseudoLegalRookMoves(board.bitboards[(byte)rook], whitePiecesBitboard, blackPiecesBitboard, side, opposingKingBitboard));
moves.AddRange(generatePseudoLegalBishopMoves(board.bitboards[(byte)bishop], whitePiecesBitboard, blackPiecesBitboard, side, opposingKingBitboard));
moves.AddRange(generatePseudoLegalQueenMoves(board.bitboards[(byte)queen], whitePiecesBitboard, blackPiecesBitboard, side, opposingKingBitboard));
return moves;
}
public static List<Move> generatePseudoLegalKnightMoves(ulong knightsBitboard, ulong whitePiecesBitboard, ulong blackPiecesBitboard, Side sideToMove, ulong opposingKingBitboard)
{
List<Move> moves = [];
ulong opposingPiecesBitboard = blackPiecesBitboard;
ulong currentPiecesBitboard = whitePiecesBitboard;
Piece currentSideKnight = Piece.WhiteKnight;
if (sideToMove.Equals(Side.Black))
{
opposingPiecesBitboard = whitePiecesBitboard;
currentPiecesBitboard = blackPiecesBitboard;
currentSideKnight = Piece.BlackKnight;
}
while (knightsBitboard != 0)
{
byte pieceIndex = knightsBitboard.popLSB();
ulong knightAttack = Attacks.KnightAttacks[pieceIndex];
ulong attackedSquaresBitboard = knightAttack & opposingPiecesBitboard & ~opposingKingBitboard;
while (attackedSquaresBitboard != 0)
{
byte captureIndex = attackedSquaresBitboard.popLSB();
moves.Add(new Move(pieceIndex, captureIndex, currentSideKnight, MoveFlags.Capture));
}
ulong regularMove = knightAttack & ~(currentPiecesBitboard | opposingPiecesBitboard);
while (regularMove != 0)
{
byte regularMoveIndex = regularMove.popLSB();
moves.Add(new Move(pieceIndex, regularMoveIndex, currentSideKnight, MoveFlags.None));
}
}
return moves;
}
public static List<Move> generatePseudoLegalPawnMoves(ulong pawnBitboard, ulong whitePiecesBitboard, ulong blackPiecesBitboard, Side sideToMove, EnPassantSquare? enPassantSquare, ulong opposingKingBitboard)
{
List<Move> moves = [];
ulong opposingPiecesBitboard = blackPiecesBitboard;
ulong currentPiecesBitboard = whitePiecesBitboard;
ulong potentialPromotingPieces = Constants.Rank7 & pawnBitboard;
Piece currentSidePawn = Piece.WhitePawn;
if (sideToMove.Equals(Side.Black))
{
opposingPiecesBitboard = whitePiecesBitboard;
currentPiecesBitboard = blackPiecesBitboard;
currentSidePawn = Piece.BlackPawn;
potentialPromotingPieces = Constants.Rank2 & pawnBitboard;
}
moves.AddRange(generatePseudoLegalPawnPromotions(potentialPromotingPieces, currentPiecesBitboard, opposingPiecesBitboard, currentSidePawn, opposingKingBitboard));
pawnBitboard &= ~potentialPromotingPieces;
ulong occupancyBitboard = whitePiecesBitboard | blackPiecesBitboard;
while (pawnBitboard != 0)
{
byte currentPieceIndex = pawnBitboard.popLSB();
ulong currentAttacks = Attacks.PawnAttacks[currentSidePawn][currentPieceIndex];
if (enPassantSquare != null)
{
moves.AddRange(generatePseudoLegalPawnEnPassantCapture(currentPieceIndex, currentAttacks, currentSidePawn, enPassantSquare, opposingKingBitboard));
}
ulong attackedSquaresBitboard = currentAttacks & opposingPiecesBitboard & ~opposingKingBitboard;
while (attackedSquaresBitboard != 0)
{
byte attackedSquareIndex = attackedSquaresBitboard.popLSB();
moves.Add(new Move(currentPieceIndex, attackedSquareIndex, currentSidePawn, MoveFlags.Capture));
}
ulong regularMove = Attacks.PawnRegularMoves[currentSidePawn][currentPieceIndex] & ~occupancyBitboard;
while (regularMove != 0)
{
byte movedToSquareIndex = regularMove.popLSB();
moves.Add(new Move(currentPieceIndex, movedToSquareIndex, currentSidePawn, MoveFlags.None));
}
// Still have to filter out doublepush
ulong doublePushMoves = Attacks.PawnDoublePush[currentSidePawn][currentPieceIndex] & ~occupancyBitboard;
while (doublePushMoves != 0)
{
byte doublePushSquareIndex = doublePushMoves.popLSB();
ulong doublePushSquareBitboard = BitboardHelper.getBitboardWithBitAt(doublePushSquareIndex);
ulong shiftedSquareBitboard = 0UL;
if (sideToMove.Equals(Side.White))
{
shiftedSquareBitboard = BitboardHelper.South(doublePushSquareBitboard);
}
else if (sideToMove.Equals(Side.Black))
{
shiftedSquareBitboard = BitboardHelper.North(doublePushSquareBitboard);
}
if ((shiftedSquareBitboard & occupancyBitboard) == 0)
{
moves.Add(new Move(currentPieceIndex, doublePushSquareIndex, currentSidePawn, MoveFlags.DoublePush));
}
}
}
return moves;
}
private static List<Move> generatePseudoLegalPawnEnPassantCapture(byte pieceIndex, ulong currentAttacksBitboard, Piece currentSidePawn, EnPassantSquare? enPassantSquare, ulong opposingKingBitboard)
{
List<Move> moves = [];
byte enPassantIndex = (byte)enPassantSquare.Value;
ulong enPassantSquareBitboard = BitboardHelper.getBitboardWithBitAt(enPassantIndex);
ulong reachedEnPassantSquareBitboard = enPassantSquareBitboard & currentAttacksBitboard;
if (reachedEnPassantSquareBitboard != 0)
{
moves.Add(new Move(pieceIndex, enPassantIndex, currentSidePawn, MoveFlags.Capture | MoveFlags.EnPassant));
}
return moves;
}
private static List<Move> generatePseudoLegalPawnPromotions(ulong possiblePromotingPawnsBitboard, ulong currentPiecesBitboard, ulong opposingPiecesBitboard, Piece currentSidePawn, ulong opposingKingBitboard)
{
List<Move> moves = [];
while (possiblePromotingPawnsBitboard != 0)
{
byte pieceIndex = possiblePromotingPawnsBitboard.popLSB();
ulong regularMovesPromotions = Attacks.PawnRegularMoves[currentSidePawn][pieceIndex] & ~(opposingPiecesBitboard | currentPiecesBitboard);
while (regularMovesPromotions != 0)
{
byte targetPromotionIndex = regularMovesPromotions.popLSB();
moves.AddRange(addPromotionMoves(pieceIndex, targetPromotionIndex, currentSidePawn));
}
ulong captureMovesPromotions = Attacks.PawnAttacks[currentSidePawn][pieceIndex] & opposingPiecesBitboard & ~opposingKingBitboard;
while (captureMovesPromotions != 0)
{
byte targetPromotionIndex = captureMovesPromotions.popLSB();
moves.AddRange(addPromotionMoves(pieceIndex, targetPromotionIndex, currentSidePawn, MoveFlags.Capture));
}
}
return moves;
}
private static List<Move> addPromotionMoves(byte fromSquare, byte toSquare, Piece pawn, MoveFlags extraFlags = MoveFlags.None)
{
List<Move> moves = [];
moves.Add(new Move(fromSquare, toSquare, pawn, extraFlags | MoveFlags.QueenPromotion));
moves.Add(new Move(fromSquare, toSquare, pawn, extraFlags | MoveFlags.RookPromotion));
moves.Add(new Move(fromSquare, toSquare, pawn, extraFlags | MoveFlags.KnightPromotion));
moves.Add(new Move(fromSquare, toSquare, pawn, extraFlags | MoveFlags.BishopPromotion));
return moves;
}
public static List<Move> generatePseudoLegalKingMoves(ulong kingBitboard, ulong whitePiecesBitboard, ulong blackPiecesBitboard, Side sideToMove, bool canCastleKingSide, bool canCastleQueenSide, ulong opposingKingBitboard, Board board)
{
if (kingBitboard == 0UL)
throw new InvalidOperationException($"No king for sideToMove={sideToMove}.");
List<Move> moves = [];
ulong opposingPiecesBitboard = blackPiecesBitboard;
ulong currentPiecesBitboard = whitePiecesBitboard;
Piece currentSideKing = Piece.WhiteKing;
if (sideToMove.Equals(Side.Black))
{
opposingPiecesBitboard = whitePiecesBitboard;
currentPiecesBitboard = blackPiecesBitboard;
currentSideKing = Piece.BlackKing;
}
byte pieceIndex = kingBitboard.getLSBIndex();
ulong kingAttacks = Attacks.KingAttacks[pieceIndex];
kingAttacks &= ~currentPiecesBitboard;
ulong kingCaptures = kingAttacks & opposingPiecesBitboard & ~opposingKingBitboard;
ulong kingCapturesMask = kingCaptures; // save before popLSB destroys it
// iterate kingCaptures (mutates it to 0)
kingAttacks ^= kingCapturesMask; // now it actually removes capture squares
// Captures
while (kingCaptures != 0)
{
byte targetIndex = kingCaptures.popLSB();
moves.Add(new Move(pieceIndex, targetIndex, currentSideKing, MoveFlags.Capture));
}
// Regular Moves
while (kingAttacks != 0)
{
byte targetIndex = kingAttacks.popLSB();
moves.Add(new Move(pieceIndex, targetIndex, currentSideKing, MoveFlags.None));
}
ulong occupancyBitboard = currentPiecesBitboard | opposingPiecesBitboard;
moves.AddRange(generatePseudoLegalKingCastle(kingBitboard, canCastleKingSide, canCastleQueenSide, sideToMove, pieceIndex, occupancyBitboard, board));
return moves;
}
private static List<Move> generatePseudoLegalKingCastle(ulong kingBitboard, bool canCastleKingSide, bool canCastleQueenSide, Side sideToMove, byte kingPosition, ulong occupancyBitboard, Board board)
{
List<Move> moves = [];
Piece king = Piece.WhiteKing;
byte[] queenSideRaySquares = [1, 2, 3];
byte[] kingSideRaySquares = [5, 6];
byte[] queenSideSafeSquares = [2, 3];
byte[] kingSideSafeSquares = [5, 6];
if (sideToMove.Equals(Side.Black))
{
king = Piece.BlackKing;
queenSideRaySquares = [57, 58, 59];
kingSideRaySquares = [61, 62];
queenSideSafeSquares = [58, 59];
kingSideSafeSquares = [61, 62];
}
occupancyBitboard.clearBitAtPosition(kingPosition);
ulong queenSideOccupancy = BitboardHelper.getBitboardWithBitsAt(queenSideRaySquares) | kingBitboard;
ulong kingSideOccupancy = BitboardHelper.getBitboardWithBitsAt(kingSideRaySquares) | kingBitboard;
bool isQueenSideIncheck = board.isInCheck(sideToMove);
foreach (byte square in queenSideSafeSquares)
{
isQueenSideIncheck = isQueenSideIncheck || board.isAttacked(sideToMove, square);
}
bool isKingSideInCheck = board.isInCheck(sideToMove);
foreach (byte square in kingSideSafeSquares)
{
isKingSideInCheck = isKingSideInCheck || board.isAttacked(sideToMove, square);
}
if (canCastleQueenSide && (occupancyBitboard & queenSideOccupancy) == 0 && !isQueenSideIncheck)
{
moves.Add(new Move(kingPosition, (byte)(kingPosition - 2), king, MoveFlags.CastleQueenSide));
}
if (canCastleKingSide && (occupancyBitboard & kingSideOccupancy) == 0 && !isKingSideInCheck)
{
moves.Add(new Move(kingPosition, (byte)(kingPosition + 2), king, MoveFlags.CastleKingSide));
}
return moves;
}
public static List<Move> generatePseudoLegalRookMoves(ulong rooksBitboard, ulong whitePiecesBitboard, ulong blackPiecesBitboard, Side sideToMove, ulong opposingKingBitboard)
{
List<Move> moves = [];
ulong opposingPiecesBitboard = blackPiecesBitboard;
ulong currentPiecesBitboard = whitePiecesBitboard;
Piece currentSideRook = Piece.WhiteRook;
if (sideToMove.Equals(Side.Black))
{
opposingPiecesBitboard = whitePiecesBitboard;
currentPiecesBitboard = blackPiecesBitboard;
currentSideRook = Piece.BlackRook;
}
ulong occupancy = currentPiecesBitboard | opposingPiecesBitboard;
while (rooksBitboard != 0)
{
byte pieceIndex = rooksBitboard.popLSB();
ulong attacks = Attacks.getRookAttacks(occupancy, pieceIndex);
moves.AddRange(addPseudoLegalMovesFromAttackTable(attacks, currentPiecesBitboard, opposingPiecesBitboard, pieceIndex, currentSideRook, opposingKingBitboard));
}
return moves;
}
public static List<Move> generatePseudoLegalBishopMoves(ulong bishopsBitboard, ulong whitePiecesBitboard, ulong blackPiecesBitboard, Side sideToMove, ulong opposingKingBitboard)
{
List<Move> moves = [];
ulong opposingPiecesBitboard = blackPiecesBitboard;
ulong currentPiecesBitboard = whitePiecesBitboard;
Piece currentSideBishop = Piece.WhiteBishop;
if (sideToMove.Equals(Side.Black))
{
opposingPiecesBitboard = whitePiecesBitboard;
currentPiecesBitboard = blackPiecesBitboard;
currentSideBishop = Piece.BlackBishop;
}
ulong occupancy = currentPiecesBitboard | opposingPiecesBitboard;
while (bishopsBitboard != 0)
{
byte pieceIndex = bishopsBitboard.popLSB();
ulong attacks = Attacks.getBishopAttacks(occupancy, pieceIndex);
moves.AddRange(addPseudoLegalMovesFromAttackTable(attacks, currentPiecesBitboard, opposingPiecesBitboard, pieceIndex, currentSideBishop, opposingKingBitboard));
}
return moves;
}
public static List<Move> generatePseudoLegalQueenMoves(ulong queensBitboard, ulong whitePiecesBitboard, ulong blackPiecesBitboard, Side sideToMove, ulong opposingKingBitboard)
{
List<Move> moves = [];
ulong opposingPiecesBitboard = blackPiecesBitboard;
ulong currentPiecesBitboard = whitePiecesBitboard;
Piece currentSidequeen = Piece.WhiteQueen;
if (sideToMove.Equals(Side.Black))
{
opposingPiecesBitboard = whitePiecesBitboard;
currentPiecesBitboard = blackPiecesBitboard;
currentSidequeen = Piece.BlackQueen;
}
ulong occupancy = currentPiecesBitboard | opposingPiecesBitboard;
while (queensBitboard != 0)
{
byte pieceIndex = queensBitboard.popLSB();
ulong attacks = Attacks.getQueenAttacks(occupancy, pieceIndex);
moves.AddRange(addPseudoLegalMovesFromAttackTable(attacks, currentPiecesBitboard, opposingPiecesBitboard, pieceIndex, currentSidequeen, opposingKingBitboard));
}
return moves;
}
public static List<Move> addPseudoLegalMovesFromAttackTable(ulong attacks, ulong currentPiecesBitboard, ulong opposingPiecesBitboard, byte pieceIndex, Piece piece, ulong opposingKingBitboard)
{
List<Move> moves = [];
attacks = (currentPiecesBitboard & attacks) ^ attacks;
ulong captures = attacks & opposingPiecesBitboard & ~opposingKingBitboard;
attacks ^= captures;
while (captures != 0)
{
byte attackIndex = captures.popLSB();
moves.Add(new Move(pieceIndex, attackIndex, piece, MoveFlags.Capture));
}
while (attacks != 0)
{
byte attackIndex = attacks.popLSB();
moves.Add(new Move(pieceIndex, attackIndex, piece, MoveFlags.None));
}
return moves;
}
}
}