BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
Bounds.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 <cstddef> // IWYU pragma: keep - for size_t
24#include <optional>
25
26namespace ben_bot::search {
27
28using eval::Score;
29using std::size_t;
30
36struct Bounds final {
39
42
46 [[nodiscard]] constexpr auto invert() const noexcept -> Bounds;
47
51 [[nodiscard]] constexpr auto null_window() const noexcept -> Bounds;
52
54 [[nodiscard]] constexpr auto contains(Score score) const noexcept -> bool;
55
61 [[nodiscard]] constexpr auto mate_distance_pruning(size_t plyFromRoot) noexcept -> std::optional<Score>;
62};
63
64/*
65 ___ ,--,
66 ,---, ,--.'|_ ,--, ,--.'|
67 ,---.'| | | :,' ,--.'| | | :
68 | | : : : ' : | |, : : ' .--.--.
69 | | | ,---. .;__,' / ,--.--. `--'_ | ' | / / '
70 ,--.__| | / \ | | | / \ ,' ,'| ' | | | : /`./
71 / ,' | / / |:__,'| : .--. .-. | ' | | | | : | : ;_
72. ' / |. ' / | ' : |__ \__\/: . . | | : ' : |__ \ \ `.
73' ; |: |' ; /| | | '.'| ," .--.; | ' : |__ | | '.'| `----. \
74| | '/ '' | / | ; : ;/ / ,. | | | '.'|; : ;/ /`--' /__ ___ ___
75| : :|| : | | , /; : .' \; : ;| , /'--'. / .\/ .\/ .\
76 \ \ / \ \ / ---`-' | , .-./| , / ---`-' `--'---'\ ; \ ; \ ; |
77 `----' `----' `--`---' ---`-' `--" `--" `--"
78
79 */
80
81constexpr auto Bounds::invert() const noexcept -> Bounds
82{
83 return {
84 .alpha = -beta,
85 .beta = -alpha
86 };
87}
88
89constexpr auto Bounds::null_window() const noexcept -> Bounds
90{
91 return {
92 .alpha = Score { static_cast<eval::Value>(-alpha.value - UINT16_C(1)) },
93 .beta = -alpha
94 };
95}
96
97constexpr auto Bounds::contains(const Score score) const noexcept -> bool
98{
99 return score > alpha and score < beta;
100}
101
102constexpr auto Bounds::mate_distance_pruning(const size_t plyFromRoot) noexcept -> std::optional<Score>
103{
104 const auto mateScore = Score::mate(plyFromRoot);
105
106 if (alpha.is_winning_mate()) {
107 if (mateScore < beta) {
108 beta = mateScore;
109
110 if (alpha >= mateScore)
111 return mateScore;
112 }
113
114 return std::nullopt;
115 }
116
117 if (alpha.is_losing_mate()) {
118 if (mateScore > alpha) {
119 alpha = mateScore;
120
121 if (beta <= mateScore)
122 return mateScore;
123 }
124 }
125
126 return std::nullopt;
127}
128
129} // namespace ben_bot::search
std::int16_t Value
Definition Score.hpp:38
constexpr Value MAX
Definition Score.hpp:46
constexpr auto contains(Score score) const noexcept -> bool
Definition Bounds.hpp:97
constexpr auto mate_distance_pruning(size_t plyFromRoot) noexcept -> std::optional< Score >
Definition Bounds.hpp:102
constexpr auto null_window() const noexcept -> Bounds
Definition Bounds.hpp:89
constexpr auto invert() const noexcept -> Bounds
Definition Bounds.hpp:81
static constexpr auto mate(const size_t plyFromRoot) noexcept -> Score
Definition Score.hpp:120