示例#1
0
def replay_sgf(sgf_contents):
    '''
    Wrapper for sgf files, exposing contents as position_w_context instances
    with open(filename) as f:
        for position_w_context in replay_sgf(f.read()):
            print(position_w_context.position)
    '''
    collection = sgf.parse(sgf_contents)
    game = collection.children[0]
    props = game.root.properties
    assert int(sgf_prop(props.get('GM', ['1']))) == 1, "Not a Go SGF!"

    komi = 0
    if props.get('KM') != None:
        komi = float(sgf_prop(props.get('KM')))
    metadata = GameMetadata(result=sgf_prop(props.get('RE')),
                            handicap=int(sgf_prop(props.get('HA', [0]))),
                            board_size=int(sgf_prop(props.get('SZ', [19]))))
    go.set_board_size(metadata.board_size)

    pos = Position(komi=komi)
    current_node = game.root
    while pos is not None and current_node is not None:
        pos = handle_node(pos, current_node)
        maybe_correct_next(pos, current_node.next)
        next_move = get_next_move(current_node)
        yield PositionWithContext(pos, next_move, metadata)
        current_node = current_node.next
示例#2
0
def replay_position(position, extract_move_probs=False):
    '''
    Wrapper for a go.Position which replays its history.
    Assumes an empty start position! (i.e. no handicap, and history must be exhaustive.)

    for position_w_context in replay_position(position):
        print(position_w_context.position)
    '''
    assert (position.n == len(position.recent) and (position.n == len(position.recent_move_prob))),\
        "Position history is incomplete"
    metadata = GameMetadata(result=position.result(),
                            handicap=0,
                            board_size=position.board.shape[0])
    go.set_board_size(metadata.board_size)

    pos = Position(komi=position.komi)
    for player_move, move_prob in zip(position.recent,
                                      position.recent_move_prob):
        color, next_move = player_move
        try:
            tmp = pos.play_move(next_move, color=color)
            if extract_move_probs:
                yield PositionWithContext(pos, move_prob, metadata)
            else:
                yield PositionWithContext(pos, next_move, metadata)
            pos = tmp
        except:
            break
    '''
示例#3
0
def replay_position(position):
    '''
    Wrapper for a go.Position which replays its history.
    Assumes an empty start position! (i.e. no handicap, and history must be exhaustive.)

    for position_w_context in replay_position(position):
        print(position_w_context.position)
    '''
    assert position.n == len(position.recent), "Position history is incomplete"
    metadata = GameMetadata(result=position.result(),
                            handicap=0,
                            board_size=position.board.shape[0])
    go.set_board_size(metadata.board_size)

    pos = Position(komi=position.komi)
    for player_move in position.recent:
        color, next_move = player_move
        yield PositionWithContext(pos, next_move, metadata)
        pos = pos.play_move(next_move, color=color)
    # return the original position, with unknown next move
    yield PositionWithContext(pos, None, metadata)
 def set_size(self, n):
     self.size = n
     go.set_board_size(n)
     self.clear()