Example #1
0
def test_losing():
    """test losing states"""

    losing_state = list('XXOO.....')

    for i in range(10):
        random.shuffle(losing_state)
        assert find_winner(''.join(losing_state)) == None
Example #2
0
def test_find_winner_with_winner():
    """test find_winner with boards that have a winner"""
    wins = [
        ("PPP......"),
        ("...PPP..."),
        ("......PPP"),
        ("P..P..P.."),
        (".P..P..P."),
        ("..P..P..P"),
        ("P...P...P"),
        ("..P.P.P.."),
    ]

    for player in ["X", "O"]:
        for win in wins:
            check = find_winner(win.replace("P", player))
            assert player == check
Example #3
0
def test_winning():
    """test winning states"""

    wins = [('PPP......'), ('...PPP...'), ('......PPP'), ('P..P..P..'),
            ('.P..P..P.'), ('..P..P..P'), ('P...P...P'), ('..P.P.P..')]

    for player in 'XO':
        other_player = 'O' if player == 'X' else 'X'

        for state in wins:
            state = state.replace('P', player)
            dots = [i for i in range(len(state)) if state[i] == '.']
            mut = random.sample(dots, k=2)
            test_state = ''.join([
                other_player if i in mut else state[i]
                for i in range(len(state))
            ])
            assert find_winner(test_state) == player
Example #4
0
def test_find_winner_without_winners():
    """test find_winner with a board that doesn't have winner"""
    board = list("XX..OO..O")
    actual = find_winner(board)
    assert not actual