-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathuci.cpp
More file actions
195 lines (176 loc) · 5.9 KB
/
uci.cpp
File metadata and controls
195 lines (176 loc) · 5.9 KB
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
#include <iostream>
#include <sstream>
#include <algorithm>
#include "uci.h"
#include "search.h"
#include "timeman.h"
#include "ucioption.h"
#include <position.h>
using namespace engine;
chess::Position pos;
OptionsMap engine::options;
void handlePosition(std::istringstream &is) {
std::string token, fen;
is >> token;
if (token == "startpos")
{
fen = chess::Position::START_FEN;
is >> token; // Consume the "moves" token, if any
}
else if (token == "fen")
while (is >> token && token != "moves")
fen += token + " ";
else
return;
pos.setFen(fen);
while (is >> token)
{
pos.push_uci(token);
}
}
timeman::LimitsType parse_limits(std::istream &is) {
timeman::LimitsType limits;
std::string token;
limits.startTime = timeman::now(); // The search starts as early as possible
while (is >> token)
if (token == "searchmoves") // Needs to be the last command on the line
while (is >> token) {
std::transform(token.begin(), token.end(), token.begin(), [](auto c) { return std::tolower(c); });
limits.searchmoves.push_back(token);
}
else if (token == "wtime")
is >> limits.time[chess::WHITE];
else if (token == "btime")
is >> limits.time[chess::BLACK];
else if (token == "winc")
is >> limits.inc[chess::WHITE];
else if (token == "binc")
is >> limits.inc[chess::BLACK];
else if (token == "movestogo")
is >> limits.movestogo;
else if (token == "depth")
is >> limits.depth;
else if (token == "nodes")
is >> limits.nodes;
else if (token == "movetime")
is >> limits.movetime;
else if (token == "mate")
is >> limits.mate;
else if (token == "perft")
is >> limits.perft;
else if (token == "infinite")
limits.infinite = 1;
else if (token == "ponder");
//std::cerr << "Pondering not supported!" << std::endl;
return limits;
}
void handleGo(std::istringstream &ss) {
search::search(pos, parse_limits(ss));
}
template<typename... Ts>
struct overload : Ts... {
using Ts::operator()...;
};
template<typename... Ts>
overload(Ts...) -> overload<Ts...>;
std::string engine::format_score(const Score &s) {
const auto format =
overload{ [](Score::Mate mate) -> std::string {
auto m = (mate.plies > 0 ? (mate.plies + 1) : mate.plies) / 2;
return std::string("mate ") + std::to_string(m);
},
[](Score::Tablebase tb) -> std::string {
constexpr int TB_CP = 20000;
return std::string("cp ")
+ std::to_string((tb.win ? TB_CP - tb.plies : -TB_CP - tb.plies));
},
[](Score::InternalUnits units) -> std::string {
return std::string("cp ") + std::to_string(units.value);
} };
return s.visit(format);
}
void engine::report(const InfoShort &info) {
std::cout << "info depth " << info.depth << " score " << format_score(info.score) << std::endl;
}
void engine::report(const InfoFull &info, bool showWDL) {
std::stringstream ss;
ss << "info";
ss << " depth " << info.depth //
<< " seldepth " << info.selDepth //
<< " multipv " << info.multiPV //
<< " score " << format_score(info.score); //
if (!info.bound.empty())
ss << " " << info.bound;
if (showWDL)
ss << " wdl " << info.wdl;
ss << " nodes " << info.nodes //
<< " nps " << info.nps //
<< " hashfull " << info.hashfull //
<< " tbhits " << info.tbHits //
<< " time " << info.timeMs //
<< " pv " << info.pv; //
std::cout << ss.str() << std::endl;
}
void engine::report(const InfoIteration &info) {
std::stringstream ss;
ss << "info";
ss << " depth " << info.depth //
<< " currmove " << info.currmove //
<< " currmovenumber " << info.currmovenumber; //
std::cout << ss.str() << std::endl;
}
void engine::report(std::string_view bestmove) {
std::cout << "bestmove " << bestmove;
std::cout << std::endl;
}
void engine::loop() {
std::string line;
pos.setFen(pos.START_FEN);
std::cout << "cppchess_engine version " << BUILD_VERSION << '\n';
while (std::getline(std::cin, line)) {
std::istringstream ss(line);
std::string token;
while (ss >> token) {
if (token == "uci") {
std::cout << "id name cppchess_engine\n";
std::cout << "id author winapiadmin\n";
std::cout << options << '\n';
std::cout << "uciok\n";
std::cout.flush();
break;
}
else if (token == "isready") {
std::cout << "readyok\n";
std::cout.flush();
break;
}
else if (token == "position") {
handlePosition(ss);
break; // rest belongs to position
}
else if (token == "go") {
handleGo(ss);
break; // rest belongs to go
}
else if (token == "ucinewgame") {
search::tt.clear();
break;
}
else if (token == "stop") {
search::stop();
break;
}
else if (token == "quit") {
return;
}
else if (token == "setoption") {
options.setoption(ss);
break;
}
else if (token=="visualize") {
std::cout << pos << std::endl;
break;
}
}
}
}