Example #1
0
def test_loose_chain():
    board = board_from_string('.........'
                              '.x.......'
                              '.x.xooooo'
                              '..ooxxx..'
                              '..ox.....'
                              '..o.x...x'
                              '..o...oox'
                              '..o...ox.'
                              '..o...o.x')

    coords = {
        coord2d(5, 4),
        coord2d(6, 4),
        coord2d(7, 4),
        coord2d(4, 5),
        coord2d(5, 6),
        coord2d(9, 6),
        coord2d(9, 7),
        coord2d(8, 8),
        coord2d(9, 9),
    }

    chain = board.loose_chain_at(coord2d(5, 6))

    assert chain == coords
Example #2
0
def test_game_to_sgf_part_variations():
    board = Board(19)
    board.play(coord2d(4, 4, 19))
    board.play(coord2d(16, 17, 19))
    board.current_node_id = 0
    board.play(coord2d(17, 16, 19))

    sgf = sgf_part_from_node(board, 0)

    assert sgf == ';B[dd](;W[pq])(;W[qp])'
Example #3
0
def test_chain_liberties():
    board = board_from_string('.........'
                              '.........'
                              '...oxo...'
                              '...xoo...'
                              '...xx....'
                              '....x....'
                              '...x.....'
                              '.........'
                              '.........')

    assert len(board.chain_liberties(board.chain_at(coord2d(4, 4)))) == 6
    assert len(board.chain_liberties(board.chain_at(coord2d(5, 4)))) == 4
Example #4
0
def test_from_string():
    board = board_from_string('.........'
                              '..x......'
                              '.........'
                              '.........'
                              '.........'
                              '.........'
                              '.........'
                              '.......o.'
                              '.........')

    assert board.at(coord2d(3, 2)) == BLACK
    assert board.at(coord2d(8, 8)) == WHITE
Example #5
0
def test_suicide_capture():
    board = board_from_string('.xo......'
                              'xo.......'
                              '...xo....'
                              '..x.xo...'
                              '...xo....'
                              '.........'
                              '.........'
                              '.........'
                              '.........')

    board.current = WHITE
    assert not board.is_suicide(coord2d(1, 1))
    assert not board.is_suicide(coord2d(4, 4))
Example #6
0
def test_game_to_sgf_basic():
    game = Game(board=Board(19),
                black_display='black',
                white_display='white',
                created_at=datetime(2016, 5, 7),
                result='B+1.5',
                komi=7.5)

    game.board.play(coord2d(4, 4, 19))
    game.board.play(coord2d(16, 17, 19))

    sgf = game_to_sgf(game)
    expected = '(;SO[weiqi.gs]FF[4]DT[2016-05-07]PW[white]PB[black]KM[7.5]SZ[19]RE[B+1.5];B[dd];W[pq])'

    assert sgf.replace('\n', '') == expected
Example #7
0
def test_mark_dead():
    board = board_from_string('..xo.....'
                              '..xoooo..'
                              '..xxxxooo'
                              '.....xxxx'
                              'xxxx.....'
                              'ooox.xxxx'
                              '..ox.xooo'
                              '..ox.xo..'
                              '..ox.xo.o')

    board.mark_dead(coord2d(7, 7))
    chain = board.loose_chain_at(coord2d(7, 7))

    for coord in range(board.length):
        assert board.is_marked_dead(coord) == (coord in chain)
Example #8
0
def test_play_captures():
    board = board_from_string('.........'
                              '...xxx...'
                              '....oox..'
                              '..xoox...'
                              '..xox....'
                              '...x.....'
                              '.........'
                              '.........'
                              '.........')

    board.current = BLACK
    board.play(coord2d(4, 3))

    expected = board_from_string('.........'
                                 '...xxx...'
                                 '...x..x..'
                                 '..x..x...'
                                 '..x.x....'
                                 '...x.....'
                                 '.........'
                                 '.........'
                                 '.........')

    assert str(board) == str(expected)
Example #9
0
def test_game_to_sgf_part_handicap():
    board = Board(19, 2)
    board.play(coord2d(10, 10, 19))

    sgf = sgf_part_from_node(board, 0)

    assert (sgf == ';AB[dp][pd];W[jj]' or sgf == ';AB[pd][dp];W[jj]')
Example #10
0
def test_toggle_edit_capture():
    board = board_from_string('.........'
                              '.........'
                              '..ooo....'
                              '..oxo....'
                              '..oxo....'
                              '..xxx....'
                              '.........'
                              '.........'
                              '.........')

    upper = coord2d(4, 4)
    lower = coord2d(4, 5)

    board.toggle_edit(lower, WHITE)
    assert board.at(upper) == EMPTY
    assert board.at(lower) == WHITE
