Exemplo n.º 1
0
def read_game_from_sgf(sgffile):
    tree = sgf_grammar.parse(sgffile.read())
    visitor = SGFVisitor()
    sequence = visitor.visit(tree)
    root, moves = sequence[0], sequence[1:]
    board_size = int(get_prop(root, 'SZ'))
    try:
        komi = float(get_prop(root, 'KM'))
    except KeyError:
        # We don't truly support zero komi, pretend it's half point
        # komi.
        komi = 0.5
    result = get_prop(root, 'RE')
    if result.startswith('B+'):
        winner = Player.black
    elif result.startswith('W+'):
        winner = Player.white
    else:
        raise ValueError(result)
    handicap = int(get_prop(root, 'HA', 0))
    board = Board(board_size, board_size)
    first_player = Player.black
    if handicap > 0:
        handicap_stones = get_props(root, 'AB')
        for sgf_move in handicap_stones:
            move = decode_sgf_move(sgf_move, board_size)
            board.place_stone(Player.black, move.point)
            first_player = Player.white
    initial_state = GameState.from_board(board, first_player, komi)
    state = initial_state
    decoded_moves = []
    for node in moves:
        key = 'B' if state.next_player == Player.black else 'W'
        sgf_move = get_prop(node, key)
        move = decode_sgf_move(sgf_move, board_size)
        decoded_moves.append(move)
        state = state.apply_move(move)

    return GameRecord(initial_state=initial_state,
                      moves=decoded_moves,
                      winner=winner)
Exemplo n.º 2
0
    def test_easy_scoring(self):
        # Both B and W have an unconditionally alive group. One
        # unconditionally dead W stone.
        orig_board = board_from_string('''
            ..xo.
            xxxo.
            .oxo.
            xxxoo
            ..xo.
        ''')
        final_board = board_from_string('''
            ..xo.
            xxxo.
            ..xo.
            xxxoo
            ..xo.
        ''')
        game = GameState.from_board(orig_board, Player.white, komi=7.5)

        board = remove_dead_stones(game)
        self.assertEqual(final_board, board)