|
| 1 | +#include <array> |
| 2 | +#include <bitbishop/bitboard.hpp> |
| 3 | +#include <bitbishop/constants.hpp> |
| 4 | + |
| 5 | +namespace Lookups { |
| 6 | + |
| 7 | +/** |
| 8 | + * @brief Computes the directional step between two aligned squares. |
| 9 | + * |
| 10 | + * Returns the signed square-index increment required to move from the |
| 11 | + * source square toward the destination square along a shared rank, file, |
| 12 | + * or diagonal. |
| 13 | + * |
| 14 | + * The returned value corresponds to the underlying square indexing: |
| 15 | + * - +-1 for horizontal movement (same rank) |
| 16 | + * - +-8 for vertical movement (same file) |
| 17 | + * - +-9 for NE–SW diagonal movement |
| 18 | + * - +-7 for NW–SE diagonal movement |
| 19 | + * - 0 if source and destination square are identical |
| 20 | + * - 0 if source and destination squares are not aligned |
| 21 | + * |
| 22 | + * This function assumes that the two squares are aligned. Its primary use |
| 23 | + * is internal traversal of rays when building geometric lookup tables such |
| 24 | + * as BETWEEN. |
| 25 | + * |
| 26 | + * @param from The starting square. |
| 27 | + * @param to The target square. |
| 28 | + * @return The signed step value to advance one square toward @p to. |
| 29 | + */ |
| 30 | +constexpr int direction(Square from, Square to) { |
| 31 | + if (from == to) { |
| 32 | + return 0; |
| 33 | + } |
| 34 | + if (from.same_rank(to)) { |
| 35 | + return (to.file() > from.file()) ? +1 : -1; |
| 36 | + } |
| 37 | + if (from.same_file(to)) { |
| 38 | + return (to.rank() > from.rank()) ? +Const::BOARD_WIDTH : -Const::BOARD_WIDTH; |
| 39 | + } |
| 40 | + if (from.same_ne_sw_diag(to)) { |
| 41 | + return (to.rank() > from.rank()) ? +(Const::BOARD_WIDTH + 1) : -(Const::BOARD_WIDTH + 1); |
| 42 | + } |
| 43 | + if (from.same_nw_se_diag(to)) { |
| 44 | + return (to.rank() > from.rank()) ? +(Const::BOARD_WIDTH - 1) : -(Const::BOARD_WIDTH - 1); |
| 45 | + } |
| 46 | + |
| 47 | + return 0; |
| 48 | +} |
| 49 | + |
| 50 | +/** |
| 51 | + * @brief Computes the bitboard of squares strictly between two aligned squares. |
| 52 | + * |
| 53 | + * Returns a bitboard containing all squares located strictly between the |
| 54 | + * source and destination squares, excluding both endpoints. |
| 55 | + * |
| 56 | + * If the squares are not aligned along the same rank, file, or diagonal, |
| 57 | + * or if both squares are identical, an empty bitboard is returned. |
| 58 | + * |
| 59 | + * This function is purely geometric and does not depend on board occupancy. |
| 60 | + * It is intended for use in compile-time construction of lookup tables and |
| 61 | + * in runtime legality checks such as pins and check interposition. |
| 62 | + * |
| 63 | + * @param from The starting square. |
| 64 | + * @param to The target square. |
| 65 | + * @return A bitboard of squares strictly between @p from and @p to. |
| 66 | + */ |
| 67 | +constexpr Bitboard ray_between(Square from, Square to) { |
| 68 | + if (from == to) { |
| 69 | + return Bitboard::Zeros(); |
| 70 | + } |
| 71 | + |
| 72 | + if (!(from.same_file(to) || from.same_rank(to) || from.same_diag(to))) { |
| 73 | + return Bitboard::Zeros(); |
| 74 | + } |
| 75 | + |
| 76 | + // Defensive check: if step is 0, squares aren't truly aligned |
| 77 | + const int step = direction(from, to); |
| 78 | + if (step == 0) { |
| 79 | + return Bitboard::Zeros(); |
| 80 | + } |
| 81 | + |
| 82 | + Bitboard ray = Bitboard::Zeros(); |
| 83 | + for (std::uint8_t square = from.value() + step; square != to.value(); square += step) { |
| 84 | + ray.set(square); |
| 85 | + } |
| 86 | + |
| 87 | + return ray; |
| 88 | +} |
| 89 | + |
| 90 | +/** |
| 91 | + * @brief Precomputed lookup table of squares lying strictly between any two squares. |
| 92 | + * |
| 93 | + * For each pair of squares (from, to), this table contains a bitboard of all |
| 94 | + * squares strictly between them along a shared rank, file, or diagonal. |
| 95 | + * |
| 96 | + * If the squares are not aligned, the corresponding entry is an empty bitboard. |
| 97 | + * |
| 98 | + * This table is computed entirely at compile time and is used extensively |
| 99 | + * in move generation, including: |
| 100 | + * - pin detection |
| 101 | + * - check resolution |
| 102 | + * - slider attack filtering |
| 103 | + * |
| 104 | + * The table is indexed as BETWEEN[from][to]. |
| 105 | + */ |
| 106 | +constexpr std::array<std::array<Bitboard, Const::BOARD_SIZE>, Const::BOARD_SIZE> BETWEEN = []() constexpr { |
| 107 | + using namespace Const; |
| 108 | + |
| 109 | + std::array<std::array<Bitboard, BOARD_SIZE>, BOARD_SIZE> table{}; |
| 110 | + |
| 111 | + for (int from = 0; from < BOARD_SIZE; ++from) { |
| 112 | + for (int to = 0; to < BOARD_SIZE; ++to) { |
| 113 | + table[from][to] = ray_between(Square(from, std::in_place), Square(to, std::in_place)); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + return table; |
| 118 | +}(); |
| 119 | + |
| 120 | +} // namespace Lookups |
0 commit comments