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 // cppcheck-suppress functionStatic
88 constexpr auto parse(ParseContext& ctx) -> typename ParseContext::iterator
89 {
90 return ctx.begin();
91 }
92
93 template <typename FormatContext>
94 // cppcheck-suppress functionStatic
95 auto format(
96 chess::pieces::Type piece, FormatContext& ctx) const
97 -> typename FormatContext::iterator
98 {
99 return std::format_to(ctx.out(), "{}", to_char(piece));
100 }
101};
102
103/*
104 ___ ,--,
105 ,---, ,--.'|_ ,--, ,--.'|
106 ,---.'| | | :,' ,--.'| | | :
107 | | : : : ' : | |, : : ' .--.--.
108 | | | ,---. .;__,' / ,--.--. `--'_ | ' | / / '
109 ,--.__| | / \ | | | / \ ,' ,'| ' | | | : /`./
110 / ,' | / / |:__,'| : .--. .-. | ' | | | | : | : ;_
111. ' / |. ' / | ' : |__ \__\/: . . | | : ' : |__ \ \ `.
112' ; |: |' ; /| | | '.'| ," .--.; | ' : |__ | | '.'| `----. \
113| | '/ '' | / | ; : ;/ / ,. | | | '.'|; : ;/ /`--' /__ ___ ___
114| : :|| : | | , /; : .' \; : ;| , /'--'. / .\/ .\/ .\
115 \ \ / \ \ / ---`-' | , .-./| , / ---`-' `--'---'\ ; \ ; \ ; |
116 `----' `----' `--`---' ---`-' `--" `--" `--"
117
118 */
119
120namespace chess::pieces {
121
122constexpr auto to_char(const Type type, const bool uppercase) -> char
123{
124 if (uppercase) {
125 static constexpr std::string_view upperChars { "PNBRQK" };
126
127 return upperChars.at(std::to_underlying(type));
128 }
129
130 static constexpr std::string_view lowerChars { "pnbrqk" };
131
132 return lowerChars.at(std::to_underlying(type));
133}
134
135inline auto from_string(std::string_view text)
136 -> std::expected<Type, std::string>
137{
138 if (text.length() != 1uz)
139 text = text.substr(0uz, 1uz);
140
141 switch (text.front()) {
142 case 'p': [[fallthrough]];
143 case 'P': return Type::Pawn;
144
145 case 'n': [[fallthrough]];
146 case 'N': return Type::Knight;
147
148 case 'b': [[fallthrough]];
149 case 'B': return Type::Bishop;
150
151 case 'r': [[fallthrough]];
152 case 'R': return Type::Rook;
153
154 case 'q': [[fallthrough]];
155 case 'Q': return Type::Queen;
156
157 case 'k': [[fallthrough]];
158 case 'K': return Type::King;
159
160 default:
161 return std::unexpected {
162 std::format(
163 "Cannot parse piece type from invalid input string: {}",
164 text)
165 };
166 }
167}
168
169} // 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