BenBot 1.7.5
A chess engine
Loading...
Searching...
No Matches
Engine.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 <array>
23#include <expected>
24#include <filesystem>
25#include <functional>
32#include <span>
33#include <string>
34#include <string_view>
35
36namespace chess::uci {
37struct GoCommandOptions;
38} // namespace chess::uci
39
40namespace ben_bot {
41
42namespace search {
43 struct Options;
44} // namespace search
45
46namespace uci = chess::uci;
47
48using chess::game::Position;
49using chess::moves::Move;
51using std::string_view;
53
57class [[nodiscard]] Engine : public uci::EngineBase {
58public:
59 Engine() = default;
60
61 ~Engine() override = default;
62
63 Engine(const Engine&) = delete;
64 Engine& operator=(const Engine&) = delete;
65
66 Engine(Engine&&) = delete;
67 Engine& operator=(Engine&&) = delete;
68
70 void read_config_file(const std::filesystem::path& file);
71
73 [[nodiscard]] auto state_to_string() const -> std::string;
74
80 auto restore_state_from_string(string_view state) -> std::expected<void, std::string>;
81
83 void set_search_options(const search::Options& opts) { searcher.context.set_options(opts); }
84
86 [[nodiscard]] auto get_search_options() const noexcept -> const search::Options&
87 {
88 return searcher.context.get_options();
89 }
90
92 void go(const search::Options& opts);
93
95 [[nodiscard]] auto get_pretty_print_move_format() const -> MoveFormat
96 {
97 return moveFormat.get_enum_value();
98 }
99
101 [[nodiscard]] auto pretty_print_move(
102 Move move, const Position& position) const -> std::string;
103
104protected:
110 [[nodiscard]] virtual auto create_search_callbacks() -> search::Callbacks;
111
112private:
113 [[nodiscard]] auto get_name() const -> std::string override;
114 [[nodiscard]] auto get_author() const -> string_view override { return "Ben Vining"; }
115
116 void new_game(bool firstCall) override;
117
118 void position_changed(const Position& pos) override { searcher.context.set_position(pos); }
119
120 void go(const uci::GoCommandOptions& opts) override;
121
122 void go_internal(search::Options opts);
123
124 void abort_search() override { searcher.context.abort(); }
125
126 void ponder_hit() override { searcher.context.ponder_hit(); }
127
128 void wait() override { searcher.context.wait(); }
129
130 auto is_searching() const noexcept -> bool override { return searcher.context.in_progress(); }
131
132 [[nodiscard]] auto get_custom_uci_options() noexcept -> OptionList override
133 {
134 return options;
135 }
136
137 [[nodiscard]] auto get_custom_uci_commands() const noexcept -> CommandList override
138 {
139 return customCommands;
140 }
141
142 void run_perft(string_view arguments) const;
143
144 void run_bench(string_view arguments) const;
145
146 void make_null_move();
147 void color_flip();
148
149 void print_logo_and_version() const;
150 void print_help(string_view args) const;
151 void print_options();
152 void print_current_position(string_view arguments) const;
153
154 void init_search_callbacks();
155
156 static void print_compiler_info();
157
158 static void start_file_logger(string_view arg);
159
160 void write_config_file(string_view arg) const;
161 void read_config_file(string_view arg);
162
163 void resize_transposition_table(const size_t sizeMB) override
164 {
165 searcher.context.resize_transposition_table(sizeMB);
166 }
167
168 void set_ponder(const bool shouldPonder) override
169 {
170 // the ponder flag is only ever turned on via the go options,
171 // but it can be turned off by disabling this UCI option
172 if (not shouldPonder)
173 searcher.context.set_pondering(false);
174 }
175
176 search::Thread searcher;
177
178 /* ----- UCI options ----- */
179
180 uci::Action clearTT {
181 "Clear Hash",
182 [this] { searcher.context.clear_transposition_table(); },
183 "Press to clear the transposition table."
184 };
185
186 uci::IntOption threads {
187 "Threads", 1, 1, 1,
188 "Number of searcher threads (currently a dummy)."
189 };
190
191 uci::IntOption moveOverhead {
192 "Move Overhead", 0, 5000, 10,
193 "Extra overhead time subtracted from search time (in ms). Increase this if engine loses on time."
194 };
195
196 uci::IntOption multiPV {
197 "MultiPV", 1, static_cast<int>(chess::moves::MAX_MOVES), 1,
198 "Number of best lines to search for and report."
199 };
200
201 uci::StringOption logFile {
202 "Debug Log File", "<empty>",
203 "If not empty, engine I/O will be mirrored to this file.",
205 };
206
207 uci::BoolOption prettyPrintMode {
208 "Pretty Print",
209 false,
210 "When on, search output is pretty-printed instead of printed in UCI format.",
211 [this]([[maybe_unused]] const bool usePretty) { init_search_callbacks(); }
212 };
213
214 uci::EnumOption<MoveFormat> moveFormat {
215 "Move Format",
216 MoveFormat::Algebraic,
217 "Notation format used to display moves in pretty printing mode."
218 };
219
220 uci::BoolOption algFormatUTF8PieceType {
221 "Algebraic Format UTF8",
222 false,
223 "When on and printing algebraic move format, uses UTF8 glyphs for piece types."
224 };
225
226 uci::BoolOption sanitizePositions {
227 "Sanitize Positions",
228 false,
229 "When on, the engine checks if the position is legal before setting it.",
230 [this](const bool sanitize) { set_sanitize_positions(sanitize); }
231 };
232
233 std::array<uci::Option*, 9uz> options {
234 &clearTT, &threads, &moveOverhead, &multiPV, &logFile, &prettyPrintMode, &moveFormat, &algFormatUTF8PieceType, &sanitizePositions
235 };
236
237 std::array<EngineCommand, 10uz> customCommands {
238 EngineCommand {
239 .name = "showpos",
240 .action = [this](const string_view args) { print_current_position(args); },
241 .description = "Prints the current position",
242 .argsHelp = "[utf8]" },
243 EngineCommand {
244 .name = "makenull",
245 .action = EngineCommand::void_cb([this] { make_null_move(); }),
246 .description = "Play a null move on the internal board",
247 .argsHelp = { } },
248 EngineCommand {
249 .name = "flip",
250 .action = EngineCommand::void_cb([this] { color_flip(); }),
251 .description = "Color-flip the current position",
252 .argsHelp = { } },
253 EngineCommand {
254 .name = "options",
255 .action = EngineCommand::void_cb([this] { print_options(); }),
256 .description = "Dump current UCI option values",
257 .argsHelp = { } },
258 EngineCommand {
259 .name = "perft",
260 .action = [this](const string_view args) { run_perft(args); },
261 .description = "Computes perft of the current position to the given depth",
262 .argsHelp = "<N> [json]" },
263 EngineCommand {
264 .name = "bench",
265 .action = [this](const string_view args) { run_bench(args); },
266 .description = "Runs a search and reports total nodes and NPS",
267 .argsHelp = "[<depth>] [<epdPath>]" },
268 EngineCommand {
269 .name = "compiler",
270 .action = EngineCommand::void_cb([] { print_compiler_info(); }),
271 .description = "Print compiler info",
272 .argsHelp = { } },
273 EngineCommand {
274 .name = "writeconfig",
275 .action = [this](const string_view args) { write_config_file(args); },
276 .description = "Writes the engine's current state to a configuration file at the given path",
277 .argsHelp = "<path>" },
278 EngineCommand {
279 .name = "readconfig",
280 .action = [this](const string_view args) { read_config_file(args); },
281 .description = "Loads engine state from a configuration file at the given path",
282 .argsHelp = "<path>" },
283 EngineCommand {
284 .name = "help",
285 .action = [this](const string_view args) { print_help(args); },
286 .description = "Display this text",
287 .argsHelp = "[--no-logo]" }
288 };
289};
290
291} // namespace ben_bot
auto state_to_string() const -> std::string
auto get_author() const -> string_view override
Definition Engine.hpp:114
auto get_custom_uci_commands() const noexcept -> CommandList override
Definition Engine.hpp:137
auto is_searching() const noexcept -> bool override
Definition Engine.hpp:130
void wait() override
Definition Engine.hpp:128
virtual auto create_search_callbacks() -> search::Callbacks
void new_game(bool firstCall) override
Engine & operator=(Engine &&)=delete
auto get_pretty_print_move_format() const -> MoveFormat
Definition Engine.hpp:95
auto get_name() const -> std::string override
~Engine() override=default
Engine()=default
Engine(const Engine &)=delete
Engine & operator=(const Engine &)=delete
void resize_transposition_table(const size_t sizeMB) override
Definition Engine.hpp:163
void go(const search::Options &opts)
void ponder_hit() override
Definition Engine.hpp:126
auto pretty_print_move(Move move, const Position &position) const -> std::string
auto restore_state_from_string(string_view state) -> std::expected< void, std::string >
void abort_search() override
Definition Engine.hpp:124
void read_config_file(const std::filesystem::path &file)
void go(const uci::GoCommandOptions &opts) override
void set_search_options(const search::Options &opts)
Definition Engine.hpp:83
void position_changed(const Position &pos) override
Definition Engine.hpp:118
void set_ponder(const bool shouldPonder) override
Definition Engine.hpp:168
auto get_custom_uci_options() noexcept -> OptionList override
Definition Engine.hpp:132
Engine(Engine &&)=delete
auto get_search_options() const noexcept -> const search::Options &
Definition Engine.hpp:86
constexpr auto MAX_MOVES
Definition MoveGen.hpp:67
auto start_file_logger(const std::filesystem::path &logFile) -> std::expected< void, std::string >
std::span< const EngineCommand > CommandList
std::span< Option * > OptionList