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

Commit

Permalink
Merge branch 'master' of https://github.com/mtyka/minigo-1
Browse files Browse the repository at this point in the history
  • Loading branch information
Mike Tyka committed Apr 8, 2019
2 parents b0d99d2 + d487515 commit c1c88a9
Show file tree
Hide file tree
Showing 6 changed files with 45 additions and 63 deletions.
18 changes: 9 additions & 9 deletions cc/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -155,9 +155,9 @@ minigo_cc_library(
)

minigo_cc_library(
name = "gtp_player",
srcs = ["gtp_player.cc"],
hdrs = ["gtp_player.h"],
name = "gtp_client",
srcs = ["gtp_client.cc"],
hdrs = ["gtp_client.h"],
deps = [
":base",
":logging",
Expand Down Expand Up @@ -205,12 +205,12 @@ minigo_cc_library(
)

minigo_cc_library(
name = "minigui_player",
srcs = ["minigui_player.cc"],
hdrs = ["minigui_player.h"],
name = "minigui_gtp_client",
srcs = ["minigui_gtp_client.cc"],
hdrs = ["minigui_gtp_client.h"],
deps = [
":base",
":gtp_player",
":gtp_client",
":logging",
":mcts",
":sgf",
Expand Down Expand Up @@ -509,9 +509,9 @@ minigo_cc_binary(
visibility = ["//visibility:public"],
deps = [
":base",
":gtp_player",
":gtp_client",
":init",
":minigui_player",
":minigui_gtp_client",
":zobrist",
"//cc/dual_net:factory",
"//cc/file",
Expand Down
4 changes: 2 additions & 2 deletions cc/gtp.cc
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@
#include "cc/constants.h"
#include "cc/dual_net/factory.h"
#include "cc/file/path.h"
#include "cc/gtp_player.h"
#include "cc/gtp_client.h"
#include "cc/init.h"
#include "cc/minigui_player.h"
#include "cc/minigui_gtp_client.h"
#include "cc/zobrist.h"
#include "gflags/gflags.h"

Expand Down
26 changes: 15 additions & 11 deletions cc/gtp_player.cc → cc/gtp_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "cc/gtp_player.h"
#include "cc/gtp_client.h"

#include <algorithm>
#include <cmath>
Expand Down 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
16 changes: 11 additions & 5 deletions cc/gtp_player.h → cc/gtp_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef CC_GTP_PLAYER_H_
#define CC_GTP_PLAYER_H_
#ifndef CC_GTP_CLIENT_H_
#define CC_GTP_CLIENT_H_

#include <deque>
#include <functional>
Expand Down 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 game 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 Expand Up @@ -194,4 +200,4 @@ class GtpClient {

} // namespace minigo

#endif // CC_GTP_PLAYER_H_
#endif // CC_GTP_CLIENT_H_
28 changes: 2 additions & 26 deletions cc/minigui_player.cc → cc/minigui_gtp_client.cc
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#include "cc/minigui_player.h"
#include "cc/minigui_gtp_client.h"

#include <algorithm>
#include <cctype>
Expand All @@ -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
16 changes: 6 additions & 10 deletions cc/minigui_player.h → cc/minigui_gtp_client.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef CC_MINIGUI_PLAYER_H_
#define CC_MINIGUI_PLAYER_H_
#ifndef CC_MINIGUI_GTP_CLIENT_H_
#define CC_MINIGUI_GTP_CLIENT_H_

#include <deque>
#include <map>
Expand All @@ -29,7 +29,7 @@
#include "absl/types/span.h"
#include "cc/color.h"
#include "cc/dual_net/dual_net.h"
#include "cc/gtp_player.h"
#include "cc/gtp_client.h"
#include "cc/thread_safe_queue.h"

namespace minigo {
Expand Down 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 Expand Up @@ -137,4 +133,4 @@ class MiniguiGtpClient : public GtpClient {

} // namespace minigo

#endif // CC_MINIGUI_PLAYER_H_
#endif // CC_MINIGUI_GTP_CLIENT_H_

0 comments on commit c1c88a9

Please sign in to comment.