def cmd_loadsgf(self, arguments): args = arguments.split() if len(args) == 2: file_, movenum = args movenum = int(movenum) print('movenum =', movenum, file=sys.stderr) else: file_ = args[0] movenum = None try: with open(file_, 'r') as f: contents = f.read() except: raise ValueError('Unreadable file: ' + file_) try: # This is kinda bad, because replay_sgf is already calling # 'play move' on its internal position objects, but we really # want to advance the engine along with us rather than try to # push in some finished Position object. for idx, p in enumerate(sgf_wrapper.replay_sgf(contents)): print('playing #', idx, p.next_move, file=sys.stderr) self._game.play_move(p.next_move) if movenum and idx == movenum: break except: raise
def test_make_sgf(self): all_pwcs = list(replay_sgf(utils_test.BOARD_SIZE, NO_HANDICAP_SGF)) second_last_position, last_move, _ = all_pwcs[-1] last_position = second_last_position.play_move(last_move) back_to_sgf = make_sgf( utils_test.BOARD_SIZE, last_position.recent, last_position.score(), komi=last_position.komi, ) reconstructed_positions = list(replay_sgf( utils_test.BOARD_SIZE, back_to_sgf)) second_last_position2, last_move2, _ = reconstructed_positions[-1] last_position2 = second_last_position2.play_move(last_move2) self.assertEqualPositions(last_position, last_position2)
def test_chinese_handicap_handling(self): intermediate_board = utils_test.load_board(''' ......... ......... ......X.. ......... ......... ......... ......... ......... ......... ''') intermediate_position = go.Position( utils_test.BOARD_SIZE, intermediate_board, n=1, komi=5.5, caps=(0, 0), recent=(go.PlayerMove(go.BLACK, coords.from_kgs( utils_test.BOARD_SIZE, 'G7')),), to_play=go.BLACK, ) final_board = utils_test.load_board(''' ....OX... .O.OOX... O.O.X.X.. .OXXX.... OX...XX.. .X.XXO... X.XOOXXX. XXXO.OOX. .XOOX.O.. ''') final_position = go.Position( utils_test.BOARD_SIZE, final_board, n=50, komi=5.5, caps=(7, 2), ko=None, recent=( go.PlayerMove( go.WHITE, coords.from_kgs(utils_test.BOARD_SIZE, 'E9')), go.PlayerMove( go.BLACK, coords.from_kgs(utils_test.BOARD_SIZE, 'F9')),), to_play=go.WHITE ) positions_w_context = list(replay_sgf( utils_test.BOARD_SIZE, CHINESE_HANDICAP_SGF)) self.assertEqualPositions( intermediate_position, positions_w_context[1].position) self.assertEqual( positions_w_context[1].next_move, coords.from_kgs( utils_test.BOARD_SIZE, 'C3')) final_replayed_position = positions_w_context[-1].position.play_move( positions_w_context[-1].next_move) self.assertEqualPositions(final_position, final_replayed_position)
def test_japanese_handicap_handling(self): intermediate_board = utils_test.load_board(''' ......... ......... ......X.. ......... ....O.... ......... ..X...... ......... ......... ''') intermediate_position = go.Position( utils_test.BOARD_SIZE, intermediate_board, n=1, komi=5.5, caps=(0, 0), recent=(go.PlayerMove(go.WHITE, coords.from_kgs( utils_test.BOARD_SIZE, 'E5')),), to_play=go.BLACK, ) final_board = utils_test.load_board(''' ......... ......... ......X.. ......... ....O.... ......... ..XX..... ......... ......... ''') final_position = go.Position( utils_test.BOARD_SIZE, final_board, n=2, komi=5.5, caps=(0, 0), recent=( go.PlayerMove(go.WHITE, coords.from_kgs( utils_test.BOARD_SIZE, 'E5')), go.PlayerMove(go.BLACK, coords.from_kgs( utils_test.BOARD_SIZE, 'D3')),), to_play=go.WHITE, ) positions_w_context = list(replay_sgf( utils_test.BOARD_SIZE, JAPANESE_HANDICAP_SGF)) self.assertEqualPositions( intermediate_position, positions_w_context[1].position) final_replayed_position = positions_w_context[-1].position.play_move( positions_w_context[-1].next_move) self.assertEqualPositions(final_position, final_replayed_position)
def test_replay_position(self): sgf_positions = list(sgf_wrapper.replay_sgf( utils_test.BOARD_SIZE, NO_HANDICAP_SGF)) initial = sgf_positions[0] self.assertEqual(initial.result, go.WHITE) final = sgf_positions[-1].position.play_move( sgf_positions[-1].next_move) # sanity check to ensure we're working with the right position final_board = utils_test.load_board(''' .OXX..... O.OX.X... .OOX..... OOOOXXXXX XOXXOXOOO XOOXOO.O. XOXXXOOXO XXX.XOXXO X..XOO.O. ''') expected_final_position = go.Position( utils_test.BOARD_SIZE, final_board, n=62, komi=6.5, caps=(3, 2), ko=None, recent=tuple(), to_play=go.BLACK ) self.assertEqualPositions(expected_final_position, final) self.assertEqual(final.n, len(final.recent)) replayed_positions = list(go.replay_position( utils_test.BOARD_SIZE, final, 1)) for sgf_pos, replay_pos in zip(sgf_positions, replayed_positions): self.assertEqualPositions(sgf_pos.position, replay_pos.position)
def test_sgf_props(self): sgf_replayer = replay_sgf(utils_test.BOARD_SIZE, CHINESE_HANDICAP_SGF) initial = next(sgf_replayer) self.assertEqual(initial.result, go.BLACK) self.assertEqual(initial.position.komi, 5.5)