template 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 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 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 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 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 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 void GenerateMoves(Board &board, bool clear, bool recalcThreats) { if (clear) { board.ResetMoves(); } if (recalcThreats) { GenThreatMaps(board); } GenPawnMoves(board); GenKnightMoves(board); GenKingMoves(board); GenBishopMoves(board); GenRookMoves(board); GenQueenMoves(board); };