- castling moves are stored as "king takes own rook" (from = king square, to = castling rook square) - per-colour castling rook squares (Board::castleRook), so DFRC (different back ranks for white/black) works - FEN parsing/printing: KQkq (X-FEN) and Shredder-FEN file letters - Board::CanCastle(): generic castling legality (empty paths, king not attacked, rook x-ray check) - MakeMove handles arbitrary king/rook start squares (king and rook may stay in place or swap through each other's squares) - UCI_Chess960 option: castling printed/parsed as king-takes-rook when on, standard e1g1 style otherwise (both are accepted when parsing) - viri format, perft castle check and castling right updates no longer assume a1/h1/a8/h8
272 lines
7.2 KiB
C++
272 lines
7.2 KiB
C++
template <MovegenMode mode>
|
|
void GenPawnMoves(Board &board) {
|
|
Bitboard pawns = board.pieces[Pawn] & board.colors[board.sideToMove];
|
|
|
|
while (pawns) {
|
|
int square = pawns.getLS1BIndex();
|
|
|
|
Bitboard pushes = getPawnPushes(square, board.sideToMove, board.occupied);
|
|
Bitboard attacks = pawnAttacks[board.sideToMove][square];
|
|
Bitboard captures = attacks & board.colors[!board.sideToMove];
|
|
|
|
Bitboard lastRank = board.sideToMove ? ranks[r_1] : ranks[r_8];
|
|
|
|
if constexpr (mode == Noisy || mode == All) {
|
|
Bitboard noisyPushes = pushes & lastRank;
|
|
|
|
if (board.enPassantTarget != noEPTarget) {
|
|
if (attacks.IsSet(board.enPassantTarget)) {
|
|
board.AddMove(Move(square, board.enPassantTarget, epCapture));
|
|
}
|
|
}
|
|
|
|
while (captures) {
|
|
int targetSquare = captures.getLS1BIndex();
|
|
|
|
if (lastRank.IsSet(targetSquare)) {
|
|
board.AddMove(Move(square, targetSquare, knightPromoCapture));
|
|
board.AddMove(Move(square, targetSquare, bishopPromoCapture));
|
|
board.AddMove(Move(square, targetSquare, rookPromoCapture));
|
|
board.AddMove(Move(square, targetSquare, queenPromoCapture));
|
|
} else {
|
|
board.AddMove(Move(square, targetSquare, capture));
|
|
}
|
|
|
|
captures.PopBit(targetSquare);
|
|
}
|
|
|
|
while (noisyPushes) {
|
|
int pushSquare = noisyPushes.getLS1BIndex();
|
|
|
|
// Only promotions because NOISY
|
|
board.AddMove(Move(square, pushSquare, knightPromotion));
|
|
board.AddMove(Move(square, pushSquare, bishopPromotion));
|
|
board.AddMove(Move(square, pushSquare, rookPromotion));
|
|
board.AddMove(Move(square, pushSquare, queenPromotion));
|
|
|
|
noisyPushes.PopBit(pushSquare);
|
|
}
|
|
|
|
pawns.PopBit(square);
|
|
}
|
|
|
|
if constexpr (mode == Quiet || mode == All) {
|
|
while (pushes) {
|
|
int pushSquare = pushes.getLS1BIndex();
|
|
|
|
if (!lastRank.IsSet(pushSquare)) {
|
|
if (std::abs(square - pushSquare) == 16) {
|
|
board.AddMove(Move(square, pushSquare, doublePawnPush));
|
|
} else {
|
|
board.AddMove(Move(square, pushSquare, quiet));
|
|
}
|
|
}
|
|
|
|
pushes.PopBit(pushSquare);
|
|
}
|
|
}
|
|
|
|
pawns.PopBit(square);
|
|
}
|
|
}
|
|
template <MovegenMode mode>
|
|
void GenKnightMoves(Board &board) {
|
|
Bitboard knights = board.colors[board.sideToMove] & board.pieces[Knight];
|
|
|
|
while (knights) {
|
|
int square = knights.getLS1BIndex();
|
|
|
|
Bitboard moves = knightAttacks[square] & ~board.colors[board.sideToMove];
|
|
Bitboard captures = moves & board.colors[!board.sideToMove];
|
|
moves &= ~captures;
|
|
|
|
if constexpr (mode == Noisy || mode == All) {
|
|
while (captures) {
|
|
int targetSquare = captures.getLS1BIndex();
|
|
|
|
board.AddMove(Move(square, targetSquare, capture));
|
|
|
|
captures.PopBit(targetSquare);
|
|
}
|
|
|
|
knights.PopBit(square);
|
|
}
|
|
|
|
if constexpr (mode == Quiet || mode == All) {
|
|
while (moves) {
|
|
int targetSquare = moves.getLS1BIndex();
|
|
|
|
board.AddMove(Move(square, targetSquare, quiet));
|
|
|
|
moves.PopBit(targetSquare);
|
|
}
|
|
}
|
|
|
|
knights.PopBit(square);
|
|
}
|
|
}
|
|
template <MovegenMode mode>
|
|
void GenRookMoves(Board &board) {
|
|
Bitboard rooks = board.colors[board.sideToMove] & board.pieces[Rook];
|
|
|
|
while (rooks) {
|
|
int square = rooks.getLS1BIndex();
|
|
|
|
Bitboard moves = getRookAttack(square, board.occupied) & ~board.colors[board.sideToMove];
|
|
Bitboard captures = moves & board.colors[!board.sideToMove];
|
|
moves &= ~captures;
|
|
|
|
if constexpr (mode == Noisy || mode == All) {
|
|
while (captures) {
|
|
int targetSquare = captures.getLS1BIndex();
|
|
|
|
board.AddMove(Move(square, targetSquare, capture));
|
|
|
|
captures.PopBit(targetSquare);
|
|
}
|
|
|
|
rooks.PopBit(square);
|
|
}
|
|
|
|
if constexpr (mode == Quiet || mode == All) {
|
|
while (moves) {
|
|
int targetSquare = moves.getLS1BIndex();
|
|
|
|
board.AddMove(Move(square, targetSquare, quiet));
|
|
|
|
moves.PopBit(targetSquare);
|
|
}
|
|
}
|
|
|
|
rooks.PopBit(square);
|
|
}
|
|
}
|
|
template <MovegenMode mode>
|
|
void GenBishopMoves(Board &board) {
|
|
Bitboard bishops = board.colors[board.sideToMove] & board.pieces[Bishop];
|
|
|
|
while (bishops) {
|
|
int square = bishops.getLS1BIndex();
|
|
|
|
Bitboard moves = getBishopAttack(square, board.occupied) & ~board.colors[board.sideToMove];
|
|
Bitboard captures = moves & board.colors[!board.sideToMove];
|
|
moves &= ~captures;
|
|
|
|
if constexpr (mode == Noisy || mode == All) {
|
|
while (captures) {
|
|
int targetSquare = captures.getLS1BIndex();
|
|
|
|
board.AddMove(Move(square, targetSquare, capture));
|
|
|
|
captures.PopBit(targetSquare);
|
|
}
|
|
|
|
bishops.PopBit(square);
|
|
}
|
|
|
|
if constexpr (mode == Quiet || mode == All) {
|
|
while (moves) {
|
|
int targetSquare = moves.getLS1BIndex();
|
|
|
|
board.AddMove(Move(square, targetSquare, quiet));
|
|
|
|
moves.PopBit(targetSquare);
|
|
}
|
|
}
|
|
|
|
bishops.PopBit(square);
|
|
}
|
|
}
|
|
template <MovegenMode mode>
|
|
void GenQueenMoves(Board &board) {
|
|
Bitboard queens = board.colors[board.sideToMove] & board.pieces[Queen];
|
|
|
|
while (queens) {
|
|
int square = queens.getLS1BIndex();
|
|
|
|
Bitboard moves = getQueenAttack(square, board.occupied) & ~board.colors[board.sideToMove];
|
|
Bitboard captures = moves & board.colors[!board.sideToMove];
|
|
moves &= ~captures;
|
|
|
|
if constexpr (mode == Noisy || mode == All) {
|
|
while (captures) {
|
|
int targetSquare = captures.getLS1BIndex();
|
|
|
|
board.AddMove(Move(square, targetSquare, capture));
|
|
|
|
captures.PopBit(targetSquare);
|
|
}
|
|
|
|
queens.PopBit(square);
|
|
}
|
|
|
|
if constexpr (mode == Quiet || mode == All) {
|
|
while (moves) {
|
|
int targetSquare = moves.getLS1BIndex();
|
|
|
|
board.AddMove(Move(square, targetSquare, quiet));
|
|
|
|
moves.PopBit(targetSquare);
|
|
}
|
|
}
|
|
|
|
queens.PopBit(square);
|
|
}
|
|
}
|
|
template <MovegenMode mode>
|
|
void GenKingMoves(Board &board) {
|
|
int kingSquare = (board.colors[board.sideToMove] & board.pieces[King]).getLS1BIndex();
|
|
|
|
Bitboard moves = (kingAttacks[kingSquare] & ~board.colors[board.sideToMove]);
|
|
Bitboard captures = moves & board.colors[!board.sideToMove];
|
|
moves &= ~board.colors[!board.sideToMove];
|
|
|
|
if constexpr (mode == Noisy || mode == All) {
|
|
while (captures) {
|
|
int square = captures.getLS1BIndex();
|
|
|
|
Move move(kingSquare, square, capture);
|
|
board.AddMove(move);
|
|
|
|
captures.PopBit(square);
|
|
}
|
|
}
|
|
|
|
if constexpr (mode == Quiet || mode == All) {
|
|
while (moves) {
|
|
int square = moves.getLS1BIndex();
|
|
|
|
board.AddMove(Move(kingSquare, square, quiet));
|
|
|
|
moves.PopBit(square);
|
|
}
|
|
|
|
// Generating castles (Chess960 aware): encoded as the king taking its own castling rook
|
|
for (const bool kingSide : {false, true}) {
|
|
if (board.CanCastle(board.sideToMove, kingSide)) {
|
|
board.AddMove(Move(kingSquare, board.castleRook[Board::CastleIdx(board.sideToMove, kingSide)],
|
|
kingSide ? kingCastle : queenCastle));
|
|
}
|
|
}
|
|
|
|
}
|
|
}
|
|
|
|
template <MovegenMode mode>
|
|
void GenerateMoves(Board &board, bool clear, bool recalcThreats) {
|
|
if (clear) {
|
|
board.ResetMoves();
|
|
}
|
|
|
|
if (recalcThreats) {
|
|
GenThreatMaps(board);
|
|
}
|
|
|
|
GenPawnMoves<mode>(board);
|
|
GenKnightMoves<mode>(board);
|
|
GenKingMoves<mode>(board);
|
|
GenBishopMoves<mode>(board);
|
|
GenRookMoves<mode>(board);
|
|
GenQueenMoves<mode>(board);
|
|
};
|