Skip to content

Commit

Permalink
Add original GTP command "tamago-read_sgf"
Browse files Browse the repository at this point in the history
  • Loading branch information
kaorahi committed Jan 3, 2024
1 parent c454642 commit 841fadd
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
26 changes: 25 additions & 1 deletion gtp/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def __init__(self, board_size: int, superko: bool, model_file_path: str, \
"komi",
"showboard",
"load_sgf",
"tamago-read_sgf",
"fixed_handicap",
"gogui-analyze_commands",
"lz-analyze",
Expand Down Expand Up @@ -305,10 +306,31 @@ def _load_sgf(self, arg_list: List[str]) -> NoReturn:
sgf_data = SGFReader(arg_list[0], board_size=self.board.get_board_size())

if len(arg_list) < 2:
moves = sgf_data.get_n_moves()
moves = 9999
else:
moves = int(arg_list[1])
self._load_sgf_data(sgf_data, moves)

def _read_sgf(self, arg_list: List[str]) -> NoReturn:
"""tamago-read_sgfコマンドを処理する。
指定したSGF文字列の局面にする。
Args:
arg_list (List[str]): コマンドの引数リスト(SGF文字列を空白文字ごとに分割したもの)
"""
sgf_text = ' '.join(arg_list)
sgf_data = SGFReader(sgf_text, board_size=self.board.get_board_size(), literal=True)
self._load_sgf_data(sgf_data)

def _load_sgf_data(self, sgf_data: SGFReader, moves: int=9999) -> NoReturn:
"""SGFデータを読み込み、指定手番まで進めた局面にする。
Args:
arg_list (List[str]): コマンドの引数リスト(SGF文字列を空白文字ごとに分割したもの)
sgf_data (SGFReader): SGFデータ
moves (int): 手数(SGFデータの手数を超える値を指定した場合は最終局面となる)
"""
moves = min(moves, sgf_data.get_n_moves())
self.board.clear()

for i in range(moves):
Expand Down Expand Up @@ -498,6 +520,8 @@ def run(self) -> NoReturn: # pylint: disable=R0912,R0915
self._showboard()
elif input_gtp_command == "load_sgf":
self._load_sgf(command_list[1:])
elif input_gtp_command == "tamago-read_sgf":
self._read_sgf(command_list[1:])
elif input_gtp_command == "fixed_handicap":
self._fixed_handicap(command_list[1])
elif input_gtp_command == "final_score":
Expand Down
13 changes: 9 additions & 4 deletions sgf/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,12 +33,13 @@
class SGFReader: # pylint: disable=R0902
"""SGFファイル読み込み。
"""
def __init__(self, filename: str, board_size: int): # pylint: disable=R0912
def __init__(self, filename_or_text: str, board_size: int, literal: bool=False): # pylint: disable=R0912
"""コンストラクタ
Args:
filename (str): SGFファイルパス
filename_or_text (str): SGFファイルパスまたはSGF文字列
board_size (int): 碁盤の大きさ
literal (bool): 第一引数をSGF文字列とみなすかどうか
"""
self.board_size = board_size
self.board_size_with_ob = board_size + OB_SIZE * 2
Expand All @@ -54,8 +55,12 @@ def __init__(self, filename: str, board_size: int): # pylint: disable=R0912
self.application = None
self.copyright = None

with open(filename, mode='r', encoding='utf-8') as sgf_file:
sgf_text = sgf_file.read()
if literal:
sgf_text = filename_or_text
else:
filename = filename_or_text
with open(filename, mode='r', encoding='utf-8') as sgf_file:
sgf_text = sgf_file.read()

sgf_text = sgf_text.replace('\n', '')

Expand Down

0 comments on commit 841fadd

Please sign in to comment.