Exemplo n.º 1
0
def test_step_return_values_win():
    under_test = ConnectFourGame()

    under_test.board = np.array(
        [
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [2, 2, 2, 0, 0, 0, 0],
            [1, 1, 1, 0, 0, 0, 0],
        ]
    )

    expected_observation = np.array(
        [
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [2, 2, 2, 0, 0, 0, 0],
            [1, 1, 1, 1, 0, 0, 0],
        ]
    )

    expected_reward = 100.0
    expected_done = True
    expected_info = {}

    observation, reward, done, info = under_test.step(3)

    assert np.array_equal(observation, expected_observation)
    assert expected_reward == reward
    assert expected_done == done
    assert expected_info == info
Exemplo n.º 2
0
def test_get_valid_moves():
    under_test = ConnectFourGame()

    expected = [0, 1, 2, 3, 4, 5, 6]

    actual = under_test.get_valid_moves()

    assert actual == expected
Exemplo n.º 3
0
def test_not_is_draw_with_full_board_and_winner():
    under_test = ConnectFourGame()
    under_test.board = np.array(
        [
            [2, 1, 2, 2, 1, 2, 1],
            [2, 1, 2, 2, 1, 2, 1],
            [2, 1, 2, 2, 1, 2, 1],
            [1, 2, 1, 2, 2, 1, 2],
            [1, 2, 1, 1, 2, 1, 1],
            [1, 2, 1, 1, 2, 1, 2],
        ]
    )

    assert not under_test.is_draw()
Exemplo n.º 4
0
def test_not_is_draw():
    under_test = ConnectFourGame()
    under_test.board = np.array(
        [
            [1, 0, 2, 0, 2, 0, 0],
            [1, 0, 2, 0, 1, 0, 0],
            [2, 0, 1, 0, 2, 0, 0],
            [1, 0, 2, 0, 1, 0, 0],
            [1, 0, 2, 0, 2, 0, 1],
            [1, 0, 2, 0, 1, 1, 2],
        ]
    )

    assert not under_test.is_draw()
Exemplo n.º 5
0
def test_error_if_col_full():
    under_test = ConnectFourGame()

    under_test.board = np.array(
        [
            [2, 0, 0, 0, 0, 0, 0],
            [1, 0, 0, 0, 0, 0, 0],
            [2, 0, 0, 0, 0, 0, 0],
            [1, 0, 0, 0, 0, 0, 0],
            [2, 0, 0, 0, 0, 0, 0],
            [1, 0, 0, 0, 0, 0, 0],
        ]
    )

    with pytest.raises(ValueError):
        under_test.step(0)
Exemplo n.º 6
0
def test_step():
    under_test = ConnectFourGame()

    expected = np.array(
        [
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [1, 0, 0, 0, 0, 0, 0],
        ]
    )

    under_test.step(0)

    assert np.array_equal(under_test.board, expected)
    assert under_test.turn == "O"
Exemplo n.º 7
0
def test_get_valid_moves_with_some_columns_full():
    under_test = ConnectFourGame()
    under_test.board = np.array(
        [
            [1, 0, 2, 0, 2, 0, 0],
            [1, 0, 2, 0, 1, 0, 0],
            [2, 0, 1, 0, 2, 0, 0],
            [1, 0, 2, 0, 1, 0, 0],
            [1, 0, 2, 0, 2, 0, 1],
            [1, 0, 2, 0, 1, 1, 2],
        ]
    )

    expected = [1, 3, 5, 6]

    actual = under_test.get_valid_moves()

    assert actual == expected
Exemplo n.º 8
0
def test_step_return_values_draw():
    under_test = ConnectFourGame()

    under_test.board = np.array(
        [
            [0, 1, 2, 2, 1, 2, 1],
            [2, 1, 2, 2, 1, 2, 1],
            [2, 1, 2, 2, 1, 2, 1],
            [1, 2, 1, 1, 2, 1, 2],
            [1, 2, 1, 1, 2, 1, 1],
            [1, 2, 1, 1, 2, 1, 2],
        ]
    )
    under_test.turn = "O"

    expected_observation = np.array(
        [
            [2, 1, 2, 2, 1, 2, 1],
            [2, 1, 2, 2, 1, 2, 1],
            [2, 1, 2, 2, 1, 2, 1],
            [1, 2, 1, 1, 2, 1, 2],
            [1, 2, 1, 1, 2, 1, 1],
            [1, 2, 1, 1, 2, 1, 2],
        ]
    )

    expected_reward = -50.0
    expected_done = True
    expected_info = {}

    observation, reward, done, info = under_test.step(0)

    assert np.array_equal(observation, expected_observation)
    assert expected_reward == reward
    assert expected_done == done
    assert expected_info == info
Exemplo n.º 9
0
def test_step_return_values():
    under_test = ConnectFourGame()

    expected_observation = np.array(
        [
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [1, 0, 0, 0, 0, 0, 0],
        ]
    )

    expected_reward = 0.0
    expected_done = False
    expected_info = {}

    observation, reward, done, info = under_test.step(0)

    assert np.array_equal(observation, expected_observation)
    assert expected_reward == reward
    assert expected_done == done
    assert expected_info == info
Exemplo n.º 10
0
def test_reset():
    under_test = ConnectFourGame()

    expected = np.array(
        [
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
            [0, 0, 0, 0, 0, 0, 0],
        ]
    )

    # Do some stuff
    under_test.step(1)
    under_test.step(2)
    under_test.step(1)
    under_test.step(4)

    under_test.reset()

    assert np.array_equal(under_test.board, expected)