-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgtp.cpp
211 lines (156 loc) · 4.25 KB
/
gtp.cpp
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
// (C) 2021-2023 by Folkert van Heusden <mail@vanheusden.com>
// Released under MIT license
#include <optional>
#include <string>
#include "color.h"
#include "gtp.h"
#include "log.h"
#include "proc.h"
#include "str.h"
GtpEngine::GtpEngine(const std::string & program, const std::string & dir, const std::string & alt_name) : program(program), name(alt_name)
{
engine = new TextProgram(program, dir);
}
GtpEngine::~GtpEngine()
{
delete engine;
}
std::optional<std::vector<std::string> > GtpEngine::getresponse(const std::optional<int> timeout_ms)
{
std::vector<std::string> out;
bool has_is = false;
for(;;) {
auto rc = engine->read(timeout_ms);
if (rc.has_value() == false) {
dolog(warning, "Failed reading from %s", name.c_str());
return { };
}
if (rc.value().size() >= 1) {
dolog(debug, "%s> %s", name.c_str(), rc.value().c_str());
if (rc.value().at(0) == '=' || has_is) {
std::string add = rc.value();
if (has_is == false) {
has_is = true;
std::size_t space = add.find(' ');
if (space != std::string::npos)
add = add.substr(space + 1);
}
out.push_back(add);
}
else if (rc.value().at(0) == '?') {
dolog(warning, "Program %s returned an error: %s", name.c_str(), rc.value().c_str());
return { };
}
}
else {
dolog(debug, "%s>---", name.c_str());
break;
}
}
if (out.empty())
return { };
return out;
}
std::optional<std::string> GtpEngine::genmove(const color_t c)
{
std::string cmd = myformat("genmove %c", c == C_WHITE ? 'w' : 'b');
dolog(debug, "%s< %s", name.c_str(), cmd.c_str());
if (engine->write(cmd)) {
auto rc = getresponse({ });
if (rc.has_value())
return rc.value().at(0);
}
return { };
}
bool GtpEngine::play(const color_t c, const std::string & vertex)
{
std::string cmd = myformat("play %c %s", c == C_WHITE ? 'w' : 'b', vertex.c_str());
dolog(debug, "%s< %s", name.c_str(), cmd.c_str());
if (engine->write(cmd))
return getresponse({ }).has_value();
return false;
}
bool GtpEngine::setkomi(const double komi)
{
std::string cmd = myformat("komi %f", komi);
dolog(debug, "%s< %s", name.c_str(), cmd.c_str());
if (engine->write(cmd))
return getresponse({ }).has_value();
return false;
}
bool GtpEngine::time_settings(const int main_time, const int byo_yomi_time, const int byo_yomi_stones)
{
std::string cmd = myformat("time_settings %d %d %d", main_time, byo_yomi_time, byo_yomi_stones);
dolog(debug, "%s< %s", name.c_str(), cmd.c_str());
if (engine->write(cmd))
return getresponse({ }).has_value();
return false;
}
bool GtpEngine::time_left(const color_t c, const int time_left_ms, const int n_stones)
{
std::string cmd = myformat("time_left %c %d %d", c == C_WHITE ? 'w' : 'b', time_left_ms / 1000, n_stones);
dolog(debug, "%s< %s", name.c_str(), cmd.c_str());
if (engine->write(cmd))
return getresponse({ }).has_value();
return false;
}
bool GtpEngine::boardsize(const int dim)
{
dolog(debug, "%s> set board size to %d", name.c_str(), dim);
if (engine->write(myformat("boardsize %d", dim)))
return getresponse({ }).has_value();
return false;
}
bool GtpEngine::clearboard()
{
if (engine->write("clear_board"))
return getresponse({ }).has_value();
return false;
}
std::optional<std::string> GtpEngine::getscore()
{
if (engine->write("final_score")) {
auto rc = getresponse({ });
if (rc.has_value())
return rc.value().at(0);
}
return { };
}
std::optional<std::string> GtpEngine::protocol_version()
{
if (engine->write("protocol_version")) {
auto rc = getresponse(30000); // 30s startup time max.
if (rc.has_value())
return rc.value().at(0);
}
return { };
}
std::string GtpEngine::getname()
{
if (name.empty() && engine->write("name")) {
auto rc = getresponse({ });
if (rc.has_value())
name = rc.value().at(0);
else
name = program;
}
dolog(info, "\"%s\" (%s) plays under PID %d", name.c_str(), program.c_str(), engine->getPid());
return name;
}
std::string GtpEngine::get_loghelper()
{
return myformat("%d", engine->getPid());
}
bool GtpEngine::has_command(const std::string & command)
{
if (engine->write("list_commands")) {
auto rc = getresponse({ });
if (rc.has_value() == false)
return false;
for(auto & line : rc.value()) {
if (line == command)
return true;
}
}
return false;
}