BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
ContinuationHistory.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 <array>
24#include <cstddef> // IWYU pragma: keep - for size_t
25#include <cstdint>
28#include <utility>
29
30namespace ben_bot {
31
32using chess::moves::Move;
33using std::size_t;
34
53struct ContinuationHistory final {
55 static constexpr auto MAX_SCORE { 16384 };
56
58 void clear() noexcept
59 {
60 // row by row, since assigning an empty table would put a 1.1 MiB temporary on the stack
61 for (auto& row : scores)
62 row.fill(0);
63 }
64
70 void age() noexcept
71 {
72 for (auto& row : scores)
73 std::ranges::transform(row, row.begin(), [](const std::int16_t score) {
74 return static_cast<std::int16_t>(score / 2);
75 });
76 }
77
91 void update(
92 const bool prevMoverIsWhite, const Move prevMove,
93 const bool moverIsWhite, const Move move,
94 const int bonus) noexcept
95 {
96 if (prevMove.is_null())
97 return;
98
100 scores[piece_to_index(prevMoverIsWhite, prevMove)][piece_to_index(moverIsWhite, move)],
101 bonus, MAX_SCORE);
102 }
103
107 [[nodiscard]] auto get(
108 const bool prevMoverIsWhite, const Move prevMove,
109 const bool moverIsWhite, const Move move) const noexcept -> int
110 {
111 if (prevMove.is_null())
112 return 0;
113
114 return scores[piece_to_index(prevMoverIsWhite, prevMove)][piece_to_index(moverIsWhite, move)];
115 }
116
117private:
118 static constexpr auto NUM_PIECE_TYPES { 6uz };
119
120 // colored piece type & to-square
121 static constexpr auto NUM_PIECE_TO { 2uz * NUM_PIECE_TYPES * 64uz };
122
123 // white pieces are 0-5 & black pieces are 6-11. For castling the moving
124 // piece is the king, and for promotions it's the pawn
125 [[nodiscard]] static constexpr auto piece_to_index(const bool isWhite, const Move move) noexcept -> size_t
126 {
127 const auto coloredPiece = (isWhite ? 0uz : NUM_PIECE_TYPES)
128 + static_cast<size_t>(std::to_underlying(move.piece()));
129
130 return (coloredPiece * 64uz) + static_cast<size_t>(move.to().index());
131 }
132
133 using Row = std::array<std::int16_t, NUM_PIECE_TO>;
134
135 std::array<Row, NUM_PIECE_TO> scores { };
136};
137
138} // namespace ben_bot
void apply_history_gravity(Entry &entry, const int bonus, const int maxScore) noexcept
auto get(const bool prevMoverIsWhite, const Move prevMove, const bool moverIsWhite, const Move move) const noexcept -> int
void update(const bool prevMoverIsWhite, const Move prevMove, const bool moverIsWhite, const Move move, const int bonus) noexcept