Skip to content
This repository was archived by the owner on Mar 11, 2021. It is now read-only.

Commit 2a4fe41

Browse files
author
Andrew Jackson
authored
add handicap support to minigui (#848)
* add handicap support to the ast tree parser. * remove unused comment variable
1 parent 82cbe92 commit 2a4fe41

File tree

2 files changed

+43
-2
lines changed

2 files changed

+43
-2
lines changed

cc/gtp_client.cc

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,11 +179,14 @@ void GtpClient::Ponder() {
179179

180180
GtpClient::Response GtpClient::ReplaySgf(
181181
const std::vector<std::unique_ptr<sgf::Node>>& trees) {
182+
182183
if (!trees.empty()) {
184+
// the SGF parser takes care of transforming an sgf into moves that the
185+
// engine is able to understand, so all we do here is just play them in.
183186
for (const auto& move : trees[0]->ExtractMainLine()) {
184187
if (!player_->PlayMove(move.c)) {
185-
MG_LOG(ERROR) << "couldn't play move " << move.c;
186-
return Response::Error("cannot load file");
188+
MG_LOG(ERROR) << "Couldn't play move " << move.c;
189+
return Response::Error("Cannot load file");
187190
}
188191
}
189192
}

cc/sgf.cc

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ bool GetTreeImpl(const Ast::Tree& tree,
210210
std::vector<std::unique_ptr<Node>>* dst) {
211211
const auto* src = &tree;
212212
const Ast::Property* prop;
213+
const Ast::Property* stones;
213214

214215
// Extract all the nodes out of this tree.
215216
for (const auto& node : src->nodes) {
@@ -219,6 +220,43 @@ bool GetTreeImpl(const Ast::Tree& tree,
219220
move.color = Color::kBlack;
220221
} else if ((prop = node.FindProperty("W")) != nullptr) {
221222
move.color = Color::kWhite;
223+
} else if ((prop = node.FindProperty("HA")) != nullptr) {
224+
int handicap;
225+
bool valid_handicap = absl::SimpleAtoi(prop->values[0], &handicap);
226+
if (!valid_handicap) {
227+
MG_LOG(ERROR) << "Invalid handicap property: " << prop->values[0];
228+
break;
229+
}
230+
231+
if ((handicap <= 1) || (handicap > 9)) {
232+
MG_LOG(ERROR) << "Invalid handicap value:" << handicap;
233+
return false;
234+
}
235+
stones = node.FindProperty("AB");
236+
if (stones == nullptr) {
237+
MG_LOG(ERROR) << "Handicap stones not specified.";
238+
return false;
239+
}
240+
241+
for (const auto& h_location : stones->values) {
242+
Move m;
243+
m.color = Color::kBlack;
244+
m.c = Coord::FromSgf(h_location, true);
245+
if (m.c == Coord::kInvalid) {
246+
MG_LOG(ERROR) << "Can't parse node " << node.ToString() << ": \""
247+
<< h_location << "\" isn't a valid SGF coord for a handicap stone";
248+
return false;
249+
}
250+
dst->push_back(absl::make_unique<Node>(m, ""));
251+
dst = &(dst->back()->children);
252+
if (h_location != stones->values.back()) {
253+
m.color = Color::kWhite;
254+
m.c = Coord::kPass;
255+
dst->push_back(absl::make_unique<Node>(m, ""));
256+
dst = &(dst->back()->children);
257+
}
258+
}
259+
continue;
222260
} else {
223261
continue;
224262
}

0 commit comments

Comments
 (0)