42struct File : Coordinate<File> {
45 FA, FB, FC, FD, FE, FF, FG, FH,
57 static constexpr std::string_view fileToChar =
"abcdefgh";
59 constexpr File(uint8_t value = none()) : Coordinate(value) {}
60 constexpr File(
int value = none()) : Coordinate(value) {}
61 explicit constexpr File(
char c) {
62 if (c >=
'a' && c <=
'h') {
63 m_value =
static_cast<uint8_t
>(c -
'a');
64 }
else if (c >=
'A' && c <=
'H') {
65 m_value =
static_cast<uint8_t
>(c -
'A');
69 static constexpr uint8_t distance(File from, File to) {
return from > to ? from - to : to - from; }
71 void print(std::ostream& os)
const {
73 os << fileToChar[m_value];
79struct Rank : Coordinate<Rank> {
83 R1, R2, R3, R4, R5, R6, R7, R8,
95 static constexpr std::string_view rankToChar =
"12345678";
97 constexpr Rank(uint8_t value = none()) : Coordinate(value) {}
98 constexpr Rank(
int value = none()) : Coordinate(value) {}
99 explicit constexpr Rank(
char c) { m_value =
static_cast<uint8_t
>(c -
'1'); }
101 static constexpr uint8_t distance(Rank from, Rank to) {
return from > to ? from - to : to - from; }
103 [[nodiscard]]
constexpr bool pawnStarting(
Color color)
const {
104 return (color == Color::WHITE && m_value == R2) || (color == Color::BLACK && m_value == R7);
106 [[nodiscard]]
constexpr bool pawnPromoting(
Color color)
const {
107 return (color == Color::WHITE && m_value == R8) || (color == Color::BLACK && m_value == R1);
110 void print(std::ostream& os)
const {
112 os << rankToChar[m_value];
118struct Square : Coordinate<Square> {
122 A1, B1, C1, D1, E1, F1, G1, H1,
123 A2, B2, C2, D2, E2, F2, G2, H2,
124 A3, B3, C3, D3, E3, F3, G3, H3,
125 A4, B4, C4, D4, E4, F4, G4, H4,
126 A5, B5, C5, D5, E5, F5, G5, H5,
127 A6, B6, C6, D6, E6, F6, G6, H6,
128 A7, B7, C7, D7, E7, F7, G7, H7,
129 A8, B8, C8, D8, E8, F8, G8, H8,
141 constexpr Square(uint8_t value = none()) : Coordinate(value) {}
142 constexpr Square(
int value = none()) : Coordinate(value) {}
143 explicit constexpr Square(
File file,
Rank rank) : Coordinate((rank << File::size()) | file) {}
144 explicit constexpr Square(std::string_view str) {
145 if (str.size() == 2) {
148 if (file.ok() && rank.ok()) {
154 [[nodiscard]]
constexpr File file()
const {
return m_value & File::mask(); }
155 [[nodiscard]]
constexpr Rank rank()
const {
return m_value >> File::size(); }
157 static constexpr uint8_t distance(Square from, Square to) {
158 static constexpr auto table = []()
constexpr {
159 std::array<std::array<uint8_t, NB>, NB> t{};
160 for (
auto i : Square::all()) {
161 for (
auto j : Square::all()) {
162 t[i][j] = std::max(File::distance(i.file(), j.file()), Rank::distance(i.rank(), j.rank()));
167 return table[from][to];
170 void print(std::ostream& os)
const {
219constexpr std::array<Direction, 8> knightDirections = {Direction::NNE, Direction::NNW, Direction::ENE, Direction::WNW,
222constexpr std::array<Direction, 8> queenDirections = {Direction::NE, Direction::NW, Direction::SE, Direction::SW,
225constexpr std::array<Direction, 8> kingDirections = {Direction::E, Direction::N, Direction::W, Direction::S,