Example #11
0
def test_ko_rule():
    board = board_from_string('.........'
                              '.........'
                              '...o.....'
                              '..oxo....'
                              '..x.x....'
                              '...x.....'
                              '.........'
                              '.........'
                              '.........')

    board.current = WHITE
    board.play(coord2d(4, 5))

    assert board.ko == coord2d(4, 4)

    with pytest.raises(IllegalMoveError):
        board.play(coord2d(4, 4))
Example #12
0
def test_game_to_sgf_part_labels():
    board = Board(19)
    board.play(coord2d(10, 10, 19))
    board.current_node.toggle_label(180)
    board.current_node.toggle_label(181)

    sgf = sgf_part_from_node(board, 0)

    assert (sgf == ';B[jj]LB[jj:A][kj:B]' or sgf == ';B[jj]LB[kj:B][jj:A]')
Example #13
0
def test_edit_cycle_capture():
    pos = ('.........'
           '.........'
           '..ooo....'
           '..oxo....'
           '..oxo....'
           '..xxx....'
           '.........'
           '.........'
           '.........')

    board = board_from_string(pos)
    center = coord2d(4, 4)
    lower = coord2d(4, 5)

    board.edit_cycle(lower)

    assert board.at(lower) == WHITE
    assert board.at(center) == EMPTY
Example #14
0
def test_game_to_sgf_part_symbols():
    board = Board(19)
    board.play(coord2d(10, 10, 19))
    board.current_node.toggle_symbol(180, 'TR')
    board.current_node.toggle_symbol(181, 'TR')
    board.current_node.toggle_symbol(182, 'SQ')

    sgf = sgf_part_from_node(board, 0)

    assert (sgf == ';B[jj]TR[jj][kj]SQ[lj]' or sgf == ';B[jj]TR[kj][jj]SQ[lj]')
Example #15
0
def test_marked_dead():
    board = board_from_string('.ox......'
                              '.oxxx....'
                              '.oooxxxxx'
                              'xxxoooooo'
                              '.x....oxx'
                              'xooooox..'
                              'xoooxxxx.'
                              '.oxxxooox'
                              '.ox.xo.o.')

    board.mark_dead(coord2d(1, 4))
    board.mark_dead(coord2d(8, 9))

    score = count_score(board, 7.5)

    assert score.black == 43
    assert score.white == 38 + 7.5
    assert score.winner == WHITE
    assert score.win_by == 2.5
Example #16
0
def test_play_node():
    board = Board()

    assert board.current_node_id is None

    board.play(coord2d(4, 4))
    assert board.current_node_id == 0
    assert board.current_node.parent_id is None
    assert board.current_node.children == []
    assert board.current_node.action == NODE_BLACK
    assert board.current_node.move == coord2d(4, 4)

    board.play(coord2d(5, 5))
    assert board.current_node.id == 1
    assert board.current_node.parent_id == 0
    assert board.current_node.children == []
    assert board.tree[0].children == [1]
    assert board.current_node.action == NODE_WHITE
    assert board.current_node.move == coord2d(5, 5)

    board.play(coord2d(6, 6))
    assert board.current_node_id == 2
    assert board.current_node.parent_id == 1
    assert board.current_node.children == []
    assert board.tree[0].children == [1]
    assert board.tree[1].children == [2]
    assert board.current_node.action == NODE_BLACK
    assert board.current_node.move == coord2d(6, 6)
Example #17
0
def test_edit_cycle_suicide():
    board = board_from_string('.........'
                              '.........'
                              '..ooo....'
                              '..o.o....'
                              '..ooo....'
                              '.........'
                              '..xxx....'
                              '..x.x....'
                              '..xxx....')

    upper = coord2d(4, 4)
    lower = coord2d(4, 8)

    board.edit_cycle(upper)
    board.edit_cycle(lower)
    assert board.at(upper) == WHITE
    assert board.at(lower) == BLACK

    board.edit_cycle(upper)
    board.edit_cycle(lower)
    assert board.at(upper) == EMPTY
    assert board.at(lower) == EMPTY
Example #18
0
def test_play():
    board = Board()

    assert board.current == BLACK
    board.play(coord2d(4, 4))

    assert board.current == WHITE
    board.play(coord2d(3, 3))

    assert board.current == BLACK
    board.play(coord2d(6, 6))

    assert board.at(coord2d(4, 4)) == BLACK
    assert board.at(coord2d(3, 3)) == WHITE
    assert board.at(coord2d(6, 6)) == BLACK
