BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
Square.hpp
Go to the documentation of this file.
1/*
2 * ======================================================================================
3 *
4 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░▒▓████████▓▒░
5 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
6 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
7 * ░▒▓███████▓▒░░▒▓██████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
8 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
9 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
10 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░ ░▒▓█▓▒░
11 *
12 * ======================================================================================
13 */
14
21
27
28#pragma once
29
30#include <cassert>
31#include <compare>
32#include <cstddef> // IWYU pragma: keep - for size_t
33#include <expected>
34#include <format>
38#include <libutil/Math.hpp>
39#include <string>
40#include <string_view>
41#include <utility>
42
48namespace chess::board {
49
72struct Square final {
75
78
82 [[nodiscard, gnu::const]] static constexpr auto from_index(BitboardIndex index) noexcept -> Square;
83
90 [[nodiscard]] static auto from_string(std::string_view text) -> std::expected<Square, std::string>;
91
95 [[nodiscard]] constexpr auto index() const noexcept -> BitboardIndex;
96
98 [[nodiscard]] constexpr auto operator==(const Square&) const noexcept -> bool = default;
99
102
104 [[nodiscard]] constexpr auto is_queenside() const noexcept -> bool;
105
107 [[nodiscard]] constexpr auto is_kingside() const noexcept -> bool;
108
110 [[nodiscard]] constexpr auto is_white_territory() const noexcept -> bool;
111
113 [[nodiscard]] constexpr auto is_black_territory() const noexcept -> bool;
114
116
118 [[nodiscard]] constexpr auto is_light() const noexcept -> bool;
119
121 [[nodiscard]] constexpr auto is_dark() const noexcept -> bool { return not is_light(); }
122};
123
126
130[[nodiscard, gnu::const]] constexpr auto operator<=>(
131 const Square first, const Square second) noexcept
132 -> std::strong_ordering
133{
134 return first.index() <=> second.index();
135}
136
140[[nodiscard, gnu::const]] constexpr auto get_en_passant_captured_square(
141 Square targetSquare, bool isWhite) noexcept
142 -> Square;
143
145
146} // namespace chess::board
147
155template <>
156struct std::formatter<chess::board::Square> final {
157 template <typename ParseContext>
158 // cppcheck-suppress functionStatic
159 constexpr auto parse(ParseContext& ctx) -> typename ParseContext::iterator
160 {
161 return ctx.begin();
162 }
163
164 template <typename FormatContext>
165 // cppcheck-suppress functionStatic
166 auto format(
167 const chess::board::Square square, FormatContext& ctx) const
168 -> typename FormatContext::iterator
169 {
170 return std::format_to(ctx.out(), "{}{}", square.file, square.rank);
171 }
172};
173
174/*
175 ___ ,--,
176 ,---, ,--.'|_ ,--, ,--.'|
177 ,---.'| | | :,' ,--.'| | | :
178 | | : : : ' : | |, : : ' .--.--.
179 | | | ,---. .;__,' / ,--.--. `--'_ | ' | / / '
180 ,--.__| | / \ | | | / \ ,' ,'| ' | | | : /`./
181 / ,' | / / |:__,'| : .--. .-. | ' | | | | : | : ;_
182. ' / |. ' / | ' : |__ \__\/: . . | | : ' : |__ \ \ `.
183' ; |: |' ; /| | | '.'| ," .--.; | ' : |__ | | '.'| `----. \
184| | '/ '' | / | ; : ;/ / ,. | | | '.'|; : ;/ /`--' /__ ___ ___
185| : :|| : | | , /; : .' \; : ;| , /'--'. / .\/ .\/ .\
186 \ \ / \ \ / ---`-' | , .-./| , / ---`-' `--'---'\ ; \ ; \ ; |
187 `----' `----' `--`---' ---`-' `--" `--" `--"
188
189 */
190
191namespace chess::board {
192
193constexpr auto Square::from_index(const BitboardIndex index) noexcept -> Square
194{
195 assert(std::cmp_less_equal(index, MAX_BITBOARD_IDX));
196
197 return {
198 .file = static_cast<File>(index & static_cast<BitboardIndex>(7)),
199 .rank = static_cast<Rank>(index >> static_cast<BitboardIndex>(3))
200 };
201}
202
203constexpr auto Square::index() const noexcept -> BitboardIndex
204{
205 return static_cast<BitboardIndex>(
206 (static_cast<int>(rank) << 3) + static_cast<int>(file));
207}
208
209constexpr auto Square::is_queenside() const noexcept -> bool
210{
211 return std::cmp_less_equal(
212 std::to_underlying(file), std::to_underlying(File::D));
213}
214
215constexpr auto Square::is_kingside() const noexcept -> bool
216{
217 return std::cmp_greater_equal(
218 std::to_underlying(file), std::to_underlying(File::E));
219}
220
221constexpr auto Square::is_white_territory() const noexcept -> bool
222{
223 return std::cmp_less_equal(
224 std::to_underlying(rank), std::to_underlying(Rank::Four));
225}
226
227constexpr auto Square::is_black_territory() const noexcept -> bool
228{
229 return std::cmp_greater_equal(
230 std::to_underlying(rank), std::to_underlying(Rank::Five));
231}
232
233constexpr auto Square::is_light() const noexcept -> bool
234{
235 return not util::math::is_even(
236 std::to_underlying(rank) + std::to_underlying(file));
237}
238
239inline auto Square::from_string(const std::string_view text)
240 -> std::expected<Square, std::string>
241{
242 if (std::cmp_not_equal(text.length(), 2)) {
243 return std::unexpected {
244 std::format(
245 "Cannot parse Square from invalid input string: {}",
246 text)
247 };
248 }
249
250 return rank_from_char(text.back())
251 .and_then([fileChar = text.front()](const Rank rankToUse) {
252 return file_from_char(fileChar)
253 .transform([rankToUse](const File fileToUse) {
254 return Square {
255 .file = fileToUse,
256 .rank = rankToUse
257 };
258 });
259 });
260}
261
263 const Square targetSquare, const bool isWhite) noexcept
264 -> Square
265{
266 // the captured pawn is on the file of the target square, but
267 // one rank below (White capture) or one rank above (Black capture)
268 const auto capturedRank = isWhite
269 ? prev_pawn_rank<Color::White>(targetSquare.rank)
270 : prev_pawn_rank<Color::Black>(targetSquare.rank);
271
272 return Square {
273 .file = targetSquare.file,
274 .rank = capturedRank
275 };
276}
277
278} // namespace chess::board
constexpr auto prev_pawn_rank(Rank rank) noexcept -> Rank
Definition Rank.hpp:155
constexpr auto operator<=>(const Square first, const Square second) noexcept -> std::strong_ordering
Definition Square.hpp:130
std::uint_fast8_t BitboardIndex
auto rank_from_char(char character) -> std::expected< Rank, std::string >
Definition Rank.hpp:166
constexpr BitboardIndex MAX_BITBOARD_IDX
@ One
The first rank. This is the rank that white's king starts on.
Definition Rank.hpp:41
@ Four
The fourth rank.
Definition Rank.hpp:44
@ Five
The fifth rank.
Definition Rank.hpp:45
@ E
The E file. This is the file that the kings start on.
Definition File.hpp:41
@ A
The A file.
Definition File.hpp:37
@ D
The D file. This is the file that the queens start on.
Definition File.hpp:40
constexpr auto is_even(const T value) noexcept -> bool
Definition Math.hpp:41
constexpr auto get_en_passant_captured_square(Square targetSquare, bool isWhite) noexcept -> Square
Definition Square.hpp:262
constexpr auto is_dark() const noexcept -> bool
Definition Square.hpp:121
constexpr auto is_kingside() const noexcept -> bool
Definition Square.hpp:215
static auto from_string(std::string_view text) -> std::expected< Square, std::string >
Definition Square.hpp:239
constexpr auto is_light() const noexcept -> bool
Definition Square.hpp:233
constexpr auto index() const noexcept -> BitboardIndex
Definition Square.hpp:203
constexpr auto is_black_territory() const noexcept -> bool
Definition Square.hpp:227
static constexpr auto from_index(BitboardIndex index) noexcept -> Square
Definition Square.hpp:193
constexpr auto is_queenside() const noexcept -> bool
Definition Square.hpp:209
constexpr auto is_white_territory() const noexcept -> bool
Definition Square.hpp:221
auto format(const chess::board::Square square, FormatContext &ctx) const -> typename FormatContext::iterator
Definition Square.hpp:166
constexpr auto parse(ParseContext &ctx) -> typename ParseContext::iterator
Definition Square.hpp:159