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

Commit

Permalink
Tidy up SGF loading code
Browse files Browse the repository at this point in the history
  • Loading branch information
tommadams committed Apr 5, 2019
1 parent f6690c6 commit f700b0c
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 43 deletions.
24 changes: 14 additions & 10 deletions cc/gtp_player.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,19 @@ void GtpClient::Ponder() {
ponder_read_count_ += player_->root()->N() - n;
}

GtpClient::Response GtpClient::ReplaySgf(
const std::vector<std::unique_ptr<sgf::Node>>& trees) {
if (!trees.empty()) {
for (const auto& move : trees[0]->ExtractMainLine()) {
if (!player_->PlayMove(move.c)) {
MG_LOG(ERROR) << "couldn't play move " << move.c;
return Response::Error("cannot load file");
}
}
}
return Response::Ok();
}

GtpClient::Response GtpClient::HandleCmd(const std::string& line) {
std::vector<absl::string_view> args =
absl::StrSplit(line, absl::ByAnyChar(" \t\r\n"), absl::SkipWhitespace());
Expand Down Expand Up @@ -379,16 +392,7 @@ GtpClient::Response GtpClient::HandleLoadsgf(CmdArgs args) {

NewGame();

if (!trees.empty()) {
for (const auto& move : trees[0]->ExtractMainLine()) {
if (!player_->PlayMove(move.c)) {
MG_LOG(ERROR) << "couldn't play move " << move.c;
return Response::Error("cannot load file");
}
}
}

return Response::Ok();
return ReplaySgf(trees);
}

GtpClient::Response GtpClient::HandleName(CmdArgs args) {
Expand Down
10 changes: 8 additions & 2 deletions cc/gtp_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -126,14 +126,20 @@ class GtpClient {
std::bind(handler, static_cast<T*>(this), std::placeholders::_1);
}

// Begin pondering again if requested.
void MaybeStartPondering();

// If waiting for the opponent to play, consider thinking for a bit.
// Returns true if we pondered.
bool MaybePonder();

virtual void Ponder();

// Begin pondering again if requested.
void MaybeStartPondering();
// Replay a loaded SGF game.
// Called by HandleLoadSgf after the SGF file has been loaded and parsed, and
// a new has been started.
virtual Response ReplaySgf(
const std::vector<std::unique_ptr<sgf::Node>>& trees);

// Handles a GTP command specified by `line`.
// Returns a (bool, string) pair containing whether the GtpPlayer should
Expand Down
26 changes: 1 addition & 25 deletions cc/minigui_player.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ MiniguiGtpClient::MiniguiGtpClient(std::unique_ptr<MctsPlayer> player,
: GtpClient(std::move(player), options) {
RegisterCmd("echo", &MiniguiGtpClient::HandleEcho);
RegisterCmd("genmove", &MiniguiGtpClient::HandleGenmove);
RegisterCmd("loadsgf", &MiniguiGtpClient::HandleLoadsgf);
RegisterCmd("play", &MiniguiGtpClient::HandlePlay);
RegisterCmd("prune_nodes", &MiniguiGtpClient::HandlePruneNodes);
RegisterCmd("report_search_interval",
Expand Down Expand Up @@ -147,26 +146,6 @@ GtpClient::Response MiniguiGtpClient::HandleGenmove(CmdArgs args) {
return response;
}

GtpClient::Response MiniguiGtpClient::HandleLoadsgf(CmdArgs args) {
auto response = CheckArgsExact(1, args);
if (!response.ok) {
return response;
}

std::string contents;
if (!file::ReadFile(std::string(args[0]), &contents)) {
return Response::Error("cannot load file");
}

std::vector<std::unique_ptr<sgf::Node>> trees;
response = ParseSgf(contents, &trees);
if (!response.ok) {
return response;
}

return ProcessSgf(trees);
}

GtpClient::Response MiniguiGtpClient::HandlePlay(CmdArgs args) {
auto response = GtpClient::HandlePlay(args);
if (response.ok) {
Expand Down Expand Up @@ -254,11 +233,8 @@ GtpClient::Response MiniguiGtpClient::HandleWinrateEvals(CmdArgs args) {
return Response::Ok();
}

GtpClient::Response MiniguiGtpClient::ProcessSgf(
GtpClient::Response MiniguiGtpClient::ReplaySgf(
const std::vector<std::unique_ptr<sgf::Node>>& trees) {
// Clear the board before replaying sgf.
NewGame();

// Traverse the SGF's game trees, loading them into the backend & running
// inference on the positions in batches.
std::function<Response(const sgf::Node&)> traverse =
Expand Down
8 changes: 2 additions & 6 deletions cc/minigui_player.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,24 +75,20 @@ class MiniguiGtpClient : public GtpClient {
std::string comment;
};

// If waiting for the opponent to play, consider thinking for a bit.
// Returns true if we pondered.
void Ponder() override;

Response HandleCmd(const std::string& line) override;
Response HandleGenmove(CmdArgs args) override;
Response HandleLoadsgf(CmdArgs args) override;
Response HandlePlay(CmdArgs args) override;
Response ReplaySgf(
const std::vector<std::unique_ptr<sgf::Node>>& trees) override;

Response HandleEcho(CmdArgs args);
Response HandlePruneNodes(CmdArgs args);
Response HandleReportSearchInterval(CmdArgs args);
Response HandleSelectPosition(CmdArgs args);
Response HandleWinrateEvals(CmdArgs args);

// Shared implementation used by HandleLoadsgf and HandlePlaysgf.
Response ProcessSgf(const std::vector<std::unique_ptr<sgf::Node>>& trees);

// Writes the search data for the tree search being performed at the given
// root to stderr. If leaf is non-null, the search path from root to leaf
// is also written.
Expand Down

0 comments on commit f700b0c

Please sign in to comment.