9
9
#include " game_state.h"
10
10
#include " board.h"
11
11
12
+ static int command_id;
13
+
12
14
std::vector<std::string> GTP_COMMANDS_LIST = {
13
15
// Part of GTP version 2 standard command
14
16
" protocol_version" ,
@@ -58,7 +60,7 @@ void gtp_hint();
58
60
void gtp_loop (bool hint) {
59
61
if (hint) gtp_hint ();
60
62
61
- auto main_game = std::make_unique <GameState>();
63
+ auto main_game = std::make_shared <GameState>();
62
64
main_game->clear_board (9 , 7 .f );
63
65
64
66
for (;;) {
@@ -67,33 +69,53 @@ void gtp_loop(bool hint) {
67
69
}
68
70
69
71
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 )) {
72
74
return ;
73
75
}
74
76
75
- std::istringstream ss{input };
77
+ std::istringstream ss{inputs };
76
78
std::string buf;
77
79
std::vector<std::string> args;
80
+
78
81
while (ss >> buf) {
79
82
args.emplace_back (buf);
80
83
}
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
+
81
102
const size_t argc = args.size ();
103
+ const auto main_cmd = args[0 ];
82
104
83
105
if (argc == 0 ) {
84
106
return ;
85
107
}
86
108
87
- if (input == " quit" ) {
109
+ if (main_cmd == " quit" ) {
88
110
std::cout << gtp_success (std::string{});
89
111
exit (EXIT_SUCCESS);
90
- } else if (input == " protocol_version" ) {
112
+ } else if (main_cmd == " protocol_version" ) {
91
113
std::cout << gtp_success (" 2" );
92
- } else if (input == " name" ) {
114
+ } else if (main_cmd == " name" ) {
93
115
std::cout << gtp_success (" Go Bot" );
94
- } else if (input == " version" ) {
116
+ } else if (main_cmd == " version" ) {
95
117
std::cout << gtp_success (" 0.1" );
96
- } else if (input. find ( " boardsize " ) == 0 ) {
118
+ } else if (main_cmd == " boardsize " ) {
97
119
if (argc >= 2 ) {
98
120
int bsize = std::stoi (args[1 ]);
99
121
float komi = main_game->get_komi ();
@@ -103,7 +125,7 @@ void gtp_prcoess(GameState *main_game) {
103
125
} else {
104
126
std::cout << gtp_fail (std::string{});
105
127
}
106
- } else if (input. find ( " komi " ) == 0 ) {
128
+ } else if (main_cmd == " komi " ) {
107
129
if (argc >= 2 ) {
108
130
float komi = std::stof (args[1 ]);
109
131
main_game->set_komi (komi);
@@ -112,16 +134,16 @@ void gtp_prcoess(GameState *main_game) {
112
134
} else {
113
135
std::cout << gtp_fail (std::string{});
114
136
}
115
- } else if (input. find ( " clear_board " ) == 0 ) {
137
+ } else if (main_cmd == " clear_board " ) {
116
138
int bsize = main_game->get_board_size ();
117
139
float komi = main_game->get_komi ();
118
140
main_game->clear_board (bsize, komi);
119
141
120
142
std::cout << gtp_success (std::string{});
121
- } else if (input. find ( " undo " ) == 0 ) {
143
+ } else if (main_cmd == " undo " ) {
122
144
main_game->undo_move ();
123
145
std::cout << gtp_success (std::string{});
124
- } else if (input. find ( " play " ) == 0 ) {
146
+ } else if (main_cmd == " play " ) {
125
147
int color = Board::INVLD;
126
148
int vtx = Board::NULL_VERTEX;
127
149
@@ -161,8 +183,8 @@ void gtp_prcoess(GameState *main_game) {
161
183
} else {
162
184
std::cout << gtp_fail (std::string{});
163
185
}
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 .
166
188
167
189
int color = main_game->get_tomove ();
168
190
if (argc >= 2 ) {
@@ -191,10 +213,10 @@ void gtp_prcoess(GameState *main_game) {
191
213
}
192
214
193
215
std::cout << gtp_success (out);
194
- } else if (input. find ( " showboard " ) == 0 ) {
216
+ } else if (main_cmd == " showboard " ) {
195
217
main_game->showboard ();
196
218
std::cout << gtp_success (std::string{});
197
- } else if (input. find ( " final_score " ) == 0 ) {
219
+ } else if (main_cmd == " final_score " ) {
198
220
float score = main_game->final_score ();
199
221
std::ostringstream result;
200
222
@@ -206,8 +228,8 @@ void gtp_prcoess(GameState *main_game) {
206
228
result << " w+" << -score;
207
229
}
208
230
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 " ) {
211
233
auto list_commands = std::ostringstream{};
212
234
auto idx = size_t {0 };
213
235
@@ -225,28 +247,28 @@ void gtp_prcoess(GameState *main_game) {
225
247
226
248
std::string gtp_success (std::string response) {
227
249
auto out = std::ostringstream{};
228
- auto prefix = std::string{" = " };
229
- auto suffix = std::string{" \n\n " };
230
250
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
+ }
232
256
233
257
return out.str ();
234
258
}
235
259
236
260
std::string gtp_fail (std::string response) {
237
261
auto out = std::ostringstream{};
238
- auto prefix = std::string{" ? " };
239
- auto suffix = std::string{" \n\n " };
240
262
241
- out << prefix << response << suffix ;
263
+ out << " ? " << response << " \n\n " ;
242
264
243
265
return out.str ();
244
266
}
245
267
246
268
void gtp_hint () {
247
269
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 "
250
272
<< " \n "
251
273
<< " Enter \" list_commands\" to show whole supported commands.\n "
252
274
<< " Enter \" showboard\" to show the current board state.\n "
0 commit comments