-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathplayer.cpp
305 lines (288 loc) · 10 KB
/
player.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
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
#include "player.h"
#include "SGF.h"
Move SgfPlayer::genmove(const Color& ) {
/* Move ret(move);
if(move == Move::INVALID) {
spdlog::debug("LOCK human genmove");
std::unique_lock<std::mutex> lock(mut);
waitingForInput = true;
while(waitingForInput) {
cond.wait(lock);
}
if(move == Move::UNDO)
undo();
else if(node) {
//if (node->has("C")) {
// s = node->get("C")[0];
//}
bool haswb = false;
while(haswb == false) {
if(node->has("B")) {
auto a = node->get("B");
std::string c;
if(a.size() <= 0|| a[0].size() < 2) {
c = std::string("pd");
} else {
c = a[0];
}
unsigned col = static_cast<unsigned>(c.at(0)- 'a');
unsigned row = 18 - static_cast<unsigned>(c.at(1) - 'a');
spdlog::debug("B {}", c);
move = Move(Position(col, row), Color::BLACK);
colorToMove = Color::WHITE;
haswb = true;
}
if (node->has("W")) {
auto a = node->get("W");
std::string c;
if(a.size() <= 0 || a[0].size() < 2) {
c = std::string("pd");
} else {
c = a[0];
}
unsigned col = static_cast<unsigned>(c.at(0) - 'a');
unsigned row = 18 - static_cast<unsigned>(c.at(1) - 'a');
spdlog::debug("W {}", c);
move = Move(Position(col, row), Color::WHITE);
colorToMove = Color::BLACK;
haswb = true;
}
if(haswb) {
history.push_back(node);
vhistory.push_back(variation);
nhistory.push_back(ni);
chistory.push_back(colorToMove);
}
nextMove();
}
} else {
move = Move(Move::PASS, colorToMove);
colorToMove = Color::other(colorToMove);
}
ret = move;
}
move = Move();
return ret;*/
return Move();
}
bool SgfPlayer::undo() {
node = history.back();
variation = vhistory.back();
ni = nhistory.back();
vhistory.pop_back();
history.pop_back();
nhistory.pop_back();
colorToMove = chistory.back();
chistory.pop_back();
return true;
}
void SgfPlayer::nextMove() {
/* auto& nodes = variation->nodes();
ni += 1;
if(ni < nodes.size()) {
node = &nodes[ni];
}
else {
int varc = variation->variations().size();
ni = 0;
if(varc > 0) {
variation = &variation->variations()[0];
auto& nodes = variation->nodes();
node = &nodes[ni];
} else {
node = 0;
spdlog::debug("PREMATURE END");
}
}
*/
}
void SgfPlayer::suggestMove(const Move& move) {
if(move == Move::UNDO)
this->move = move;
else
this->move = Move();
{
spdlog::debug("LOCK suggest move [{}]", move.toString());
std::lock_guard<std::mutex> lock(mut);
waitingForInput = false;
}
cond.notify_one();
}
void SgfPlayer::parseSgf(const std::string& fname) {
/*
spdlog::debug("Before parsing SGF");
auto problems(sgf::parse(fname));
spdlog::debug("Parsed SGF");
for(std::size_t i = 0; i < problems.size(); ++i) {
sgf.push_back(std::shared_ptr<sgf::GameTree>(new sgf::GameTree(problems[i])));
}
spdlog::debug("sgf.size() = {}", sgf.size());
variation = sgf.at(0).get();
auto& nodes = variation->nodes();
if(nodes.size() > 0) {
node = &nodes[ni];
}
if(node->has("PW")){
std::stringstream ss;
ss << "sgf: " << node->get("PW")[0];
names[1] = ss.str();
}
if(node->has("PB")){
std::stringstream ss;
ss << "sgf: " << node->get("PB")[0];
names[0] = ss.str();
}
*/
}
bool SgfPlayer::clear() {
/*if(history.size() > 0) {
node = history.front();
variation = vhistory.front();
ni = nhistory.front();
vhistory.clear();
history.clear();
nhistory.clear();
colorToMove = chistory.front();
chistory.clear();
}*/
return true;
}
std::string& SgfPlayer::getName(const Color& whose) {
return whose == Color::BLACK ? names[0] : names[1];
}
Move GtpEngine::genmove(const Color& colorToMove) {
GtpClient::CommandOutput ret(issueCommand(colorToMove == Color::BLACK ? "genmove B" : "genmove W"));
if(ret.size() < 1) {
spdlog::warn("Invalid GTP response.");
return Move(Move::INVALID, colorToMove);
}
spdlog::debug("Parsing move string [{}]", ret[0]);
return Move::parseGtp(ret[0], colorToMove);
}
const Board& GtpEngine::showboard() {
board.territoryReady = false;
board.parseGtp(GtpClient::showboard());
return board;
}
bool GtpEngine::fixed_handicap(int handicap, std::vector<Position>& stones) {
std::stringstream ssout;
ssout << "fixed_handicap " << handicap;
GtpClient::CommandOutput out = GtpClient::issueCommand(ssout.str());
if (GtpClient::success(out)) {
stones.clear();
std::stringstream ssin(out[0].substr(2));
Position pos;
while ((ssin >> pos)){
stones.push_back(pos);
}
return true;
};
return false;
}
bool GtpEngine::komi(float komi) {
std::stringstream ssout;
ssout << "komi " << komi;
return GtpClient::success(GtpClient::issueCommand(ssout.str()));
}
bool GtpEngine::play(const Move& m) {
std::stringstream ssout;
ssout << "play " << m;
return GtpClient::success(GtpClient::issueCommand(ssout.str()));
}
bool GtpEngine::boardsize(int boardSize) {
std::stringstream ssout;
ssout << "boardsize " << boardSize;
return GtpClient::success(GtpClient::issueCommand(ssout.str()));
}
bool GtpEngine::clear() {
board.clear(board.getSize());
return GtpClient::success(GtpClient::issueCommand("clear_board"));
}
bool GtpEngine::undo() {
return GtpClient::success(GtpClient::issueCommand("undo"));
}
bool GtpEngine::estimateTerritory(bool finalize, const Color& colorToMove) {
bool success = true;
if (finalize) {
spdlog::debug("initial territory");
board.clearTerritory(board.getSize());
spdlog::debug("dead");
success |= setTerritory(GtpClient::issueCommand("final_status_list dead"), board, Color::EMPTY);
spdlog::debug("white");
success |= setTerritory(GtpClient::issueCommand("final_status_list white_territory"), board, Color::WHITE);
spdlog::debug("black");
success |= setTerritory(GtpClient::issueCommand("final_status_list black_territory"), board, Color::BLACK);
}
else {
std::stringstream ss;
ss << "initial_influence " << colorToMove << " influence_regions";
GtpClient::CommandOutput ret = GtpClient::issueCommand(ss.str());
board.clearTerritory(board.getSize());
board.parseGtpInfluence(ret);
ret = GtpClient::issueCommand("dragon_status");
for (size_t i = 0; i < ret.size(); ++i) {
spdlog::debug(ret[i]);
std::stringstream ss(ret[i]);
char c;
if (i == 0) {
ss >> c;
}
Position pos;
ss >> pos;
ss >> c >> c;
if (c == 'd') {
std::ostringstream ss;
ss << "dragon_stones " << pos;
GtpClient::CommandOutput ret = GtpClient::issueCommand(ss.str());
if (GtpClient::success(ret)) {
std::istringstream ss(ret[0].substr(2));
Position p;
while ((ss >> p)){
board[p].influence = Color::other(board[p].stone);
}
}
}
}
}
board.territoryReady = success;
return success;
}
const Board& GtpEngine::showterritory(bool final, Color colorToMove) {
estimateTerritory(final, colorToMove);
board.invalidate();
return board;
}
bool GtpEngine::setTerritory(const GtpClient::CommandOutput& ret, Board& b, const Color& color) {
if(GtpClient::success(ret) && ret.at(0).length() > 2) {
std::stringstream ss;
ss << ret.front().substr(2) << "\n";
std::copy(++ret.begin(), ret.end(), std::ostream_iterator<std::string>(ss, "\n"));
std::string s;
spdlog::debug(ss.str());
Position p;
while((ss >> p)) {
if(color == Color::EMPTY)
b[p].influence = Color::other(b[p].stone);
else
b[p].influence = color;
}
return true;
}
return false;
}
void GtpEngine::setEngineName(const std::string& nameExtra) {
GtpClient::CommandOutput retName = GtpClient::name();
spdlog::debug("name");
GtpClient::CommandOutput retVersion = GtpClient::version();
spdlog::debug("version");
std::ostringstream ss;
try {
ss << retName.at(0).substr(2) << " " << retVersion.at(0).substr(2);
spdlog::debug("parsed");
}
catch( std::out_of_range&) { }
if (!nameExtra.empty())
ss << " " << nameExtra;
spdlog::debug(ss.str());
Engine::setName(ss.str());
}