BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
Distances.hpp
Go to the documentation of this file.
1/*
2 * ======================================================================================
3 *
4 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░▒▓████████▓▒░
5 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
6 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
7 * ░▒▓███████▓▒░░▒▓██████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
8 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
9 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
10 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░ ░▒▓█▓▒░
11 *
12 * ======================================================================================
13 */
14
20
21#pragma once
22
23#include <algorithm>
26#include <utility>
27
28namespace chess::board {
29
32
41[[nodiscard, gnu::const]] constexpr auto file_distance(
42 const Square& first, const Square& second) noexcept
44
53[[nodiscard, gnu::const]] constexpr auto rank_distance(
54 const Square& first, const Square& second) noexcept
56
62[[nodiscard, gnu::const]] constexpr auto are_on_same_diagonal(
63 const Square& first, const Square& second) noexcept
64 -> bool;
65
78[[nodiscard, gnu::const]] constexpr auto manhattan_distance(
79 const Square& first, const Square& second) noexcept
81
88[[nodiscard, gnu::const]] constexpr auto center_manhattan_distance(
89 const Square& square) noexcept
91
103[[nodiscard, gnu::const]] constexpr auto chebyshev_distance(
104 const Square& first, const Square& second) noexcept
105 -> BitboardIndex;
106
114[[nodiscard, gnu::const]] auto knight_distance(
115 const Square& first, const Square& second)
116 -> BitboardIndex;
117
119
120/*
121 ___ ,--,
122 ,---, ,--.'|_ ,--, ,--.'|
123 ,---.'| | | :,' ,--.'| | | :
124 | | : : : ' : | |, : : ' .--.--.
125 | | | ,---. .;__,' / ,--.--. `--'_ | ' | / / '
126 ,--.__| | / \ | | | / \ ,' ,'| ' | | | : /`./
127 / ,' | / / |:__,'| : .--. .-. | ' | | | | : | : ;_
128. ' / |. ' / | ' : |__ \__\/: . . | | : ' : |__ \ \ `.
129' ; |: |' ; /| | | '.'| ," .--.; | ' : |__ | | '.'| `----. \
130| | '/ '' | / | ; : ;/ / ,. | | | '.'|; : ;/ /`--' /__ ___ ___
131| : :|| : | | , /; : .' \; : ;| , /'--'. / .\/ .\/ .\
132 \ \ / \ \ / ---`-' | , .-./| , / ---`-' `--'---'\ ; \ ; \ ; |
133 `----' `----' `--`---' ---`-' `--" `--" `--"
134
135 */
136
137constexpr auto file_distance(
138 const Square& first, const Square& second) noexcept
140{
141 const auto firstFile = std::to_underlying(first.file);
142 const auto secondFile = std::to_underlying(second.file);
143
144 const auto [minFile, maxFile] = std::minmax(firstFile, secondFile);
145
146 return static_cast<BitboardIndex>(maxFile - minFile);
147}
148
149constexpr auto rank_distance(
150 const Square& first, const Square& second) noexcept
152{
153 const auto firstRank = std::to_underlying(first.rank);
154 const auto secondRank = std::to_underlying(second.rank);
155
156 const auto [minRank, maxRank] = std::minmax(firstRank, secondRank);
157
158 return static_cast<BitboardIndex>(maxRank - minRank);
159}
160
161constexpr auto are_on_same_diagonal(
162 const Square& first, const Square& second) noexcept
163 -> bool
164{
165 return std::cmp_equal(file_distance(first, second), rank_distance(first, second));
166}
167
168constexpr auto manhattan_distance(
169 const Square& first, const Square& second) noexcept
171{
172 return static_cast<BitboardIndex>(file_distance(first, second) + rank_distance(first, second));
173}
174
176 const Square& square) noexcept
178{
179 auto file = static_cast<int>(square.file);
180 auto rank = static_cast<int>(square.rank);
181
182 file ^= (file - 4) >> 8;
183 rank ^= (rank - 4) >> 8;
184
185 return static_cast<BitboardIndex>((file + rank) & 7);
186}
187
188constexpr auto chebyshev_distance(
189 const Square& first, const Square& second) noexcept
191{
192 const auto fileDist = file_distance(first, second);
193 const auto rankDist = rank_distance(first, second);
194
195 return std::max(fileDist, rankDist);
196}
197
198} // namespace chess::board
std::uint_fast8_t BitboardIndex
auto knight_distance(const Square &first, const Square &second) -> BitboardIndex
constexpr auto center_manhattan_distance(const Square &square) noexcept -> BitboardIndex
constexpr auto file_distance(const Square &first, const Square &second) noexcept -> BitboardIndex
constexpr auto chebyshev_distance(const Square &first, const Square &second) noexcept -> BitboardIndex
constexpr auto rank_distance(const Square &first, const Square &second) noexcept -> BitboardIndex
constexpr auto manhattan_distance(const Square &first, const Square &second) noexcept -> BitboardIndex
constexpr auto are_on_same_diagonal(const Square &first, const Square &second) noexcept -> bool