-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathuci.hpp
354 lines (345 loc) · 13.4 KB
/
uci.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
#ifndef UCI_HPP_
#define UCI_HPP_
#include <cstddef>
#include <iostream>
#include <map>
#include <sstream>
#include <string>
#include <vector>
#include <boost/signals2.hpp>
// Based on the UCI protocol (April 2006). Extracted from https://github.com/acdemiralp/mechanical_turk
class uci final
{
public:
enum class command
{
search_moves ,
ponder ,
white_time ,
black_time ,
white_increment,
black_increment,
moves_to_go ,
depth ,
nodes ,
mate ,
move_time ,
infinite
};
enum class state
{
checking,
ok ,
error
};
enum class information
{
depth ,
selective_depth ,
time ,
nodes ,
principle_variation ,
multi_principle_variation,
score_centipawns ,
score_mate ,
score_lowerbound ,
score_upperbound ,
current_move ,
current_move_number ,
hash_full ,
nodes_per_second ,
table_base_hits ,
cpu_load ,
string ,
refutation ,
current_line
};
const std::string start_fen = "rnbqkbnr/pppppppp/8/8/8/8/PPPPPPPP/RNBQKBNR w KQkq - 0 1";
// UI to Engine.
boost::signals2::signal<void()> receive_uci ;
boost::signals2::signal<void(bool on)> receive_debug ;
boost::signals2::signal<void()> receive_is_ready ;
boost::signals2::signal<void( const std::string& name, const std::string& value)> receive_set_option ;
boost::signals2::signal<void(bool later, const std::string& name, const std::size_t& code )> receive_register ;
boost::signals2::signal<void()> receive_uci_new_game;
boost::signals2::signal<void(const std::string& fen, const std::vector<std::string>& moves)> receive_position ;
boost::signals2::signal<void(const std::map<command, std::string>& parameters)> receive_go ;
boost::signals2::signal<void()> receive_stop ;
boost::signals2::signal<void()> receive_ponder_hit ;
boost::signals2::signal<void()> receive_quit ;
// Engine to UI.
static void send_id (const std::string& name = "", const std::string& author = "")
{
std::cout << "id" << (!name.empty() ? " name " + name : "") << (!author.empty() ? " author " + author : "") << std::endl;
}
static void send_uci_ok ()
{
std::cout << "uciok" << std::endl;
}
static void send_ready_ok ()
{
std::cout << "readyok" << std::endl;
}
static void send_best_move (const std::string& move, const bool ponder = false)
{
std::cout << "bestmove" << (ponder ? " ponder" : "") << std::endl;
}
static void send_copy_protection (const state state)
{
std::string string = "copyprotection";
if (state == state::checking) string += " checking";
else if (state == state::ok ) string += " ok";
else if (state == state::error ) string += " error";
std::cout << string << std::endl;
}
static void send_registration (const state state)
{
std::string string = "registration";
if (state == state::checking) string += " checking";
else if (state == state::ok ) string += " ok";
else if (state == state::error ) string += " error";
std::cout << string << std::endl;
}
static void send_information (const std::map<information, std::string>& parameters)
{
std::string string = "info";
for (auto& parameter : parameters)
{
switch(parameter.first)
{
case information::depth : string += " depth " ; break;
case information::selective_depth : string += " seldepth " ; break;
case information::time : string += " time " ; break;
case information::nodes : string += " nodes " ; break;
case information::principle_variation : string += " pv " ; break;
case information::multi_principle_variation: string += " multipv " ; break;
case information::current_move : string += " currmove " ; break;
case information::current_move_number : string += " currmovenumber "; break;
case information::hash_full : string += " hashfull " ; break;
case information::nodes_per_second : string += " nps " ; break;
case information::table_base_hits : string += " tbhits " ; break;
case information::cpu_load : string += " cpuload " ; break;
case information::string : string += " string " ; break;
case information::refutation : string += " refutation " ; break;
case information::current_line : string += " currline " ; break;
default : break;
}
string += parameter.second;
}
if (parameters.count(information::score_centipawns) > 0 || parameters.count(information::score_mate) > 0)
{
string += " score";
if (parameters.count(information::score_centipawns))
string += " cp " + parameters.at(information::score_centipawns);
if (parameters.count(information::score_mate ))
string += " mate " + parameters.at(information::score_mate );
if (parameters.count(information::score_lowerbound))
string += " lowerbound";
if (parameters.count(information::score_upperbound))
string += " upperbound";
}
std::cout << string << std::endl;
}
static void send_option_check_box (const std::string& name, const bool initial = true)
{
std::cout << "option name " << name << " type check default " << std::boolalpha << initial << std::endl;
}
static void send_option_spin_wheel (const std::string& name, const std::size_t& initial, const std::size_t& minimum, const std::size_t& maximum)
{
std::cout << "option name " << name << " type spin default " << initial << " min " << minimum << " max " << maximum << std::endl;
}
static void send_option_combo_box (const std::string& name, const std::string& initial, const std::vector<std::string>& values)
{
auto string = "option name " + name + " type combo default " + initial;
for(auto& value : values)
string += " var " + value;
std::cout << string << std::endl;
}
static void send_option_button (const std::string& name)
{
std::cout << "option name " << name << " type button" << std::endl;
}
static void send_option_string (const std::string& name, const std::string& initial)
{
std::cout << "option name " << name << " type string default " << initial << std::endl;
}
// Base options.
static void send_option_hash (const std::size_t& initial, const std::size_t& minimum, const std::size_t& maximum)
{
send_option_spin_wheel("Hash" , initial, minimum, maximum);
}
static void send_option_nalimov_path (const std::string& initial)
{
send_option_string ("NalimovPath" , initial);
}
static void send_option_nalimov_cache (const std::size_t& initial, const std::size_t& minimum, const std::size_t& maximum)
{
send_option_spin_wheel("NalimovCache" , initial, minimum, maximum);
}
static void send_option_ponder (const bool initial = true)
{
send_option_check_box ("Ponder" , initial);
}
static void send_option_own_book (const bool initial = true)
{
send_option_check_box ("OwnBook" , initial);
}
static void send_option_multi_principle_variation (const std::size_t& initial, const std::size_t& minimum, const std::size_t& maximum)
{
send_option_spin_wheel("MultiPV" , initial, minimum, maximum);
}
static void send_option_uci_show_current_line (const bool initial = true)
{
send_option_check_box ("UCI_ShowCurrLine" , initial);
}
static void send_option_uci_show_refutations (const bool initial = true)
{
send_option_check_box ("UCI_ShowRefutations" , initial);
}
static void send_option_uci_limit_strength (const bool initial = true)
{
send_option_check_box ("UCI_LimitStrength" , initial);
}
static void send_option_uci_elo (const std::size_t& initial, const std::size_t& minimum, const std::size_t& maximum)
{
send_option_spin_wheel("UCI_Elo" , initial, minimum, maximum);
}
static void send_option_uci_analyse_mode (bool const initial = true)
{
send_option_check_box ("UCI_AnalyseMode" , initial);
}
static void send_option_uci_opponent (const std::string& initial)
{
send_option_string ("UCI_Opponent" , initial);
}
static void send_option_uci_about (const std::string& initial)
{
send_option_string ("UCI_EngineAbout" , initial);
}
static void send_option_uci_set_position_centipawns(const std::string& initial)
{
send_option_string ("UCI_SetPositionValue", initial);
}
// Start console IO.
void launch()
{
std::string line;
auto running = true;
while (running && getline(std::cin, line))
{
std::istringstream iss(line);
std::string token;
iss >> std::skipws >> token;
if (token == "uci" )
{
receive_uci();
}
else if (token == "debug" )
{
iss >> token;
receive_debug(token == "on");
}
else if (token == "isready" )
{
receive_is_ready();
}
else if (token == "setoption" )
{
std::string name, value;
iss >> token;
while (iss >> token && token != "value")
name += std::string(" ", name .empty() ? 0 : 1) + token;
while (iss >> token)
value += std::string(" ", value.empty() ? 0 : 1) + token;
receive_set_option(name, value);
}
else if (token == "register" )
{
auto later = false;
std::string name ;
std::size_t code = 0;
iss >> token;
if (token == "later")
later = true;
if (token == "name" )
while (iss >> token && token != "code")
name += std::string(" ", name.empty() ? 0 : 1) + token;
if (token == "code")
iss >> code;
receive_register(later, name, code);
}
else if (token == "ucinewgame")
{
receive_uci_new_game();
}
else if (token == "position" )
{
std::string fen ;
std::vector<std::string> moves;
iss >> token;
if (token == "startpos")
{
fen = start_fen;
iss >> token;
}
else if (token == "fen")
while(iss >> token && token != "moves")
fen += token + " ";
else
continue;
while (iss >> token)
moves.push_back(token);
receive_position(fen, moves);
}
else if (token == "go" )
{
std::map<command, std::string> commands;
while (iss >> token)
if (token == "searchmoves")
while (iss >> token)
commands[command::search_moves] += std::string(" ", commands[command::search_moves].empty() ? 0 : 1) + token;
else if (token == "ponder" )
commands[command::ponder];
else if (token == "wtime" )
iss >> commands[command::white_time ];
else if (token == "btime" )
iss >> commands[command::black_time ];
else if (token == "winc" )
iss >> commands[command::white_increment];
else if (token == "binc" )
iss >> commands[command::black_increment];
else if (token == "movestogo" )
iss >> commands[command::moves_to_go ];
else if (token == "depth" )
iss >> commands[command::depth ];
else if (token == "nodes" )
iss >> commands[command::nodes ];
else if (token == "mate" )
iss >> commands[command::mate ];
else if (token == "move_time" )
iss >> commands[command::move_time ];
else if (token == "infinite" )
commands[command::infinite];
receive_go(commands);
}
else if (token == "stop" )
{
receive_stop();
}
else if (token == "ponderhit" )
{
receive_ponder_hit();
}
else if (token == "quit" )
{
receive_quit();
running = false;
}
else
{
std::cout << "Unrecognized command: " << line << std::endl;
}
}
}
};
#endif