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 constexpr auto parse(ParseContext& ctx) -> typename ParseContext::iterator
100 {
101 return ctx.begin();
102 }
103
104 template <typename FormatContext>
105 auto format(
106 const chess::board::Rank rank, FormatContext& ctx) const
107 -> typename FormatContext::iterator
108 {
109 return std::format_to(ctx.out(), "{}", rank_to_char(rank));
110 }
111};
112
113/*
114 ___ ,--,
115 ,---, ,--.'|_ ,--, ,--.'|
116 ,---.'| | | :,' ,--.'| | | :
117 | | : : : ' : | |, : : ' .--.--.
118 | | | ,---. .;__,' / ,--.--. `--'_ | ' | / / '
119 ,--.__| | / \ | | | / \ ,' ,'| ' | | | : /`./
120 / ,' | / / |:__,'| : .--. .-. | ' | | | | : | : ;_
121. ' / |. ' / | ' : |__ \__\/: . . | | : ' : |__ \ \ `.
122' ; |: |' ; /| | | '.'| ," .--.; | ' : |__ | | '.'| `----. \
123| | '/ '' | / | ; : ;/ / ,. | | | '.'|; : ;/ /`--' /__ ___ ___
124| : :|| : | | , /; : .' \; : ;| , /'--'. / .\/ .\/ .\
125 \ \ / \ \ / ---`-' | , .-./| , / ---`-' `--'---'\ ; \ ; \ ; |
126 `----' `----' `--`---' ---`-' `--" `--" `--"
127
128 */
129
130namespace chess::board {
131
132constexpr auto back_rank_for(const Color color) noexcept -> Rank
133{
134 if (color == Color::White)
135 return Rank::One;
136
137 return Rank::Eight;
138}
139
140template <Color Side>
141constexpr auto next_pawn_rank(const Rank rank) noexcept -> Rank
142{
143 if constexpr (Side == Color::White) {
144 assert(rank != Rank::Eight);
145 return static_cast<Rank>(std::to_underlying(rank) + 1uz);
146 } else {
147 assert(rank != Rank::One);
148 return static_cast<Rank>(std::to_underlying(rank) - 1uz);
149 }
150}
151
152template <Color Side>
153constexpr auto prev_pawn_rank(const Rank rank) noexcept -> Rank
154{
155 if constexpr (Side == Color::White) {
156 assert(rank != Rank::One);
157 return static_cast<Rank>(std::to_underlying(rank) - 1uz);
158 } else {
159 assert(rank != Rank::Eight);
160 return static_cast<Rank>(std::to_underlying(rank) + 1uz);
161 }
162}
163
164inline auto rank_from_char(const char character)
165 -> std::expected<Rank, std::string>
166{
167 switch (character) {
168 case '1': return Rank::One;
169 case '2': return Rank::Two;
170 case '3': return Rank::Three;
171 case '4': return Rank::Four;
172 case '5': return Rank::Five;
173 case '6': return Rank::Six;
174 case '7': return Rank::Seven;
175 case '8': return Rank::Eight;
176
177 default:
178 return std::unexpected {
179 std::format(
180 "Cannot parse Rank from character: {}",
181 character)
182 };
183 }
184}
185
186constexpr auto rank_to_char(const Rank rank) -> char
187{
188 constexpr std::string_view ranks { "12345678" };
189
190 return ranks.at(std::to_underlying(rank));
191}
192
193} // namespace chess::board
constexpr auto prev_pawn_rank(Rank rank) noexcept -> Rank
Definition Rank.hpp:153
std::uint_fast8_t BitboardIndex
auto rank_from_char(char character) -> std::expected< Rank, std::string >
Definition Rank.hpp:164
constexpr auto back_rank_for(Color color) noexcept -> Rank
Definition Rank.hpp:132
constexpr auto rank_to_char(Rank rank) -> char
Definition Rank.hpp:186
constexpr auto next_pawn_rank(Rank rank) noexcept -> Rank
Definition Rank.hpp:141
@ 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:105
constexpr auto parse(ParseContext &ctx) -> typename ParseContext::iterator
Definition Rank.hpp:99