BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
Rank.hpp
Go to the documentation of this file.
1/*
2 * ======================================================================================
3 *
4 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░▒▓████████▓▒░
5 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
6 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
7 * ░▒▓███████▓▒░░▒▓██████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
8 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
9 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
10 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░ ░▒▓█▓▒░
11 *
12 * ======================================================================================
13 */
14
19
20#pragma once
21
22#include <cassert>
23#include <expected>
24#include <format>
27#include <string>
28#include <string_view>
29#include <utility>
30
31namespace chess::board {
32
33using pieces::Color;
34
50
56[[nodiscard, gnu::const]] constexpr auto back_rank_for(Color color) noexcept -> Rank;
57
61template <Color Side>
62[[nodiscard, gnu::const]] constexpr auto next_pawn_rank(Rank rank) noexcept -> Rank;
63
67template <Color Side>
68[[nodiscard, gnu::const]] constexpr auto prev_pawn_rank(Rank rank) noexcept -> Rank;
69
77[[nodiscard]] auto rank_from_char(char character)
78 -> std::expected<Rank, std::string>;
79
85[[nodiscard, gnu::const]] constexpr auto rank_to_char(Rank rank) -> char;
86
87} // namespace chess::board
88
96template <>
97struct std::formatter<chess::board::Rank> final {
98 template <typename ParseContext>
99 // cppcheck-suppress functionStatic
100 constexpr auto parse(ParseContext& ctx) -> typename ParseContext::iterator
101 {
102 return ctx.begin();
103 }
104
105 template <typename FormatContext>
106 // cppcheck-suppress functionStatic
107 auto format(
108 const chess::board::Rank rank, FormatContext& ctx) const
109 -> typename FormatContext::iterator
110 {
111 return std::format_to(ctx.out(), "{}", rank_to_char(rank));
112 }
113};
114
115/*
116 ___ ,--,
117 ,---, ,--.'|_ ,--, ,--.'|
118 ,---.'| | | :,' ,--.'| | | :
119 | | : : : ' : | |, : : ' .--.--.
120 | | | ,---. .;__,' / ,--.--. `--'_ | ' | / / '
121 ,--.__| | / \ | | | / \ ,' ,'| ' | | | : /`./
122 / ,' | / / |:__,'| : .--. .-. | ' | | | | : | : ;_
123. ' / |. ' / | ' : |__ \__\/: . . | | : ' : |__ \ \ `.
124' ; |: |' ; /| | | '.'| ," .--.; | ' : |__ | | '.'| `----. \
125| | '/ '' | / | ; : ;/ / ,. | | | '.'|; : ;/ /`--' /__ ___ ___
126| : :|| : | | , /; : .' \; : ;| , /'--'. / .\/ .\/ .\
127 \ \ / \ \ / ---`-' | , .-./| , / ---`-' `--'---'\ ; \ ; \ ; |
128 `----' `----' `--`---' ---`-' `--" `--" `--"
129
130 */
131
132namespace chess::board {
133
134constexpr auto back_rank_for(const Color color) noexcept -> Rank
135{
136 if (color == Color::White)
137 return Rank::One;
138
139 return Rank::Eight;
140}
141
142template <Color Side>
143constexpr auto next_pawn_rank(const Rank rank) noexcept -> Rank
144{
145 if constexpr (Side == Color::White) {
146 assert(rank != Rank::Eight);
147 return static_cast<Rank>(std::to_underlying(rank) + 1uz);
148 } else {
149 assert(rank != Rank::One);
150 return static_cast<Rank>(std::to_underlying(rank) - 1uz);
151 }
152}
153
154template <Color Side>
155constexpr auto prev_pawn_rank(const Rank rank) noexcept -> Rank
156{
157 if constexpr (Side == Color::White) {
158 assert(rank != Rank::One);
159 return static_cast<Rank>(std::to_underlying(rank) - 1uz);
160 } else {
161 assert(rank != Rank::Eight);
162 return static_cast<Rank>(std::to_underlying(rank) + 1uz);
163 }
164}
165
166inline auto rank_from_char(const char character)
167 -> std::expected<Rank, std::string>
168{
169 switch (character) {
170 case '1': return Rank::One;
171 case '2': return Rank::Two;
172 case '3': return Rank::Three;
173 case '4': return Rank::Four;
174 case '5': return Rank::Five;
175 case '6': return Rank::Six;
176 case '7': return Rank::Seven;
177 case '8': return Rank::Eight;
178
179 default:
180 return std::unexpected {
181 std::format(
182 "Cannot parse Rank from character: {}",
183 character)
184 };
185 }
186}
187
188constexpr auto rank_to_char(const Rank rank) -> char
189{
190 constexpr std::string_view ranks { "12345678" };
191
192 return ranks.at(std::to_underlying(rank));
193}
194
195} // namespace chess::board
constexpr auto prev_pawn_rank(Rank rank) noexcept -> Rank
Definition Rank.hpp:155
std::uint_fast8_t BitboardIndex
auto rank_from_char(char character) -> std::expected< Rank, std::string >
Definition Rank.hpp:166
constexpr auto back_rank_for(Color color) noexcept -> Rank
Definition Rank.hpp:134
constexpr auto rank_to_char(Rank rank) -> char
Definition Rank.hpp:188
constexpr auto next_pawn_rank(Rank rank) noexcept -> Rank
Definition Rank.hpp:143
@ One
The first rank. This is the rank that white's king starts on.
Definition Rank.hpp:41
@ Seven
The seventh rank. This is the rank that black's pawns start on.
Definition Rank.hpp:47
@ Four
The fourth rank.
Definition Rank.hpp:44
@ Two
The second rank. This is the rank that white's pawns start on.
Definition Rank.hpp:42
@ Eight
The back rank. This is the rank that black's king starts on.
Definition Rank.hpp:48
@ Three
The third rank.
Definition Rank.hpp:43
@ Five
The fifth rank.
Definition Rank.hpp:45
@ Six
The sixth rank.
Definition Rank.hpp:46
auto format(const chess::board::Rank rank, FormatContext &ctx) const -> typename FormatContext::iterator
Definition Rank.hpp:107
constexpr auto parse(ParseContext &ctx) -> typename ParseContext::iterator
Definition Rank.hpp:100