-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgtp.py
More file actions
385 lines (299 loc) · 13.5 KB
/
Copy pathgtp.py
File metadata and controls
385 lines (299 loc) · 13.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
"""GTP (Go Text Protocol) に準拠したインターフェース.
GoGui(https://github.com/Remi-Coulom/gogui?tab=readme-ov-file)との通信で用いる.
"""
import datetime
import gomoku
from gomoku import Position, IntersectionState
from engine import Engine
from error import GTPError
class GTP:
VERSION = "2.0"
def __init__(self, engine: Engine):
self.__engine = engine
self.__commands = {}
self.__quit_flag = False
self.__log_write = lambda x: None
self.__log_write_line = lambda x: self.__log_write(f"{x}\n")
self.__init_commands()
def __init_commands(self):
self.__commands = {
"protocol_version": self.__exec_protocol_version_command,
"name": self.__exec_name_command,
"version": self.__exec_version_command,
"known_command": self.__exec_know_command_command,
"list_commands": self.__exec_list_commands_command,
"quit": self.__exec_quit_command,
"boardsize": self.__exec_board_size_command,
"clear_board": self.__exec_clear_board_command,
"komi": self.__exec_komi_command,
"play": self.__exec_play_command,
"genmove": self.__exec_gen_move_command,
"reg_genmove": self.__exec_reg_gen_move_command,
"undo": self.__exec_undo_command,
"showboard": self.__exec_showboard_command,
"time_settings": self.__exec_time_settings_command,
"time_left": self.__exec_time_left_command,
"loadsgf": self.load_sgf_command,
"color": self.__exec_color_command,
# gogui-rule commands
"gogui-rules_game_id": self.__exec_rules_game_id_command,
"gogui-rules_board": self.__exec_showboard_command,
"gogui-rules_board_size": self.__exec_rules_board_size_command,
"gogui-rules_legal_moves": self.__exec_rules_legal_moves_command,
"gogui-rules_side_to_move": self.__exec_rules_side_to_move_command,
"gogui-rules_final_result": self.__exec_rules_final_result_command,
}
def mainloop(self, log_file_path: str = None):
"""コマンド受付のメインループ.
Args:
log_file_path (str, optional): ログファイルのパス. デフォルトはNoneで、ログは出力しない.
"""
logger = None
if log_file_path:
logger = open(log_file_path, "w", encoding="utf-8")
def write(s: str):
logger.write(s)
logger.flush()
self.__log_write = write
while not self.__quit_flag:
cin = input()
self.__log_write_line(f"[{datetime.datetime.now()}] Input: {cin}")
cmd_name, args, id = self.__parse_command(cin)
try:
if self.__commands.get(cmd_name, None):
self.__commands[cmd_name](id, args)
else:
if cmd_name in self.__engine.get_original_commands():
self.success(id, self.__engine.exec_original_command(cmd_name, args))
else:
self.failure(id, f"unknown command: {cmd_name}")
except GTPError as e:
self.failure(id, e.message)
if log_file_path:
logger.close()
self.__quit_flag = False
@staticmethod
def __parse_command(cmd: str) -> tuple[str, list[str], int | None]:
"""コマンドを分解して、(コマンド名, 引数リスト, ID)のタプルを返す
"""
splitted = cmd.lower().split()
has_id = cmd.isdigit()
offset = 1 if has_id else 0
args = []
for i in range(offset + 1, len(splitted)):
args.append(splitted[i])
return (splitted[offset], args, int(splitted[0]) if has_id else None)
def success(self, id: int | None, msg: str):
id_str = str(id) if id is not None else ""
output = f"={id_str} {msg}\n"
self.__log_write_line(f"[{datetime.datetime.now()}] Status: Success Output: {output}")
print(output, end="\n\n", flush=True)
def failure(self, id: int | None, msg: str):
id_str = str(id) if id is not None else ""
output = f"{id_str} {msg}\n"
self.__log_write_line(f"[{datetime.datetime.now()}] Status: Failure Output: {output}")
print(f"?{output}", end="\n\n", flush=True)
def __exec_protocol_version_command(self, id: int | None, args: list[str]):
self.success(id, self.VERSION)
def __exec_name_command(self, id: int | None, args: list[str]):
self.success(id, self.__engine.name)
def __exec_version_command(self, id: int | None, args: list[str]):
self.success(id, self.__engine.version)
def __exec_know_command_command(self, id: int | None, args: list[str]):
"""指定されたコマンドが存在するかどうかを確認するコマンド"""
if len(args) == 0:
self.failure(id, "invalid option")
return
self.success(id, "true" if args[0] in self.__commands else "false")
def __exec_list_commands_command(self, id: int | None, args: list[str]):
"""サポートされているコマンドのリストを返す"""
commands = self.__commands.keys()
self.success(id, "\n".join(commands))
def __exec_quit_command(self, id: int | None, args: list[str]):
self.__engine.quit()
self.__quit_flag = True
self.success(id, "")
def __exec_board_size_command(self, id: int | None, args: list[str]):
"""盤面のサイズを設定するするコマンド"""
if len(args) == 0:
self.failure(id, "invalid option")
return
if not args[0].isdigit():
self.failure(id, "board size must be a positive integer")
return
if self.__engine.set_board_size(int(args[0])):
self.success(id, "")
else:
self.failure(id, "unacceptable size")
def __exec_clear_board_command(self, id: int | None, args: list[str]):
"""盤面をクリアするコマンド"""
self.__engine.clear_board()
self.success(id, "")
def __exec_komi_command(self, id: int | None, args: list[str]):
"""
コミを設定するコマンド
コミは囲碁特有のルールであるため,特に何もしない
"""
if len(args) == 0:
self.failure(id, "invalid option")
return
self.success(id, "")
def __exec_play_command(self, id: int | None, args: list[str]):
"""指定された座標に石を打つコマンド"""
if len(args) < 2:
self.failure(id, "invalid option")
return
color = self.__parse_color(args[0])
if color == IntersectionState.EMPTY:
self.failure(id, "invalid color")
return
coord = self.__parse_coord(args[1])
if coord == -1:
self.failure(id, "invalid coordinate")
return
if not self.__engine.play(color, coord):
self.failure(id, "invalid move")
return
self.success(id, "")
def __exec_gen_move_command(self, id: int | None, args: list[str]):
"""次の手を生成して着手するコマンド"""
if len(args) == 0:
self.failure(id, "invalid option")
return
color = self.__parse_color(args[0])
if color != self.__engine.get_side_to_move():
self.failure(id, "invalid color")
return
coord = self.__engine.gen_move()
self.__engine.play(color, coord)
self.success(id, self.__coord_to_str(coord))
def __exec_reg_gen_move_command(self, id: int | None, args: list[str]):
"""次の手を生成するが着手はしないコマンド"""
if len(args) == 0:
self.failure(id, "invalid option")
return
color = self.__parse_color(args[0])
if color == IntersectionState.EMPTY:
self.failure(id, "invalid color")
return
coord = self.__engine.gen_move(color)
self.success(id, self.__coord_to_str(coord))
def __exec_undo_command(self, id: int | None, args: list[str]):
"""直前の着手を取り消すコマンド"""
if self.__engine.undo():
self.success(id, "")
else:
self.failure(id, "cannot undo")
def __exec_showboard_command(self, id: int | None, args: list[str]):
"""現在の盤面を表示するコマンド"""
self.success(id, self.__engine.show_board())
def __exec_time_settings_command(self, id: int | None, args: list[str]):
"""時間設定コマンド"""
if len(args) < 3:
self.failure(id, "invalid option")
return
if not args[0].isdigit() or not args[1].isdigit() or not args[2].isdigit():
self.failure(id, "time settings must be integers")
return
self.__engine.set_time(int(args[0]), int(args[1]), int(args[2]))
self.success(id, "")
def __exec_time_left_command(self, id: int | None, args: list[str]):
"""残り時間を設定するコマンド"""
if len(args) < 3:
self.failure(id, "invalid option")
return
color = self.__parse_color(args[0])
if color == IntersectionState.EMPTY:
self.failure(id, "invalid color")
return
if not args[1].isdigit() or not args[2].isdigit():
self.failure(id, "time left must be integers")
return
self.__engine.set_time_left(color, int(args[1]), int(args[2]))
self.success(id, "")
def load_sgf_command(self, id: int | None, args: list[str]):
"""
SGFファイルを読み込むコマンド
現状は非対応
"""
self.failure(id, "SGF file is not supported")
def __exec_color_command(self, id: int | None, args: list[str]):
"""指定された座標の石の色を取得するコマンド"""
if len(args) < 1:
self.failure(id, "invalid option")
coord = self.__parse_coord(args[0])
if coord == -1:
self.failure(id, "invalid coordinate")
return
color = self.__engine.get_color_at(coord)
if color == IntersectionState.EMPTY:
self.success(id, "empty")
elif color == IntersectionState.BLACK:
self.success(id, "black")
elif color == IntersectionState.WHITE:
self.success(id, "white")
else:
assert False, "Unknown color state"
def exec_showboard_command(self, id: int | None, args: list[str]):
"""現在の盤面を表示するコマンド"""
self.success(id, self.__engine.show_board())
"""
gogui-rule commands
GoGuiにgomokuのルールを適用するためのコマンド
"""
def __exec_rules_game_id_command(self, id: int | None, args: list[str]):
self.success(id, "gomoku")
def __exec_rules_board_size_command(self, id: int | None, args: list[str]):
self.success(id, str(self.__engine.board_size))
def __exec_rules_legal_moves_command(self, id: int | None, args: list[str]):
"""現在の盤面における合法手を取得するコマンド"""
moves = []
if self.__engine._pos.winner == IntersectionState.EMPTY and self.__engine._pos.empty_count > 0:
moves = [self.__coord_to_str(coord) for coord in self.__engine.get_legal_moves()]
if(len(moves) != 0):
moves.append("pass")
self.success(id, " ".join(moves))
def __exec_rules_side_to_move_command(self, id: int | None, args: list[str]):
"""現在の手番の色を取得するコマンド"""
side = self.__engine.get_side_to_move()
if side == IntersectionState.BLACK:
self.success(id, "black")
elif side == IntersectionState.WHITE:
self.success(id, "white")
else:
assert False, "Unknown side to move state"
def __exec_rules_final_result_command(self, id: int | None, args: list[str]):
"""ゲームの最終結果を取得するコマンド"""
self.success(id, self.__engine.get_final_result())
@staticmethod
def __parse_color(color: str) -> IntersectionState:
color = color.lower()
if color == "black" or color == "b":
return IntersectionState.BLACK
elif color == "white" or color == "w":
return IntersectionState.WHITE
else:
return IntersectionState.EMPTY
def __parse_coord(self, coord: str) -> int:
coord = coord.lower()
if coord == "pass":
return gomoku.PASS_COORD
if len(coord) < 2:
return -1
x = ord(coord[0]) - ord('a')
# GoGuiは座標にiを用いないため調整が必要
if coord[0] > 'i':
x -= 1
y = int(coord[1:])
y = self.__engine.board_size - y
return x + y * self.__engine.board_size
def __coord_to_str(self, coord: int) -> str:
if coord == gomoku.PASS_COORD:
return "pass"
x = coord % self.__engine.board_size
y = coord // self.__engine.board_size
# GoGuiは座標にiを用いない.
x_chr = chr(ord('A') + x)
if x_chr >= 'I':
x_chr = chr(ord(x_chr) + 1)
return f"{x_chr}{self.__engine.board_size - y}"