Ejemplo n.º 1
0
def test_successor_4x4_vertical_edge_right():
    # given
    current_state = [
        [1, 2, 3, 4],
        [5, 6, 7, 0],
        [9, 10, 11, 12],
        [13, 14, 15, 13]
    ]
    current_position = Position(3, 1)

    # when
    result = list(state_space.successors(current_state, current_position))

    # then
    expected_successors = [
        (
            [
                [1, 2, 3, 4],
                [5, 6, 0, 7],
                [9, 10, 11, 12],
                [13, 14, 15, 13]
            ],
            Position(2, 1),
            1,
            7
        ),
        (
            [
                [1, 2, 3, 0],
                [5, 6, 7, 4],
                [9, 10, 11, 12],
                [13, 14, 15, 13]
            ],
            Position(3, 0),
            1,
            4
        ),
        (
            [
                [1, 2, 3, 4],
                [5, 6, 7, 12],
                [9, 10, 11, 0],
                [13, 14, 15, 13]
            ],
            Position(3, 2),
            1,
            12
        )
    ]

    assert len(result) == len(expected_successors)
    for successor in result:
        assert successor in expected_successors
Ejemplo n.º 2
0
def test_successor_4x4_horizontal_edge_down():
    # given
    current_state = [
        [1, 2, 3, 4],
        [5, 6, 7, 8],
        [9, 10, 11, 12],
        [13, 14, 0, 15]
    ]
    current_position = Position(2, 3)

    # when
    result = list(state_space.successors(current_state, current_position))

    # then
    expected_successors = [
        (
            [
                [1, 2, 3, 4],
                [5, 6, 7, 8],
                [9, 10, 11, 12],
                [13, 14, 15, 0]
            ],
            Position(3, 3),
            1,
            15
        ),
        (
            [
                [1, 2, 3, 4],
                [5, 6, 7, 8],
                [9, 10, 11, 12],
                [13, 0, 14, 15]
            ],
            Position(1, 3),
            1,
            14
        ),
        (
            [
                [1, 2, 3, 4],
                [5, 6, 7, 8],
                [9, 10, 0, 12],
                [13, 14, 11, 15]
            ],
            Position(2, 2),
            1,
            11
        )
    ]

    assert len(result) == len(expected_successors)
    for successor in result:
        assert successor in expected_successors
Ejemplo n.º 3
0
def test_successor_4x4_vertical_edge_left():
    # given
    current_state = [
        [1, 2, 3, 4],
        [5, 6, 7, 8],
        [0, 10, 11, 12],
        [13, 14, 15, 9]
    ]
    current_position = Position(0, 2)

    # when
    result = list(state_space.successors(current_state, current_position))

    # then
    expected_successors = [
        (
            [
                [1, 2, 3, 4],
                [0, 6, 7, 8],
                [5, 10, 11, 12],
                [13, 14, 15, 9]
            ],
            Position(0, 1),
            1,
            5
        ),
        (
            [
                [1, 2, 3, 4],
                [5, 6, 7, 8],
                [13, 10, 11, 12],
                [0, 14, 15, 9]
            ],
            Position(0, 3),
            1,
            13
        ),
        (
            [
                [1, 2, 3, 4],
                [5, 6, 7, 8],
                [10, 0, 11, 12],
                [13, 14, 15, 9]
            ],
            Position(1, 2),
            1,
            10
        )
    ]

    assert len(result) == len(expected_successors)
    for successor in result:
        assert successor in expected_successors
Ejemplo n.º 4
0
def move_down_right(state, current_position, height, width):
    new_x = increment_axis(current_position.x, width)
    new_y = increment_axis(current_position.y, height)
    new_state, moved_token = get_new_sate(state, current_position.x,
                                          current_position.y, new_x, new_y)
    new_position = Position(new_x, new_y)
    return new_state, new_position, 3, moved_token
Ejemplo n.º 5
0
def move_right(state, current_position, width):
    cost = 2 if current_position.x == width - 1 else 1
    new_x = increment_axis(current_position.x, width)
    new_state, moved_token = get_new_sate(state, current_position.x,
                                          current_position.y, new_x,
                                          current_position.y)
    new_position = Position(new_x, current_position.y)
    return new_state, new_position, cost, moved_token
