Esempio n. 1
0
def test_cannot_pick_slot_in_game_lost(game):
    """Test to forbid a slot pick when game is lost"""
    x, y = 5, 6
    pick = PickSlotSchema(x=x, y=y)
    game.pick_slot(pick)

    x, y = 2, 3
    pick = PickSlotSchema(x=x, y=y)
    with raises(GameIsOver):
        game.pick_slot(pick)
Esempio n. 2
0
def test_cannot_pick_slot_in_game_won(game):
    """Test to forbid a slot pick when game is won"""
    # Set all slots as picked
    for slot in game.board.iter_slots():
        slot.available = True if slot.mine else False

    # Set only one slot to be available
    x, y = 2, 3
    game.board.slots[x][y].available = True
    pick = PickSlotSchema(x=x, y=y)
    game.pick_slot(pick)

    x, y = 4, 4
    pick = PickSlotSchema(x=x, y=y)
    with raises(GameIsOver):
        game.pick_slot(pick)
Esempio n. 3
0
def test_loss_game(game):
    """Test to loss a game"""
    x, y = 5, 6
    pick = PickSlotSchema(x=x, y=y)
    picked_slot = game.board.slots[x][y]

    assert picked_slot.mine is True
    game.pick_slot(pick)
    assert game.status == GameStatusEnum.lost
Esempio n. 4
0
def test_game_pick_slot(game):
    """Test to pick a slot during the game"""
    x, y = 3, 0
    pick = PickSlotSchema(x=x, y=y)
    picked_slot = game.board.slots[x][y]

    assert picked_slot.available is True
    game.pick_slot(pick)
    assert picked_slot.available is False
Esempio n. 5
0
def test_board_toggle_flag_slot(game):
    """Test to toggle a flag in the board"""
    x, y = 7, 7
    flag_slot = PickSlotSchema(x=x, y=y)
    assert game.board.slots[x][y].flag is False
    game.board.toggle_flag_slot(flag_slot)
    assert game.board.slots[x][y].flag is True
    game.board.toggle_flag_slot(flag_slot)
    assert game.board.slots[x][y].flag is False
Esempio n. 6
0
def test_pick_slot_already_picked(game):
    """Test to pick a slot already picked during the game"""
    x, y = 2, 3
    pick = PickSlotSchema(x=x, y=y)
    picked_slot = game.board.slots[x][y]

    assert picked_slot.available is True
    game.pick_slot(pick)
    assert picked_slot.available is False

    with raises(SlotAlreadyPicked):
        game.pick_slot(pick)
Esempio n. 7
0
    def _clear_adjacent_slots(self, pick):
        """Clear adjacent slots when there is no mine around"""
        available_adjacent_slots = self._get_available_adjacent_slots(pick)
        are_mines = any([slot.mine for slot in available_adjacent_slots])

        while len(available_adjacent_slots) > 0 and not are_mines:
            for slot in available_adjacent_slots:
                slot.pick()
                new_pick = PickSlotSchema(x=slot.x, y=slot.y)
                self._clear_adjacent_slots(new_pick)

            available_adjacent_slots = self._get_available_adjacent_slots(pick)
            are_mines = any([slot.mine for slot in available_adjacent_slots])
Esempio n. 8
0
def test_win_game(game):
    """Test to win a game"""
    # Set all slots as picked
    for slot in game.board.iter_slots():
        slot.available = True if slot.mine else False

    # Set only one slot to be available
    x, y = 2, 3
    game.board.slots[x][y].available = True

    pick = PickSlotSchema(x=x, y=y)
    game.pick_slot(pick)
    assert game.status == GameStatusEnum.won
Esempio n. 9
0
def test_board_pick_slot_clear_adjacent(game):
    """Test to pick a slot in a board"""
    x, y = 4, 1
    pick = PickSlotSchema(x=x, y=y)
    picked_slot = game.board.slots[x][y]

    assert picked_slot.available is True
    game.pick_slot(pick)

    expected_result_after_clear = [
        [False, False, True, True, True, True, True, True],
        [False, False, True, True, True, True, True, True],
        [False, False, False, False, True, True, True, True],
        [False, False, False, False, True, True, True, True],
        [False, False, False, False, False, False, True, True],
        [False, False, False, False, False, False, True, True],
        [False, False, False, False, False, False, True, True],
        [False, False, True, True, True, True, True, True],
    ]
    for x, row in enumerate(game.board.slots):
        for y, slot in enumerate(row):
            assert slot.available is expected_result_after_clear[x][y]