BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
PieceTypes.hpp
Go to the documentation of this file.
1/*
2 * ======================================================================================
3 *
4 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓███████▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░▒▓████████▓▒░
5 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
6 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
7 * ░▒▓███████▓▒░░▒▓██████▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
8 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
9 * ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░░▒▓█▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓█▓▒░
10 * ░▒▓███████▓▒░░▒▓████████▓▒░▒▓█▓▒░░▒▓█▓▒░ ░▒▓███████▓▒░ ░▒▓██████▓▒░ ░▒▓█▓▒░
11 *
12 * ======================================================================================
13 */
14
20
25
26#pragma once
27
28#include <cstddef> // IWYU pragma: keep - for size_t
29#include <cstdint> // IWYU pragma: keep - for std::uint_fast8_t
30#include <expected>
31#include <format>
32#include <string>
33#include <string_view>
34#include <utility>
35
39namespace chess::pieces {
40
41using std::size_t;
42
48enum class Type : std::uint_fast8_t {
55};
56
65[[nodiscard]] auto from_string(std::string_view text)
66 -> std::expected<Type, std::string>;
67
72[[nodiscard, gnu::const]] constexpr auto to_char(
73 Type type, bool uppercase = true)
74 -> char;
75
76} // namespace chess::pieces
77
84template <>
85struct std::formatter<chess::pieces::Type> final {
86 template <typename ParseContext>
87 constexpr auto parse(ParseContext& ctx) -> typename ParseContext::iterator
88 {
89 return ctx.begin();
90 }
91
92 template <typename FormatContext>
93 auto format(
94 chess::pieces::Type piece, FormatContext& ctx) const
95 -> typename FormatContext::iterator
96 {
97 return std::format_to(ctx.out(), "{}", to_char(piece));
98 }
99};
100
101/*
102 ___ ,--,
103 ,---, ,--.'|_ ,--, ,--.'|
104 ,---.'| | | :,' ,--.'| | | :
105 | | : : : ' : | |, : : ' .--.--.
106 | | | ,---. .;__,' / ,--.--. `--'_ | ' | / / '
107 ,--.__| | / \ | | | / \ ,' ,'| ' | | | : /`./
108 / ,' | / / |:__,'| : .--. .-. | ' | | | | : | : ;_
109. ' / |. ' / | ' : |__ \__\/: . . | | : ' : |__ \ \ `.
110' ; |: |' ; /| | | '.'| ," .--.; | ' : |__ | | '.'| `----. \
111| | '/ '' | / | ; : ;/ / ,. | | | '.'|; : ;/ /`--' /__ ___ ___
112| : :|| : | | , /; : .' \; : ;| , /'--'. / .\/ .\/ .\
113 \ \ / \ \ / ---`-' | , .-./| , / ---`-' `--'---'\ ; \ ; \ ; |
114 `----' `----' `--`---' ---`-' `--" `--" `--"
115
116 */
117
118namespace chess::pieces {
119
120constexpr auto to_char(const Type type, const bool uppercase) -> char
121{
122 if (uppercase) {
123 static constexpr std::string_view upperChars { "PNBRQK" };
124
125 return upperChars.at(std::to_underlying(type));
126 }
127
128 static constexpr std::string_view lowerChars { "pnbrqk" };
129
130 return lowerChars.at(std::to_underlying(type));
131}
132
133inline auto from_string(std::string_view text)
134 -> std::expected<Type, std::string>
135{
136 if (text.length() != 1uz)
137 text = text.substr(0uz, 1uz);
138
139 switch (text.front()) {
140 case 'p': [[fallthrough]];
141 case 'P': return Type::Pawn;
142
143 case 'n': [[fallthrough]];
144 case 'N': return Type::Knight;
145
146 case 'b': [[fallthrough]];
147 case 'B': return Type::Bishop;
148
149 case 'r': [[fallthrough]];
150 case 'R': return Type::Rook;
151
152 case 'q': [[fallthrough]];
153 case 'Q': return Type::Queen;
154
155 case 'k': [[fallthrough]];
156 case 'K': return Type::King;
157
158 default:
159 return std::unexpected {
160 std::format(
161 "Cannot parse piece type from invalid input string: {}",
162 text)
163 };
164 }
165}
166
167} // namespace chess::pieces
auto from_string(std::string_view text) -> std::expected< Type, std::string >
constexpr auto to_char(Type type, bool uppercase=true) -> char
@ Pawn
A White pawn.
auto format(chess::pieces::Type piece, FormatContext &ctx) const -> typename FormatContext::iterator
constexpr auto parse(ParseContext &ctx) -> typename ParseContext::iterator