BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
File.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 <cctype> // IWYU pragma: keep - for std::tolower()
23#include <expected>
24#include <format>
26#include <magic_enum/magic_enum.hpp>
27#include <string>
28
29namespace chess::board {
30
36enum class File : BitboardIndex {
37 A,
38 B,
39 C,
40 D,
41 E,
42 F,
43 G,
45};
46
55[[nodiscard]] auto file_from_char(char character)
56 -> std::expected<File, std::string>;
57
63[[nodiscard]] auto file_to_char(File file) -> char;
64
65} // namespace chess::board
66
73template <>
74struct std::formatter<chess::board::File> final {
75 template <typename ParseContext>
76 // cppcheck-suppress functionStatic
77 constexpr auto parse(ParseContext& ctx) -> typename ParseContext::iterator
78 {
79 return ctx.begin();
80 }
81
82 template <typename FormatContext>
83 // cppcheck-suppress functionStatic
84 auto format(
85 chess::board::File file, FormatContext& ctx) const -> typename FormatContext::iterator
86 {
87 return std::format_to(ctx.out(), "{}", chess::board::file_to_char(file));
88 }
89};
90
91/*
92 ___ ,--,
93 ,---, ,--.'|_ ,--, ,--.'|
94 ,---.'| | | :,' ,--.'| | | :
95 | | : : : ' : | |, : : ' .--.--.
96 | | | ,---. .;__,' / ,--.--. `--'_ | ' | / / '
97 ,--.__| | / \ | | | / \ ,' ,'| ' | | | : /`./
98 / ,' | / / |:__,'| : .--. .-. | ' | | | | : | : ;_
99. ' / |. ' / | ' : |__ \__\/: . . | | : ' : |__ \ \ `.
100' ; |: |' ; /| | | '.'| ," .--.; | ' : |__ | | '.'| `----. \
101| | '/ '' | / | ; : ;/ / ,. | | | '.'|; : ;/ /`--' /__ ___ ___
102| : :|| : | | , /; : .' \; : ;| , /'--'. / .\/ .\/ .\
103 \ \ / \ \ / ---`-' | , .-./| , / ---`-' `--'---'\ ; \ ; \ ; |
104 `----' `----' `--`---' ---`-' `--" `--" `--"
105
106 */
107
108namespace chess::board {
109
110inline auto file_from_char(char character)
111 -> std::expected<File, std::string>
112{
113 switch (character) {
114 case 'a': [[fallthrough]];
115 case 'A': return File::A;
116
117 case 'b': [[fallthrough]];
118 case 'B': return File::B;
119
120 case 'c': [[fallthrough]];
121 case 'C': return File::C;
122
123 case 'd': [[fallthrough]];
124 case 'D': return File::D;
125
126 case 'e': [[fallthrough]];
127 case 'E': return File::E;
128
129 case 'f': [[fallthrough]];
130 case 'F': return File::F;
131
132 case 'g': [[fallthrough]];
133 case 'G': return File::G;
134
135 case 'h': [[fallthrough]];
136 case 'H': return File::H;
137
138 default:
139 return std::unexpected {
140 std::format(
141 "Cannot parse File from character: {}",
142 character)
143 };
144 }
145}
146
147inline auto file_to_char(const File file) -> char
148{
149 const auto upperChar = magic_enum::enum_name(file).front();
150
151 return static_cast<char>(
152 std::tolower(static_cast<unsigned char>(upperChar)));
153}
154
155} // namespace chess::board
std::uint_fast8_t BitboardIndex
auto file_from_char(char character) -> std::expected< File, std::string >
Definition File.hpp:110
auto file_to_char(File file) -> char
Definition File.hpp:147
@ C
The C file.
Definition File.hpp:39
@ 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
@ F
The F file.
Definition File.hpp:42
@ B
The B file.
Definition File.hpp:38
@ H
The H file.
Definition File.hpp:44
@ G
The G file.
Definition File.hpp:43
@ D
The D file. This is the file that the queens start on.
Definition File.hpp:40
auto format(chess::board::File file, FormatContext &ctx) const -> typename FormatContext::iterator
Definition File.hpp:84
constexpr auto parse(ParseContext &ctx) -> typename ParseContext::iterator
Definition File.hpp:77