Example #19
0
def test_snapback_not_ko():
    board = board_from_string('.........'
                              '.........'
                              '.........'
                              '.........'
                              '.........'
                              '.........'
                              'ooo......'
                              'xxo.x....'
                              '..xx.....')

    board.current = WHITE
    board.play(coord2d(2, 9))
    board.play(coord2d(1, 9))
    board.play(coord2d(2, 9))

    assert board.at(coord2d(1, 8)) == EMPTY
    assert board.at(coord2d(2, 8)) == EMPTY
    assert board.at(coord2d(2, 9)) == WHITE
    assert board.at(coord2d(1, 9)) == EMPTY
Example #20
0
def test_can_reach_only():
    board = board_from_string('.....x.o.'
                              '.....x.o.'
                              '.....x.o.'
                              '.....xo..'
                              '.....xo..'
                              '....xxo..'
                              'xxxx..o..'
                              'ooooo.o..'
                              '.....o...')

    assert can_reach_only(board, coord2d(1, 1), BLACK)[0]
    assert not can_reach_only(board, coord2d(1, 1), WHITE)[0]

    assert can_reach_only(board, coord2d(9, 9), WHITE)[0]
    assert not can_reach_only(board, coord2d(9, 9), BLACK)[0]

    assert not can_reach_only(board, coord2d(6, 7), BLACK)[0]
    assert not can_reach_only(board, coord2d(6, 7), WHITE)[0]
Example #21
0
def test_neutral_points():
    board = board_from_string('.....x.o.'
                              '.....x.o.'
                              '.....x.o.'
                              '.....xo..'
                              '.....xo..'
                              '....xxo..'
                              'xxxx..o..'
                              'ooooo.o..'
                              '.....o...')

    score = count_score(board, 7.5)

    assert score.black == 40
    assert score.white == 35 + 7.5
    assert score.winner == WHITE
    assert score.win_by == 2.5
    assert score.points[coord2d(7, 1)] == EMPTY
    assert score.points[coord2d(7, 2)] == EMPTY
    assert score.points[coord2d(7, 3)] == EMPTY
    assert score.points[coord2d(5, 7)] == EMPTY
    assert score.points[coord2d(6, 7)] == EMPTY
    assert score.points[coord2d(6, 8)] == EMPTY
Example #22
0
def test_coord_from_sgf():
    assert coord_from_sgf("Aa", 19) == coord2d(1, 1, 19)
    assert coord_from_sgf("Ca", 19) == coord2d(3, 1, 19)
    assert coord_from_sgf("dD", 19) == coord2d(4, 4, 19)
    assert coord_from_sgf("Dp", 19) == coord2d(4, 16, 19)
    assert coord_from_sgf("sS", 19) == coord2d(19, 19, 19)
Example #23
0
def test_coord_to_sgf():
    assert coord_to_sgf(coord2d(1, 1, 19), 19) == 'aa'
    assert coord_to_sgf(coord2d(3, 1, 19), 19) == 'ca'
    assert coord_to_sgf(coord2d(4, 4, 19), 19) == 'dd'
    assert coord_to_sgf(coord2d(4, 16, 19), 19) == 'dp'
    assert coord_to_sgf(coord2d(19, 19, 19), 19) == 'ss'
Example #24
0
def test_chain():
    board = board_from_string('.........'
                              '.........'
                              '...oxo...'
                              '...xoo...'
                              '...xx....'
                              '....x....'
                              '...x.....'
                              '.........'
                              '.........')

    chains = [
        [
            coord2d(4, 4),
            {coord2d(4, 4),
             coord2d(4, 5),
             coord2d(5, 5),
             coord2d(5, 6)}
        ],
        [coord2d(4, 3), {coord2d(4, 3)}],
        [coord2d(5, 3), {coord2d(5, 3)}],
        [coord2d(6, 3), {coord2d(6, 3),
                         coord2d(6, 4),
                         coord2d(5, 4)}],
        [coord2d(4, 7), {coord2d(4, 7)}],
    ]

    for c in chains:
        assert board.chain_at(c[0]) == c[1]
Example #25
0
def test_coord_to_2d():
    coord = coord2d(4, 5, 9)
    x, y = coord_to_2d(coord, 9)
    assert x == 4
    assert y == 5
Example #26
0
def test_neighbors():
    assert set(neighbors(coord2d(1, 1), 9)) == {coord2d(2, 1), coord2d(1, 2)}
    assert set(neighbors(coord2d(9, 9), 9)) == {coord2d(9, 8), coord2d(8, 9)}
    assert set(neighbors(coord2d(5, 5), 9)) == {
        coord2d(5, 4),
        coord2d(5, 6),
        coord2d(4, 5),
        coord2d(6, 5)
    }