|
| 1 | +#include <bitbishop/bitboard.hpp> |
| 2 | +#include <bitbishop/board.hpp> |
| 3 | +#include <bitbishop/lookups/between_squares.hpp> |
| 4 | +#include <bitbishop/lookups/bishop_rays.hpp> |
| 5 | +#include <bitbishop/lookups/rook_rays.hpp> |
| 6 | +#include <bitbishop/square.hpp> |
| 7 | + |
| 8 | +/** |
| 9 | + * @brief Enumeration of sliding ray types used for pin detection. |
| 10 | + * |
| 11 | + * Distinguishes between rook-like and bishop-like rays, |
| 12 | + * determining which enemy sliders can produce a pin. |
| 13 | + */ |
| 14 | +enum class RayType : std::uint8_t { |
| 15 | + ROOK, ///< Orthogonal rays (rook / queen) |
| 16 | + BISHOP ///< Diagonal rays (bishop / queen) |
| 17 | +}; |
| 18 | + |
| 19 | +/** |
| 20 | + * @brief Enumeration of ray directions from the king square. |
| 21 | + * |
| 22 | + * Used to determine ray orientation and correct blocker ordering |
| 23 | + * (LSB vs MSB) when scanning occupied squares. |
| 24 | + */ |
| 25 | +enum class RayDir : std::uint8_t { |
| 26 | + N, |
| 27 | + S, |
| 28 | + E, |
| 29 | + W, ///< Orthogonal directions |
| 30 | + NE, |
| 31 | + NW, |
| 32 | + SE, |
| 33 | + SW ///< Diagonal directions |
| 34 | +}; |
| 35 | + |
| 36 | +/** |
| 37 | + * @brief Describes a single ray used for pin detection. |
| 38 | + * |
| 39 | + * A PinRay represents one directional ray starting from the king square, |
| 40 | + * along with the information required to: |
| 41 | + * - determine blocker ordering |
| 42 | + * - validate enemy sliding pieces that can create pins |
| 43 | + */ |
| 44 | +struct PinRay { |
| 45 | + Bitboard ray; ///< Precomputed ray bitboard from the king square |
| 46 | + RayType type; ///< Type of sliding piece relevant for this ray |
| 47 | + RayDir dir; ///< Direction of the ray |
| 48 | + |
| 49 | + /** |
| 50 | + * @brief Returns the first blocker along this ray. |
| 51 | + * |
| 52 | + * The function selects either the least-significant or most-significant |
| 53 | + * bit depending on the ray direction. |
| 54 | + * |
| 55 | + * @param bb Bitboard of occupied squares along the ray |
| 56 | + * @return Square of the closest blocker to the king |
| 57 | + */ |
| 58 | + [[nodiscard]] Square first_blocker(const Bitboard& bb) const { |
| 59 | + switch (dir) { |
| 60 | + case RayDir::N: |
| 61 | + case RayDir::NE: |
| 62 | + case RayDir::E: |
| 63 | + case RayDir::NW: |
| 64 | + return bb.lsb().value(); |
| 65 | + default: |
| 66 | + return bb.msb().value(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + /** |
| 71 | + * @brief Checks whether a piece matches the required enemy slider for this ray. |
| 72 | + * |
| 73 | + * For rook rays, accepts enemy rooks or queens. |
| 74 | + * For bishop rays, accepts enemy bishops or queens. |
| 75 | + * |
| 76 | + * @param piece Bitboard with a single enemy piece |
| 77 | + * @param board Current board state |
| 78 | + * @param them Enemy color |
| 79 | + * @return true if the piece can create a pin along this ray |
| 80 | + */ |
| 81 | + [[nodiscard]] bool matches_slider(const Bitboard& piece, const Board& board, Color them) const { |
| 82 | + return (type == RayType::BISHOP) ? (piece & (board.bishops(them) | board.queens(them))) |
| 83 | + : (piece & (board.rooks(them) | board.queens(them))); |
| 84 | + } |
| 85 | +}; |
| 86 | + |
| 87 | +/** |
| 88 | + * @brief Result structure for pin computation. |
| 89 | + * |
| 90 | + * Contains: |
| 91 | + * - a bitboard of all pinned friendly pieces |
| 92 | + * - a per-square pin ray mask restricting legal movement |
| 93 | + */ |
| 94 | +struct PinResult { |
| 95 | + Bitboard pinned = Bitboard::Zeros(); |
| 96 | + std::array<Bitboard, Const::BOARD_SIZE> pin_ray{}; |
| 97 | +}; |
| 98 | + |
| 99 | +/** |
| 100 | + * @brief Scans a single ray from the king to detect a possible pin. |
| 101 | + * |
| 102 | + * The function detects the classical pin pattern: |
| 103 | + * king → friendly piece → enemy sliding piece |
| 104 | + * |
| 105 | + * If such a configuration is found: |
| 106 | + * - the friendly piece is marked as pinned |
| 107 | + * - its legal movement is restricted to the pin ray |
| 108 | + * |
| 109 | + * @param king_sq Square of the friendly king |
| 110 | + * @param ray_info Ray descriptor (direction, type, bitboard) |
| 111 | + * @param board Current board position |
| 112 | + * @param us Friendly color |
| 113 | + * @param result Accumulated pin result (modified in place) |
| 114 | + */ |
| 115 | +void scan_pin_ray(Square king_sq, const PinRay& ray_info, const Board& board, Color us, PinResult& result); |
| 116 | + |
| 117 | +/** |
| 118 | + * @brief Computes all pinned pieces for the given side. |
| 119 | + * |
| 120 | + * Pins are detected by scanning all rook and bishop rays |
| 121 | + * originating from the king square. |
| 122 | + * |
| 123 | + * A piece is considered pinned if moving it would expose |
| 124 | + * the king to a sliding attack. |
| 125 | + * |
| 126 | + * @param king_sq Square of the friendly king |
| 127 | + * @param board Current board position |
| 128 | + * @param us Friendly color |
| 129 | + * @return PinResult containing pinned pieces and their pin rays |
| 130 | + */ |
| 131 | +PinResult compute_pins(Square king_sq, const Board& board, Color us); |
0 commit comments