Skip to content

Commit ce1f7da

Browse files
committed
Support GTP command id.
1 parent 3f4109d commit ce1f7da

File tree

2 files changed

+52
-28
lines changed

2 files changed

+52
-28
lines changed

README.md

+3-1
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,11 @@
44

55
# 編譯
66

7+
需要支援 C++11 或以上的編譯器
8+
79
git clone https://github.com/CGLemon/GoComponent
810
cd GoComponent
9-
g++ -o bot src/*.cc
11+
g++ src/*.cc -o bot -std=c++11
1012

1113
# 測試
1214

src/gtp.cc

+49-27
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99
#include "game_state.h"
1010
#include "board.h"
1111

12+
static int command_id;
13+
1214
std::vector<std::string> GTP_COMMANDS_LIST = {
1315
// Part of GTP version 2 standard command
1416
"protocol_version",
@@ -58,7 +60,7 @@ void gtp_hint();
5860
void gtp_loop(bool hint) {
5961
if (hint) gtp_hint();
6062

61-
auto main_game = std::make_unique<GameState>();
63+
auto main_game = std::make_shared<GameState>();
6264
main_game->clear_board(9, 7.f);
6365

6466
for (;;) {
@@ -67,33 +69,53 @@ void gtp_loop(bool hint) {
6769
}
6870

6971
void gtp_prcoess(GameState *main_game) {
70-
std::string input;
71-
if (!std::getline(std::cin, input)) {
72+
std::string inputs;
73+
if (!std::getline(std::cin, inputs)) {
7274
return;
7375
}
7476

75-
std::istringstream ss{input};
77+
std::istringstream ss{inputs};
7678
std::string buf;
7779
std::vector<std::string> args;
80+
7881
while (ss >> buf) {
7982
args.emplace_back(buf);
8083
}
84+
85+
if (args.empty()) {
86+
return;
87+
}
88+
89+
// check the command id here
90+
command_id = -1;
91+
auto toke_str = args[0];
92+
bool is_digit = true;
93+
94+
for (char c : toke_str) {
95+
is_digit &= isdigit(c);
96+
}
97+
if (is_digit) {
98+
command_id = std::stoi(toke_str);
99+
args.erase(std::begin(args)); // remove command id
100+
}
101+
81102
const size_t argc = args.size();
103+
const auto main_cmd = args[0];
82104

83105
if (argc == 0) {
84106
return;
85107
}
86108

87-
if (input == "quit") {
109+
if (main_cmd == "quit") {
88110
std::cout << gtp_success(std::string{});
89111
exit(EXIT_SUCCESS);
90-
} else if (input == "protocol_version") {
112+
} else if (main_cmd == "protocol_version") {
91113
std::cout << gtp_success("2");
92-
} else if (input == "name") {
114+
} else if (main_cmd == "name") {
93115
std::cout << gtp_success("Go Bot");
94-
} else if (input == "version") {
116+
} else if (main_cmd == "version") {
95117
std::cout << gtp_success("0.1");
96-
} else if (input.find("boardsize") == 0) {
118+
} else if (main_cmd == "boardsize") {
97119
if (argc >= 2) {
98120
int bsize = std::stoi(args[1]);
99121
float komi = main_game->get_komi();
@@ -103,7 +125,7 @@ void gtp_prcoess(GameState *main_game) {
103125
} else {
104126
std::cout << gtp_fail(std::string{});
105127
}
106-
} else if (input.find("komi") == 0) {
128+
} else if (main_cmd == "komi") {
107129
if (argc >= 2) {
108130
float komi = std::stof(args[1]);
109131
main_game->set_komi(komi);
@@ -112,16 +134,16 @@ void gtp_prcoess(GameState *main_game) {
112134
} else {
113135
std::cout << gtp_fail(std::string{});
114136
}
115-
} else if (input.find("clear_board") == 0) {
137+
} else if (main_cmd == "clear_board") {
116138
int bsize = main_game->get_board_size();
117139
float komi = main_game->get_komi();
118140
main_game->clear_board(bsize, komi);
119141

120142
std::cout << gtp_success(std::string{});
121-
} else if (input.find("undo") == 0) {
143+
} else if (main_cmd == "undo") {
122144
main_game->undo_move();
123145
std::cout << gtp_success(std::string{});
124-
} else if (input.find("play") == 0) {
146+
} else if (main_cmd == "play") {
125147
int color = Board::INVLD;
126148
int vtx = Board::NULL_VERTEX;
127149

@@ -161,8 +183,8 @@ void gtp_prcoess(GameState *main_game) {
161183
} else {
162184
std::cout << gtp_fail(std::string{});
163185
}
164-
} else if (input.find("genmove") == 0) {
165-
// TODO: You should implement your move generator.
186+
} else if (main_cmd == "genmove") {
187+
// TODO: You should implement your move generator here.
166188

167189
int color = main_game->get_tomove();
168190
if (argc >= 2) {
@@ -191,10 +213,10 @@ void gtp_prcoess(GameState *main_game) {
191213
}
192214

193215
std::cout << gtp_success(out);
194-
} else if (input.find("showboard") == 0) {
216+
} else if (main_cmd == "showboard") {
195217
main_game->showboard();
196218
std::cout << gtp_success(std::string{});
197-
} else if (input.find("final_score") == 0) {
219+
} else if (main_cmd == "final_score") {
198220
float score = main_game->final_score();
199221
std::ostringstream result;
200222

@@ -206,8 +228,8 @@ void gtp_prcoess(GameState *main_game) {
206228
result << "w+" << -score;
207229
}
208230
std::cout << gtp_success(result.str());
209-
} else if (input.find("help") == 0 ||
210-
input.find("list_commands") == 0) {
231+
} else if (main_cmd == "help" ||
232+
main_cmd == "list_commands") {
211233
auto list_commands = std::ostringstream{};
212234
auto idx = size_t{0};
213235

@@ -225,28 +247,28 @@ void gtp_prcoess(GameState *main_game) {
225247

226248
std::string gtp_success(std::string response) {
227249
auto out = std::ostringstream{};
228-
auto prefix = std::string{"= "};
229-
auto suffix = std::string{"\n\n"};
230250

231-
out << prefix << response << suffix;
251+
if (command_id >= 0) {
252+
out << '=' << command_id << ' ' << response << "\n\n";
253+
} else {
254+
out << '=' << ' ' << response << "\n\n";
255+
}
232256

233257
return out.str();
234258
}
235259

236260
std::string gtp_fail(std::string response) {
237261
auto out = std::ostringstream{};
238-
auto prefix = std::string{"? "};
239-
auto suffix = std::string{"\n\n"};
240262

241-
out << prefix << response << suffix;
263+
out << "? " << response << "\n\n";
242264

243265
return out.str();
244266
}
245267

246268
void gtp_hint() {
247269
std::cerr
248-
<< "Start the main GTP loop. GTP is not for human. But you could\n"
249-
<< "still try to play the bot with it. Here are some tips.\n"
270+
<< "Start to running the main GTP loop. GTP is not for human. But you\n"
271+
<< "could still try to play the bot with it. Here are some tips.\n"
250272
<< "\n"
251273
<< "Enter \"list_commands\" to show whole supported commands.\n"
252274
<< "Enter \"showboard\" to show the current board state.\n"

0 commit comments

Comments
 (0)