BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
ThreefoldChecker.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 <algorithm>
23#include <beman/inplace_vector/inplace_vector.hpp>
24#include <cstdint> // IWYU pragma: keep - for std::uint64_t
25#include <utility>
26
27namespace chess::game {
28
32struct ThreefoldChecker final {
33 using HashValue = std::uint64_t;
34
36 constexpr void reset(HashValue initialPositionHash);
37
39 constexpr void push(HashValue newHash);
40
42 [[nodiscard]] constexpr auto is_threefold() const noexcept -> bool;
43
45 [[nodiscard]] constexpr auto repetition_count() const noexcept -> size_t;
46
47private:
48 // stores a history of hash values
49 // the oldest value is at front() and the most recent is at back()
50 // The history is reset by every capture & pawn move, so it only needs to hold the positions
51 // since the last one. That's at most 101, since the fifty-move rule draws the game when the
52 // halfmove clock reaches 100, so the history never fills up in a real game.
53 using History = beman::inplace_vector::inplace_vector<HashValue, 101uz>;
54
55 History history;
56};
57
58/*
59 ___ ,--,
60 ,---, ,--.'|_ ,--, ,--.'|
61 ,---.'| | | :,' ,--.'| | | :
62 | | : : : ' : | |, : : ' .--.--.
63 | | | ,---. .;__,' / ,--.--. `--'_ | ' | / / '
64 ,--.__| | / \ | | | / \ ,' ,'| ' | | | : /`./
65 / ,' | / / |:__,'| : .--. .-. | ' | | | | : | : ;_
66. ' / |. ' / | ' : |__ \__\/: . . | | : ' : |__ \ \ `.
67' ; |: |' ; /| | | '.'| ," .--.; | ' : |__ | | '.'| `----. \
68| | '/ '' | / | ; : ;/ / ,. | | | '.'|; : ;/ /`--' /__ ___ ___
69| : :|| : | | , /; : .' \; : ;| , /'--'. / .\/ .\/ .\
70 \ \ / \ \ / ---`-' | , .-./| , / ---`-' `--'---'\ ; \ ; \ ; |
71 `----' `----' `--`---' ---`-' `--" `--" `--"
72
73 */
74
75constexpr void ThreefoldChecker::reset(const HashValue initialPositionHash)
76{
77 history.clear();
78 history.emplace_back(initialPositionHash);
79}
80
81constexpr void ThreefoldChecker::push(const HashValue newHash)
82{
83 // if the history is full, forget the oldest position. That can only happen if more than 100
84 // moves are played without a capture or pawn move, which the fifty-move rule doesn't allow
85 if (std::cmp_equal(history.size(), History::capacity()))
86 history.erase(history.begin());
87
88 history.emplace_back(newHash);
89}
90
91constexpr auto ThreefoldChecker::is_threefold() const noexcept -> bool
92{
93 return std::cmp_greater_equal(
94 repetition_count(), 3uz);
95}
96
97constexpr auto ThreefoldChecker::repetition_count() const noexcept -> size_t
98{
99 return static_cast<size_t>(
100 std::ranges::count(history, history.back()));
101}
102
103} // namespace chess::game
constexpr void reset(HashValue initialPositionHash)
constexpr auto repetition_count() const noexcept -> size_t
constexpr auto is_threefold() const noexcept -> bool
constexpr void push(HashValue newHash)