Esempio n. 1
0
def test_act_with_not_ready_exception(rows, cols, action):
    # Arrange
    env = PuzzleEnvironment(PuzzleEnvironmentSettings(rows, cols))

    # Act, Assert
    with pytest.raises(PuzzleEnvironmentNotReadyException):
        env.act(action)
Esempio n. 2
0
def test_act(rows, cols, action):
    # Arrange
    env = PuzzleEnvironment(PuzzleEnvironmentSettings(rows, cols))
    env.setup()
    old_none_position = get_none_position(env.get_state(), rows, cols)

    # Act
    env.act(action)

    # Assert
    new_state = env.get_state()
    new_none_position = get_none_position(new_state, rows, cols)
    assert_unique_values(new_state, rows, cols)

    if action == PuzzleAction.UP and old_none_position[0] == rows - 1 or \
        action == PuzzleAction.DOWN and old_none_position[0] == 0 or \
        action == PuzzleAction.LEFT and old_none_position[1] == cols - 1 or \
        action == PuzzleAction.RIGHT and old_none_position[1] == 0:
        assert old_none_position == new_none_position
    else:
        if action == PuzzleAction.UP:
            assert old_none_position[0] + 1 == new_none_position[0]
            assert old_none_position[1] == new_none_position[1]
        elif action == PuzzleAction.DOWN:
            assert old_none_position[0] - 1 == new_none_position[0]
            assert old_none_position[1] == new_none_position[1]
        elif action == PuzzleAction.LEFT:
            assert old_none_position[0] == new_none_position[0]
            assert old_none_position[1] + 1 == new_none_position[1]
        elif action == PuzzleAction.RIGHT:
            assert old_none_position[0] == new_none_position[0]
            assert old_none_position[1] - 1 == new_none_position[1]
Esempio n. 3
0
def test_get_state_with_exception(rows, cols):
    # Arrange
    env = PuzzleEnvironment(PuzzleEnvironmentSettings(rows, cols))

    # Act, Assert
    with pytest.raises(PuzzleEnvironmentNotReadyException):
        env.get_state()
Esempio n. 4
0
def test_act_with_incorrect_action_exception(rows, cols, action):
    # Arrange
    env = PuzzleEnvironment(PuzzleEnvironmentSettings(rows, cols))

    # Act, Assert
    with pytest.raises(PuzzleEnvironmentException):
        env.act(action)
Esempio n. 5
0
def test_is_completed_with_exception(rows, cols):
    # Arrange
    env = PuzzleEnvironment(PuzzleEnvironmentSettings(rows, cols))

    # Act, Assert
    with pytest.raises(PuzzleEnvironmentNotReadyException):
        env.is_completed()
Esempio n. 6
0
def test_environment_setup_unique_values(rows, cols):
    # Arrange
    env = PuzzleEnvironment(PuzzleEnvironmentSettings(rows, cols))

    # Act
    env.setup()

    # Assert
    assert_unique_values(env.get_state(), rows, cols)
Esempio n. 7
0
def test_is_not_completed_none_element(rows, cols):
    # Arrange
    env = PuzzleEnvironment(PuzzleEnvironmentSettings(rows, cols))
    env.setup()

    # Act
    env._PuzzleEnvironment__env = generate_completed_env(rows, cols)
    env._PuzzleEnvironment__empty_position = (rows - 1, cols - 1)
    env.act(PuzzleAction.DOWN)
    is_completed = env.is_completed()

    # Assert
    assert not is_completed
Esempio n. 8
0
def test_get_state(rows, cols):
    # Arrange
    env = PuzzleEnvironment(PuzzleEnvironmentSettings(rows, cols))
    env.setup()

    # Act
    state = env.get_state()

    # Assert
    for i in range(rows):
        for j in range(cols):
            assert state[i][j] is None or (state[i][j] >= 1
                                           and state[i][j] < rows * cols)
Esempio n. 9
0
def test_is_completed(rows, cols):
    # Arrange
    env = PuzzleEnvironment(PuzzleEnvironmentSettings(rows, cols))
    env.setup()

    # Act
    # import pdb; pdb.set_trace()
    env._PuzzleEnvironment__env = generate_completed_env(rows, cols)
    env._PuzzleEnvironment__empty_position = (rows - 1, cols - 1)
    is_completed = env.is_completed()

    # Assert
    assert is_completed
Esempio n. 10
0
def test_is_not_completed_middle_elements(rows, cols):
    # Arrange
    env = PuzzleEnvironment(PuzzleEnvironmentSettings(rows, cols))
    env.setup()

    # Act
    env._PuzzleEnvironment__env = generate_completed_env(rows, cols)
    env._PuzzleEnvironment__empty_position = (rows - 1, cols - 1)
    buf = env._PuzzleEnvironment__env[0][0]
    env._PuzzleEnvironment__env[0][0] = env._PuzzleEnvironment__env[0][1]
    env._PuzzleEnvironment__env[0][1] = buf
    is_completed = env.is_completed()

    # Assert
    assert not is_completed
Esempio n. 11
0
def test_incorrect_settings(rows, cols):
    # Arrange, Act, Assert
    with pytest.raises(PuzzleEnvironmentException):
        PuzzleEnvironmentSettings(rows, cols)
Esempio n. 12
0
def run(args):
    with terminal_window() as window:
        puzzle_env_settings = PuzzleEnvironmentSettings(args.rows_number, args.cols_number)
        game = PuzzleGame(window, puzzle_env_settings, args.debug)
        game.start()