BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
Score.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 <cmath> // IWYU pragma: keep - for std::abs()
24#include <cstddef> // IWYU pragma: keep - for size_t
25#include <cstdint> // IWYU pragma: keep - for std::int16_t
26#include <format>
28#include <limits>
29#include <utility>
30
31namespace ben_bot::eval {
32
33using std::size_t;
34
38using Value = std::int16_t;
39
46inline constexpr Value MAX { std::numeric_limits<Value>::max() - UINT16_C(5) };
47
54inline constexpr Value MATE { MAX / UINT16_C(2) };
55
59inline constexpr Value DRAW { UINT16_C(0) };
60
67struct Score final {
69 Value value { UINT16_C(0) };
70
75 constexpr operator Value() const noexcept { return value; }
76
78 [[nodiscard]] constexpr auto operator-() const noexcept -> Score { return { static_cast<Value>(-value) }; }
79
82
84 [[nodiscard]] auto is_mate() const noexcept -> bool { return std::cmp_greater_equal(std::abs(value), MATE); }
85
87 [[nodiscard]] constexpr auto is_winning_mate() const noexcept -> bool { return std::cmp_greater_equal(value, MATE); }
88
90 [[nodiscard]] constexpr auto is_losing_mate() const noexcept -> bool { return std::cmp_less_equal(value, -MATE); }
91
95 [[nodiscard]] auto ply_to_mate() const noexcept -> size_t
96 {
97 assert(is_mate());
98
99 return static_cast<size_t>(MAX - std::abs(value));
100 }
101
103
110 [[nodiscard]] constexpr auto to_tt(size_t plyFromRoot) const noexcept -> Value;
111
114
116 [[nodiscard]] auto to_libchess() const noexcept -> LibchessScore;
117
122 [[nodiscard, gnu::const]] static constexpr auto mate(const size_t plyFromRoot) noexcept -> Score
123 {
124 // multiply by -1 here because this score is relative to the player who got mated
125 return { static_cast<Value>(
126 (MAX - static_cast<Value>(plyFromRoot)) * -1) };
127 }
128
132 [[nodiscard, gnu::const]] static constexpr auto from_tt(
133 Value eval, size_t plyFromRoot) noexcept
134 -> Score;
135};
136
137/*
138 ___ ,--,
139 ,---, ,--.'|_ ,--, ,--.'|
140 ,---.'| | | :,' ,--.'| | | :
141 | | : : : ' : | |, : : ' .--.--.
142 | | | ,---. .;__,' / ,--.--. `--'_ | ' | / / '
143 ,--.__| | / \ | | | / \ ,' ,'| ' | | | : /`./
144 / ,' | / / |:__,'| : .--. .-. | ' | | | | : | : ;_
145. ' / |. ' / | ' : |__ \__\/: . . | | : ' : |__ \ \ `.
146' ; |: |' ; /| | | '.'| ," .--.; | ' : |__ | | '.'| `----. \
147| | '/ '' | / | ; : ;/ / ,. | | | '.'|; : ;/ /`--' /__ ___ ___
148| : :|| : | | , /; : .' \; : ;| , /'--'. / .\/ .\/ .\
149 \ \ / \ \ / ---`-' | , .-./| , / ---`-' `--'---'\ ; \ ; \ ; |
150 `----' `----' `--`---' ---`-' `--" `--" `--"
151
152 */
153
154constexpr auto Score::to_tt(const size_t plyFromRoot) const noexcept -> Value
155{
156 const auto ply = static_cast<Value>(plyFromRoot);
157
158 if (is_losing_mate())
159 return static_cast<Value>(value - ply);
160
161 if (is_winning_mate())
162 return static_cast<Value>(value + ply);
163
164 return value;
165}
166
167constexpr auto Score::from_tt(
168 const Value eval, const size_t plyFromRoot) noexcept
169 -> Score
170{
171 const auto ply = static_cast<Value>(plyFromRoot);
172
173 if (std::cmp_less_equal(eval, -MATE))
174 return { static_cast<Value>(eval + ply) };
175
176 if (std::cmp_greater_equal(eval, MATE))
177 return { static_cast<Value>(eval - ply) };
178
179 return { eval };
180}
181
182inline auto Score::to_libchess() const noexcept -> LibchessScore
183{
184 LibchessScore res;
185
186 if (is_mate()) {
187 auto ply = static_cast<int>(ply_to_mate());
188
189 if (is_losing_mate())
190 ply *= -1;
191
192 res.value = LibchessScore::MateIn { .plies = ply };
193 } else {
194 res.value = LibchessScore::Centipawns { .value = value };
195 }
196
197 return res;
198}
199
200} // namespace ben_bot::eval
201
209template <>
210struct std::formatter<ben_bot::eval::Score> final {
211 template <typename ParseContext>
212 // cppcheck-suppress functionStatic
213 constexpr auto parse(ParseContext& ctx) -> typename ParseContext::iterator
214 {
215 return ctx.begin();
216 }
217
218 template <typename FormatContext>
219 // cppcheck-suppress functionStatic
220 auto format(
221 const ben_bot::eval::Score& score, FormatContext& ctx) const
222 -> typename FormatContext::iterator
223 {
224 return std::format_to(ctx.out(), "{}", score.value);
225 }
226};
std::int16_t Value
Definition Score.hpp:38
constexpr Value DRAW
Definition Score.hpp:59
constexpr Value MAX
Definition Score.hpp:46
constexpr Value MATE
Definition Score.hpp:54
auto is_mate() const noexcept -> bool
Definition Score.hpp:84
constexpr auto is_losing_mate() const noexcept -> bool
Definition Score.hpp:90
static constexpr auto from_tt(Value eval, size_t plyFromRoot) noexcept -> Score
Definition Score.hpp:167
chess::uci::printing::SearchInfo::Score LibchessScore
Definition Score.hpp:113
constexpr auto operator-() const noexcept -> Score
Definition Score.hpp:78
auto to_libchess() const noexcept -> LibchessScore
Definition Score.hpp:182
constexpr auto to_tt(size_t plyFromRoot) const noexcept -> Value
Definition Score.hpp:154
constexpr auto is_winning_mate() const noexcept -> bool
Definition Score.hpp:87
static constexpr auto mate(const size_t plyFromRoot) noexcept -> Score
Definition Score.hpp:122
auto ply_to_mate() const noexcept -> size_t
Definition Score.hpp:95
std::variant< Centipawns, MateIn > value
Definition Printing.hpp:117
auto format(const ben_bot::eval::Score &score, FormatContext &ctx) const -> typename FormatContext::iterator
Definition Score.hpp:220
constexpr auto parse(ParseContext &ctx) -> typename ParseContext::iterator
Definition Score.hpp:213