|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <bitbishop/attacks/generate_attacks.hpp> |
| 4 | +#include <bitbishop/bitboard.hpp> |
| 5 | +#include <bitbishop/board.hpp> |
| 6 | +#include <bitbishop/color.hpp> |
| 7 | +#include <bitbishop/lookups/attackers.hpp> |
| 8 | +#include <bitbishop/move.hpp> |
| 9 | +#include <bitbishop/movegen/bishop_moves.hpp> |
| 10 | +#include <bitbishop/movegen/castling_moves.hpp> |
| 11 | +#include <bitbishop/movegen/check_mask.hpp> |
| 12 | +#include <bitbishop/movegen/king_moves.hpp> |
| 13 | +#include <bitbishop/movegen/knight_moves.hpp> |
| 14 | +#include <bitbishop/movegen/pawn_moves.hpp> |
| 15 | +#include <bitbishop/movegen/pins.hpp> |
| 16 | +#include <bitbishop/movegen/queen_moves.hpp> |
| 17 | +#include <bitbishop/movegen/rook_moves.hpp> |
| 18 | +#include <vector> |
| 19 | + |
| 20 | +/** |
| 21 | + * @brief Generates all legal moves for the given side in the current position. |
| 22 | + * |
| 23 | + * This function computes the full set of legal moves for @p us, taking into |
| 24 | + * account checks, pins, enemy attacks, and special rules such as castling |
| 25 | + * and en passant. Move legality is enforced during generation rather than |
| 26 | + * by post-filtering. |
| 27 | + * |
| 28 | + * The generation pipeline is: |
| 29 | + * - Detect checkers and compute the check resolution mask |
| 30 | + * - Compute pinned pieces and their allowed movement rays |
| 31 | + * - Generate legal king moves (always allowed) |
| 32 | + * - Generate legal castling moves (only when not in check) |
| 33 | + * - If in double check, stop after king moves |
| 34 | + * - Otherwise, generate legal moves for all remaining pieces |
| 35 | + * |
| 36 | + * @param moves Vector to append generated legal moves to |
| 37 | + * @param board Current board position |
| 38 | + * @param us Color of the side to generate moves for |
| 39 | + * |
| 40 | + * @note The move list is appended to; it is not cleared by this function. |
| 41 | + * @note Assumes the board position is internally consistent and legal. |
| 42 | + */ |
| 43 | +void generate_legal_moves(std::vector<Move> moves, const Board& board, Color us) { |
| 44 | + Square king_sq = board.king_square(us).value(); |
| 45 | + Color them = ColorUtil::opposite(us); |
| 46 | + |
| 47 | + Bitboard checkers = attackers_to(king_sq, them); |
| 48 | + Bitboard check_mask = compute_check_mask(king_sq, checkers, board); |
| 49 | + PinResult pins = compute_pins(king_sq, board, us); |
| 50 | + Bitboard enemy_attacks = generate_attacks(board, them); |
| 51 | + |
| 52 | + generate_legal_king_moves(moves, board, us, king_sq, enemy_attacks, check_mask); |
| 53 | + |
| 54 | + generate_castling_moves(moves, board, us, checkers, enemy_attacks); |
| 55 | + |
| 56 | + if (checkers.count() > 1) { |
| 57 | + return; |
| 58 | + } |
| 59 | + |
| 60 | + generate_knight_legal_moves(moves, board, us, check_mask, pins); |
| 61 | + generate_bishop_legal_moves(moves, board, us, check_mask, pins); |
| 62 | + generate_rook_legal_moves(moves, board, us, check_mask, pins); |
| 63 | + generate_queen_legal_moves(moves, board, us, check_mask, pins); |
| 64 | + generate_pawn_legal_moves(moves, board, us, king_sq, check_mask, pins); |
| 65 | +} |
0 commit comments