Ejemplo n.º 6
0
def move_down(state, current_position, height):
    cost = 2 if current_position.y == height - 1 else 1
    new_y = increment_axis(current_position.y, height)
    new_state, moved_token = get_new_sate(state, current_position.x,
                                          current_position.y,
                                          current_position.x, new_y)
    new_position = Position(current_position.x, new_y)
    return new_state, new_position, cost, moved_token
Ejemplo n.º 7
0
def test_successor_4x2_horizontal_edge_down():
    # given
    current_state = [
        [1, 2, 3, 4],
        [5, 6, 0, 7]
    ]
    current_position = Position(2, 1)

    # when
    result = list(state_space.successors(current_state, current_position))

    # then
    expected_successors = [
        (
            [
                [1, 2, 0, 4],
                [5, 6, 3, 7]
            ],
            Position(2, 0),
            1,
            3
        ),
        (
            [
                [1, 2, 3, 4],
                [5, 6, 7, 0]
            ],
            Position(3, 1),
            1,
            7
        ),
        (
            [
                [1, 2, 3, 4],
                [5, 0, 6, 7]
            ],
            Position(1, 1),
            1,
            6
        )
    ]

    assert len(result) == len(expected_successors)
    for successor in result:
        assert successor in expected_successors
Ejemplo n.º 8
0
def test_generate_start_state_4x2():
    state = ([[1, 2, 3, 4], [5, 6, 7, 0]], Position(3, 1))

    for i in range(10000):
        choice = random.choice(list(successors(state[0], state[1])))
        state = (choice[0], choice[1])

    print(state)
    assert True
Ejemplo n.º 9
0
def test_generate_start_state_10x10():
    state = (
        [
            [1, 2, 3, 4, 5, 6, 7, 8, 9, 10],
            [11, 12, 13, 14, 15, 16, 17, 18, 19, 20],
            [21, 22, 23, 24, 25, 26, 27, 28, 29, 30],
            [31, 32, 33, 34, 35, 36, 37, 38, 39, 40],
            [41, 42, 43, 44, 45, 46, 47, 48, 49, 50],
            [51, 52, 53, 54, 55, 56, 57, 58, 59, 60],
            [61, 62, 63, 64, 65, 66, 67, 68, 69, 70],
            [71, 72, 73, 74, 75, 76, 77, 78, 79, 80],
            [81, 82, 83, 84, 85, 86, 87, 88, 89, 90],
            [91, 92, 93, 94, 95, 96, 97, 98, 99,
             0]  # this is the most unsatisfying thing i've seen all week
        ],
        Position(9, 9))

    for i in range(10000):
        choice = random.choice(list(successors(state[0], state[1])))
        state = (choice[0], choice[1])

    print(state)
    assert True
Ejemplo n.º 10
0
def find_element(state, element):
    for i in range(len(state)):
        for j in range(len(state[i])):
            if state[i][j] == element:
                return Position(j, i)
Ejemplo n.º 11
0
def test_successors_4x2_minor_corner():
    # given
    current_state = [
        [1, 2, 3, 4],
        [0, 6, 7, 5]
    ]
    current_position = Position(0, 1)

    # when
    result = list(state_space.successors(current_state, current_position))

    # then
    expected_successors = [
        (
            [
                [0, 2, 3, 4],
                [1, 6, 7, 5]
            ],
            Position(0, 0),
            1,
            1
        ),
        (
            [
                [1, 2, 3, 4],
                [6, 0, 7, 5]
            ],
            Position(1, 1),
            1,
            6
        ),
        (
            [
                [1, 2, 3, 4],
                [5, 6, 7, 0]
            ],
            Position(3, 1),
            2,
            5
        ),
        (
            [
                [1, 0, 3, 4],
                [2, 6, 7, 5]
            ],
            Position(1, 0),
            3,
            2
        ),
        (
            [
                [1, 2, 3, 0],
                [4, 6, 7, 5]
            ],
            Position(3, 0),
            3,
            4
        )
    ]

    assert len(result) == len(expected_successors)
    for successor in result:
        assert successor in expected_successors
