Esempio n. 1
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)
Esempio n. 2
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
Esempio n. 3
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]
Esempio n. 4
0
def test_rebuild_pos():
    pos = ('.........'
           '.x.......'
           '.x.xooooo'
           '..ooxxx..'
           '..ox.....'
           '..o.x...x'
           '..o...oox'
           '..o...ox.'
           '..o...o.x')
    board = board_from_string(pos)

    board.current = WHITE
    board.play(PASS)
    board.play(RESIGN)
    board._rebuild_pos()

    expected = board_from_string(pos)

    assert str(board) == str(expected)
Esempio n. 5
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
Esempio n. 6
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
Esempio n. 7
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))
Esempio n. 8
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)
Esempio n. 9
0
def test_board_from_dict():
    board = board_from_string('.........'
                              '..x......'
                              '.........'
                              '..o......'
                              '.........'
                              '.........'
                              '.........'
                              '..x....o.'
                              '.........')
    board.handicap = 1

    from_dict = board_from_dict(board.to_dict())

    assert str(from_dict) == str(board)
    assert from_dict.handicap == 1
Esempio n. 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
Esempio n. 11
0
def test_multiple_groups():
    board = board_from_string('..xo.....'
                              '..xoooo..'
                              '..xxxxooo'
                              '.....xxxx'
                              'xxxx.....'
                              'ooox.xxxx'
                              '..ox.xooo'
                              '..ox.xo..'
                              '..ox.xo..')

    score = count_score(board, 7.5)

    assert score.black == 45
    assert score.white == 36 + 7.5
    assert score.winner == BLACK
    assert score.win_by == 1.5
Esempio n. 12
0
def test_basic():
    board = board_from_string('.....xo..'
                              '.....xo..'
                              '.....xo..'
                              '.....xo..'
                              '.....xo..'
                              '.....xo..'
                              'xxxxxxo..'
                              'ooooooo..'
                              '.........')

    score = count_score(board, 7.5)

    assert score.black == 42
    assert score.white == 39 + 7.5
    assert score.winner == WHITE
    assert score.win_by == 4.5
Esempio n. 13
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))
Esempio n. 14
0
def test_handicap():
    board = board_from_string('.....xo..'
                              '.....xo..'
                              '.....xo..'
                              '.....xo..'
                              '.....xo..'
                              '.....xo..'
                              'xxxxxxo..'
                              'ooooooo..'
                              '.........')

    board.handicap = 4

    score = count_score(board, 0.5)

    assert score.black == 42
    assert score.white == 39 + 4 + 0.5
    assert score.winner == WHITE
    assert score.win_by == 1.5
Esempio n. 15
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]
Esempio n. 16
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
Esempio n. 17
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
Esempio n. 18
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
Esempio n. 19
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
Esempio n. 20
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
Esempio n. 21
0
def test_place_hc():
    tests = {
        0: ('.........'
            '.........'
            '.........'
            '.........'
            '.........'
            '.........'
            '.........'
            '.........'
            '.........'),
        1: ('.........'
            '.........'
            '.........'
            '.........'
            '.........'
            '.........'
            '.........'
            '.........'
            '.........'),
        2: ('.........'
            '.........'
            '......x..'
            '.........'
            '.........'
            '.........'
            '..x......'
            '.........'
            '.........'),
        3: ('.........'
            '.........'
            '......x..'
            '.........'
            '.........'
            '.........'
            '..x...x..'
            '.........'
            '.........'),
        4: ('.........'
            '.........'
            '..x...x..'
            '.........'
            '.........'
            '.........'
            '..x...x..'
            '.........'
            '.........'),
        5: ('.........'
            '.........'
            '..x...x..'
            '.........'
            '....x....'
            '.........'
            '..x...x..'
            '.........'
            '.........'),
        6: ('.........'
            '.........'
            '..x...x..'
            '.........'
            '..x...x..'
            '.........'
            '..x...x..'
            '.........'
            '.........'),
        7: ('.........'
            '.........'
            '..x...x..'
            '.........'
            '..x.x.x..'
            '.........'
            '..x...x..'
            '.........'
            '.........'),
        8: ('.........'
            '.........'
            '..x.x.x..'
            '.........'
            '..x...x..'
            '.........'
            '..x.x.x..'
            '.........'
            '.........'),
        9: ('.........'
            '.........'
            '..x.x.x..'
            '.........'
            '..x.x.x..'
            '.........'
            '..x.x.x..'
            '.........'
            '.........'),
    }

    for hc, pos in tests.items():
        board = Board(9)
        board.place_handicap(hc)

        assert str(board) == str(board_from_string(pos))