Ejemplo n.º 12
0
def test_successor_4x4_minor_corner():
    # given
    current_state = [
        [1, 2, 3, 0],
        [5, 6, 7, 8],
        [9, 10, 11, 12],
        [13, 14, 15, 4]
    ]
    current_position = Position(3, 0)

    # when
    result = list(state_space.successors(current_state, current_position))

    # then
    expected_successors = [
        (
            [
                [1, 2, 0, 3],
                [5, 6, 7, 8],
                [9, 10, 11, 12],
                [13, 14, 15, 4]
            ],
            Position(2, 0),
            1,
            3
        ),
        (
            [
                [1, 2, 3, 8],
                [5, 6, 7, 0],
                [9, 10, 11, 12],
                [13, 14, 15, 4]
            ],
            Position(3, 1),
            1,
            8
        ),
        (
            [
                [1, 2, 3, 4],
                [5, 6, 7, 8],
                [9, 10, 11, 12],
                [13, 14, 15, 0]
            ],
            Position(3, 3),
            2,
            4
        ),
        (
            [
                [0, 2, 3, 1],
                [5, 6, 7, 8],
                [9, 10, 11, 12],
                [13, 14, 15, 4]
            ],
            Position(0, 0),
            2,
            1
        ),
        (
            [
                [1, 2, 3, 7],
                [5, 6, 0, 8],
                [9, 10, 11, 12],
                [13, 14, 15, 4]
            ],
            Position(2, 1),
            3,
            7
        ),
        (
            [
                [1, 2, 3, 13],
                [5, 6, 7, 8],
                [9, 10, 11, 12],
                [0, 14, 15, 4]
            ],
            Position(0, 3),
            3,
            13
        )
    ]

    assert len(result) == len(expected_successors)
    for successor in result:
        assert successor in expected_successors
Ejemplo n.º 13
0
def test_successors_4x2_major_corner():
    # given
    current_state = [
        [4, 2, 3, 1],
        [5, 6, 7, 0]
    ]
    current_position = Position(3, 1)

    # when
    result = list(state_space.successors(current_state, current_position))

    # then
    expected_successors = [
        (
            [
                [4, 2, 3, 0],
                [5, 6, 7, 1],
            ],
            Position(3, 0),
            1,
            1
        ),
        (
            [
                [4, 2, 3, 1],
                [5, 6, 0, 7],
            ],
            Position(2, 1),
            1,
            7
        ),
        (
            [
                [4, 2, 3, 1],
                [0, 6, 7, 5],
            ],
            Position(0, 1),
            2,
            5
        ),
        (
            [
                [4, 2, 0, 1],
                [5, 6, 7, 3],
            ],
            Position(2, 0),
            3,
            3
        ),
        (
            [
                [0, 2, 3, 1],
                [5, 6, 7, 4],
            ],
            Position(0, 0),
            3,
            4
        )
    ]

    assert len(result) == len(expected_successors)
    for successor in result:
        assert successor in expected_successors
Ejemplo n.º 14
0
def test_successor_4x4_middle():
    # given
    current_state = [
        [1, 2, 3, 4],
        [5, 6, 0, 8],
        [9, 10, 11, 12],
        [13, 14, 15, 7]
    ]
    current_position = Position(2, 1)

    # when
    result = list(state_space.successors(current_state, current_position))

    # then
    expected_successors = [
        (
            [
                [1, 2, 0, 4],
                [5, 6, 3, 8],
                [9, 10, 11, 12],
                [13, 14, 15, 7]
            ],
            Position(2, 0),
            1,
            3
        ),
        (
            [
                [1, 2, 3, 4],
                [5, 6, 11, 8],
                [9, 10, 0, 12],
                [13, 14, 15, 7]
            ],
            Position(2, 2),
            1,
            11
        ),
        (
            [
                [1, 2, 3, 4],
                [5, 0, 6, 8],
                [9, 10, 11, 12],
                [13, 14, 15, 7]
            ],
            Position(1, 1),
            1,
            6
        ),
        (
            [
                [1, 2, 3, 4],
                [5, 6, 8, 0],
                [9, 10, 11, 12],
                [13, 14, 15, 7]
            ],
            Position(3, 1),
            1,
            8
        )
    ]

    assert len(result) == len(expected_successors)
    for successor in result:
        assert successor in expected_successors