Exemple #1
0
    def update_path(self):
        if self.analyzed_maze:
            prev_directions = self.analyzed_maze.directions
        else:
            prev_directions = None

        self.analyzed_maze = analyze(self.array)

        if not self.path_list:
            self.path_list = [[] for x in range(const.DUDE_NUM)]

        # computes paths
        # in game mode returns "True" if there are unreachable dudes
        hopeless_fools = self.compute_paths()

        if hopeless_fools:
            if prev_directions is None:
                # we load a file with unreachable dudes
                self.unreachable = True
                raise ValueError('There is an unreachable dude...')

            self.analyzed_maze.directions = prev_directions

        self.unreachable = False

        # get set of all path cells
        flatten = lambda l: [item for sub_list in l for item in sub_list]
        self.all_path_cells = set(flatten(self.path_list))

        self.directions = self.analyzed_maze.directions

        return hopeless_fools
Exemple #2
0
def empty(request):
    h, w = request.param
    maze = zeros(h, w)
    maze[0, 0] = 1
    amaze = analyze(maze)
    print_all(maze, amaze)
    return maze, amaze
Exemple #3
0
def test_walls(maze):
    sol = analyze(maze)
    (rows, cols) = maze.shape
    assert numpy.all(char == b"#" for char in sol.directions[0])
    assert numpy.all(char == b"#" for char in sol.directions[rows - 1])
    assert numpy.all(char == b"#" for char in sol.directions[:0])
    assert numpy.all(char == b"#" for char in sol.directions[:cols - 1])
Exemple #4
0
def test_attributes(maze):
    sol = analyze(maze)
    assert hasattr(sol, 'directions')
    assert hasattr(sol, 'distances')
    assert hasattr(sol, 'is_reachable')
    assert hasattr(sol, 'path')
    assert not hasattr(sol, 'nonexistent_attribute')
Exemple #5
0
def gen_notreachable():
    random.seed(89)
    array = maze.generate_maze(8, 8)
    array = maze.remove_frame(array)
    array[5, 4] = -1
    array = maze.randomize(array)
    return maze.analyze(array)
Exemple #6
0
def test_simple_maze2():
    test_array = numpy.array([[-1, 0, -1, -1], [-1, 0, 0, -1], [-1, -1, 0, -1],
                              [0, 1, 0, -1], [-1, -1, 0, -1]])
    r = analyze(test_array)
    assert r.is_reachable == True
    assert r.path(0, 1) == [(0, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1)]
    assert r.distances[0, 1] == 5
Exemple #7
0
def test_path(stored_maze2):
    a = analyze(stored_maze2[0])
    point = (1, 1)
    distance = a.distances[point]
    path = a.path(*point)
    assert (len(path) == distance + 1)
    assert (path[0] == point)
    assert (path[-1] == a.goal)
Exemple #8
0
def test_extreme_long_path():
    """time test"""
    A = numpy.full((5000, 5000), 0, dtype=int)
    A[8, 7] = 1
    B = maze.analyze(A)
    B.path(4999, 4999)
    B.path(4997, 4999)
    B.path(4996, 4999)
    B.path(4995, 4999)
    B.path(4999, 4995)
    assert (B.distances[2, 2] == 11)
Exemple #9
0
def test_simple_maze_cycle():
    test_array = numpy.array([[-1, 0, -1, -1, -1], [-1, 0, 0, 0, 0],
                              [-1, -1, 0, -1, 0], [0, 1, 0, 0, 0],
                              [-1, -1, -1, -1, -1]])
    r = analyze(test_array)
    assert r.is_reachable == True
    assert r.path(0, 1) == [(0, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1)]
    assert r.distances[0, 1] == 5
    # wall
    with pytest.raises(Exception):
        r.path(0, 4)
Exemple #10
0
 def _recalculate_paths(self, row, column, value, old_value):
     if value in self.DUDE_VALUES and self.result is not None:
         # No need to recalculate
         return
     # Recalculate paths if there is a goal and dudes in the maze
     self.goal = maze.find_goal(self.array)
     if self.goal is not None and type(self.goal) is not tuple:
         self.goal = tuple(self.goal)
     if self.goal is not None and len(
             self.dudes) > 0 and self.result is None:
         self.result = maze.analyze(self.array)
Exemple #11
0
def global_walled(request):
    h, w, d = request.param
    skip_small(h, w, d)
    maze = zeros(h, w)
    maze[0, 0] = 1
    half = w // 2 if d == VERTICAL else h // 2

    maze[dim(half, d)] = -1
    maze[dim(half + 1, d)] = -1
    amaze = analyze(maze)
    return maze, amaze, half, d
Exemple #12
0
def test_simple_maze():
    test_array = numpy.array([
        [-1, 0, -1, -1],
        [-1, 0, 0, -1],
        [-1, -1, 0, -1],
        [1, 0, 0, -1],
        [-1, -1, -1, -1]
    ])
    r = analyze(test_array)
    assert r.is_reachable == True
    assert r.path(0,1) == [(0, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1), (3, 0)]
    assert r.distances[0, 1] == 6
Exemple #13
0
def test_simple_maze_cycle():
    test_array = numpy.array([
        [-1, 0, -1, -1, -1],
        [-1, 0, 0, 0, 0],
        [-1, -1, 0, -1, 0],
        [0, 1, 0, 0, 0],
        [-1, -1, -1, -1, -1]
    ])
    r = analyze(test_array)
    assert r.is_reachable == True
    assert r.path(0,1) == [(0, 1), (1, 1), (1, 2), (2, 2), (3, 2), (3, 1)]
    assert r.distances[0, 1] == 5
    # wall
    with pytest.raises(Exception):
        r.path(0,4)
Exemple #14
0
def s_shape(request):
    h, w = request.param
    maze = numpy.full((h, w), -1, dtype=numpy.int8)
    directions = numpy.full((h, w), b'#', dtype=('a', 1))
    distances = numpy.full(maze.shape, -1, dtype=numpy.int)
    path = []

    # This prepares both the maze and expected result for shapes like:
    #
    #    v#>>v#X
    #    v#^#v#^
    #    v#^#v#^
    #    v#^#v#^
    #    v#^#v#^
    #    >>^#>>^
    for column in range(w):
        if column % 2 == 0:
            maze[:, column] = 0
            if column % 4 == 0:
                directions[:, column] = b'v'
                directions[h - 1, column] = b'>'
                path += [(r, column) for r in range(h)]
            else:
                directions[:, column] = b'^'
                directions[0, column] = b'>'
                path += [(r, column) for r in reversed(range(h))]
        else:
            row = h - 1 if column % 4 == 1 else 0
            path.append((row, column))
            maze[row, column] = 0
            directions[row, column] = b'>'

    # Target
    column = w - 1
    row = h - 1 if column % 4 < 2 else 0
    maze[row, column] = 1
    directions[row, column] = b'X'

    # Reconstruct the distances from path
    for idx, loc in enumerate(reversed(path)):
        distances[loc] = idx

    amaze = analyze(maze)
    print_all(maze, amaze)
    return maze, distances, directions, path, amaze
Exemple #15
0
def test_m1():
    in_maze = np.array([[-1, -1, -1, 0, -1, -1, -1, 0, -1, -1, -1],
                        [-1, 0, 0, 0, -1, 0, 0, 0, 0, 0, -1],
                        [-1, -1, -1, 0, -1, -1, -1, -1, -1, 0, -1],
                        [-1, 0, -1, 0, 0, 0, 0, 0, -1, 0, -1],
                        [-1, 0, -1, -1, -1, -1, -1, -1, -1, 0, -1],
                        [0, 0, -1, 0, 0, 0, 0, 1, 0, 0, -1],
                        [-1, -1, -1, 0, -1, -1, -1, 0, -1, -1, -1],
                        [-1, 0, 0, 0, -1, 0, -1, 0, -1, 0, 0],
                        [-1, 0, -1, -1, -1, 0, -1, 0, -1, 0, -1],
                        [-1, 0, -1, 0, 0, 0, 0, 0, -1, 0, -1],
                        [-1, -1, -1, 0, -1, -1, -1, -1, -1, 0, -1]])
    m_directions = np.array(
        [[b'#', b'#', b'#', b' ', b'#', b'#', b'#', b'v', b'#', b'#', b'#'],
         [b'#', b' ', b' ', b' ', b'#', b'>', b'>', b'>', b'>', b'v', b'#'],
         [b'#', b'#', b'#', b' ', b'#', b'#', b'#', b'#', b'#', b'v', b'#'],
         [b'#', b' ', b'#', b' ', b' ', b' ', b' ', b' ', b'#', b'v', b'#'],
         [b'#', b' ', b'#', b'#', b'#', b'#', b'#', b'#', b'#', b'v', b'#'],
         [b' ', b' ', b'#', b'>', b'>', b'>', b'>', b'X', b'<', b'<', b'#'],
         [b'#', b'#', b'#', b'^', b'#', b'#', b'#', b'^', b'#', b'#', b'#'],
         [b'#', b'>', b'>', b'^', b'#', b'v', b'#', b'^', b'#', b' ', b' '],
         [b'#', b'^', b'#', b'#', b'#', b'v', b'#', b'^', b'#', b' ', b'#'],
         [b'#', b'^', b'#', b'>', b'>', b'>', b'>', b'^', b'#', b' ', b'#'],
         [b'#', b'#', b'#', b'^', b'#', b'#', b'#', b'#', b'#', b' ', b'#']])
    m_distances = np.array([[-1, -1, -1, -1, -1, -1, -1, 9, -1, -1, -1],
                            [-1, -1, -1, -1, -1, 10, 9, 8, 7, 6, -1],
                            [-1, -1, -1, -1, -1, -1, -1, -1, -1, 5, -1],
                            [-1, -1, -1, -1, -1, -1, -1, -1, -1, 4, -1],
                            [-1, -1, -1, -1, -1, -1, -1, -1, -1, 3, -1],
                            [-1, -1, -1, 4, 3, 2, 1, 0, 1, 2, -1],
                            [-1, -1, -1, 5, -1, -1, -1, 1, -1, -1, -1],
                            [-1, 8, 7, 6, -1, 8, -1, 2, -1, -1, -1],
                            [-1, 9, -1, -1, -1, 7, -1, 3, -1, -1, -1],
                            [-1, 10, -1, 8, 7, 6, 5, 4, -1, -1, -1],
                            [-1, -1, -1, 9, -1, -1, -1, -1, -1, -1, -1]])
    m_reachable = False
    m = analyze(in_maze)

    assert np.array_equal(m_distances, m.distances)
    assert np.array_equal(m_directions, m.directions)
    assert m_reachable == m.is_reachable
Exemple #16
0
def global_walled(request):
    h, w, d = request.param
    skip_small(h, w, d)
    maze = zeros(h, w)
    maze[0, 0] = 1
    half = w // 2 if d == VERTICAL else h // 2

    maze[dim(half, d)] = -1
    maze[dim(half + 1, d)] = -1

    print("BX--------")
    print(maze)
    print("EX--------")

    amaze = analyze(maze)

    print("mBX--------")
    print(amaze.directions)
    print(amaze.distances)
    print("mEX--------")

    return maze, amaze, half, d
Exemple #17
0
def test_m2():
    in_maze = np.array([[-1, -1, -1, -1, -1], [0, 0, 0, 0, -1],
                        [-1, -1, -1, -1, -1], [0, 0, 0, 1, -1],
                        [-1, 0, -1, -1, -1], [-1, 0, -1, 0, 0],
                        [-1, -1, -1, 0, 0]])
    m_directions = np.array([[b'#', b'#', b'#', b'#', b'#'],
                             [b' ', b' ', b' ', b' ', b'#'],
                             [b'#', b'#', b'#', b'#', b'#'],
                             [b'>', b'>', b'>', b'X', b'#'],
                             [b'#', b'^', b'#', b'#', b'#'],
                             [b'#', b'^', b'#', b' ', b' '],
                             [b'#', b'#', b'#', b' ', b' ']])
    m_distances = np.array([[-1, -1, -1, -1, -1], [-1, -1, -1, -1, -1],
                            [-1, -1, -1, -1, -1], [3, 2, 1, 0, -1],
                            [-1, 3, -1, -1, -1], [-1, 4, -1, -1, -1],
                            [-1, -1, -1, -1, -1]])
    m_reachable = False
    m = analyze(in_maze)

    assert np.array_equal(m_distances, m.distances)
    assert np.array_equal(m_directions, m.directions)
    assert m_reachable == m.is_reachable
    assert m.path(3, 0) == [(3, 0), (3, 1), (3, 2), (3, 3)]
    assert m.path(5, 1) == [(5, 1), (4, 1), (3, 1), (3, 2), (3, 3)]
Exemple #18
0
def test_finnish(maze):
    sol = analyze(maze)
    x, y = numpy.where(sol.distances == 0)
    assert sol.directions[x, y] == b"X"
Exemple #19
0
def test_distances(maze):
    sol = analyze(maze)
    assert 12 == sol.distances[1][1]
Exemple #20
0
def test_path(maze):
    sol = analyze(maze)
    path = sol.path(1, 1)
    assert isinstance(path, list)
    assert isinstance(path[0][0], int)
    assert isinstance(path[0][1], int)
Exemple #21
0
def test_pathInWall(maze):
    sol = analyze(maze)
    with pytest.raises(Exception):
        sol.path(0, 0)
Exemple #22
0
def test_directions(stored_maze2):
    a = analyze(stored_maze2[0])
    assert (a.directions[20, 23] == b" ")
    assert (a.directions[0, 0] == b"#")
    assert (a.directions[0, 0] == b"#")
    assert (a.directions[10, 29] == b"X")
Exemple #23
0
def gen_noframe():
    random.seed(42)
    array = maze.generate_maze(10, 10)
    array = maze.remove_frame(array)
    return maze.analyze(array)
Exemple #24
0
def test_path_speed(huge):
    amaze = analyze(huge)
    for i in range(250):
        amaze.path(2047, 2047)
Exemple #25
0
import maze
import numpy
A = numpy.array([[-1,1],[0,0]])
B =maze.analyze( A )
print(A)
print(B.directions)
print(B.distances)
print(B.is_reachable)
print(B.path(1,1))
Exemple #26
0
def test_m3():
    in_maze = np.array([[-1, -1, -1, -1, -1, 0, -1, -1, -1, -1, -1, -1, -1],
                        [-1, 0, 0, 0, -1, 0, -1, 0, 0, 0, 0, 0, 1],
                        [-1, 0, -1, -1, -1, 0, -1, 0, -1, -1, -1, -1, -1],
                        [-1, 0, 0, 0, 0, 0, 0, 0, -1, 0, 0, 0, -1],
                        [-1, -1, -1, -1, -1, -1, -1, 0, -1, -1, -1, 0, -1],
                        [0, 0, 0, 0, 0, 0, -1, 0, -1, 0, 0, 0, -1],
                        [-1, -1, -1, 0, -1, -1, -1, 0, -1, 0, -1, -1, -1],
                        [0, 0, -1, 0, -1, 0, 0, 0, -1, 0, -1, 0, 0],
                        [-1, -1, -1, 0, -1, -1, -1, -1, -1, 0, -1, 0, -1],
                        [-1, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, 0, -1],
                        [-1, 0, -1, -1, -1, -1, -1, 0, -1, 0, -1, 0, -1],
                        [-1, 0, -1, 0, 0, 0, -1, 0, 0, 0, -1, 0, -1],
                        [-1, 0, -1, 0, -1, 0, -1, 0, -1, -1, -1, 0, -1],
                        [-1, 0, -1, 0, -1, 0, -1, 0, -1, 0, 0, 0, -1],
                        [-1, 0, -1, 0, -1, -1, -1, 0, -1, -1, -1, -1, -1],
                        [-1, 0, -1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
                        [-1, -1, -1, 0, -1, -1, -1, -1, -1, -1, -1, -1, -1]])
    m_directions = np.array([[
        b'#', b'#', b'#', b'#', b'#', b'v', b'#', b'#', b'#', b'#', b'#', b'#',
        b'#'
    ],
                             [
                                 b'#', b'v', b'<', b'<', b'#', b'v', b'#',
                                 b'>', b'>', b'>', b'>', b'>', b'X'
                             ],
                             [
                                 b'#', b'v', b'#', b'#', b'#', b'v', b'#',
                                 b'^', b'#', b'#', b'#', b'#', b'#'
                             ],
                             [
                                 b'#', b'>', b'>', b'>', b'>', b'>', b'>',
                                 b'^', b'#', b' ', b' ', b' ', b'#'
                             ],
                             [
                                 b'#', b'#', b'#', b'#', b'#', b'#', b'#',
                                 b'^', b'#', b'#', b'#', b' ', b'#'
                             ],
                             [
                                 b' ', b' ', b' ', b' ', b' ', b' ', b'#',
                                 b'^', b'#', b' ', b' ', b' ', b'#'
                             ],
                             [
                                 b'#', b'#', b'#', b' ', b'#', b'#', b'#',
                                 b'^', b'#', b' ', b'#', b'#', b'#'
                             ],
                             [
                                 b' ', b' ', b'#', b' ', b'#', b'>', b'>',
                                 b'^', b'#', b' ', b'#', b' ', b' '
                             ],
                             [
                                 b'#', b'#', b'#', b' ', b'#', b'#', b'#',
                                 b'#', b'#', b' ', b'#', b' ', b'#'
                             ],
                             [
                                 b'#', b' ', b' ', b' ', b' ', b' ', b' ',
                                 b' ', b' ', b' ', b'#', b' ', b'#'
                             ],
                             [
                                 b'#', b' ', b'#', b'#', b'#', b'#', b'#',
                                 b' ', b'#', b' ', b'#', b' ', b'#'
                             ],
                             [
                                 b'#', b' ', b'#', b' ', b' ', b' ', b'#',
                                 b' ', b' ', b' ', b'#', b' ', b'#'
                             ],
                             [
                                 b'#', b' ', b'#', b' ', b'#', b' ', b'#',
                                 b' ', b'#', b'#', b'#', b' ', b'#'
                             ],
                             [
                                 b'#', b' ', b'#', b' ', b'#', b' ', b'#',
                                 b' ', b'#', b' ', b' ', b' ', b'#'
                             ],
                             [
                                 b'#', b' ', b'#', b' ', b'#', b'#', b'#',
                                 b' ', b'#', b'#', b'#', b'#', b'#'
                             ],
                             [
                                 b'#', b' ', b'#', b' ', b' ', b' ', b' ',
                                 b' ', b' ', b' ', b' ', b' ', b' '
                             ],
                             [
                                 b'#', b'#', b'#', b' ', b'#', b'#', b'#',
                                 b'#', b'#', b'#', b'#', b'#', b'#'
                             ]])
    m_distances = np.array(
        [[-1, -1, -1, -1, -1, 12, -1, -1, -1, -1, -1, -1, -1],
         [-1, 15, 16, 17, -1, 11, -1, 5, 4, 3, 2, 1, 0],
         [-1, 14, -1, -1, -1, 10, -1, 6, -1, -1, -1, -1, -1],
         [-1, 13, 12, 11, 10, 9, 8, 7, -1, -1, -1, -1, -1],
         [-1, -1, -1, -1, -1, -1, -1, 8, -1, -1, -1, -1, -1],
         [-1, -1, -1, -1, -1, -1, -1, 9, -1, -1, -1, -1, -1],
         [-1, -1, -1, -1, -1, -1, -1, 10, -1, -1, -1, -1, -1],
         [-1, -1, -1, -1, -1, 13, 12, 11, -1, -1, -1, -1, -1],
         [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
         [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
         [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
         [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
         [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
         [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
         [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
         [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1],
         [-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1]])
    m_reachable = False
    m = analyze(in_maze)

    assert np.array_equal(m_distances, m.distances)
    assert np.array_equal(m_directions, m.directions)
    assert m_reachable == m.is_reachable
    assert m.path(1, 3) == [(1, 3), (1, 2), (1, 1), (2, 1), (3, 1), (3, 2),
                            (3, 3), (3, 4), (3, 5), (3, 6), (3, 7), (2, 7),
                            (1, 7), (1, 8), (1, 9), (1, 10), (1, 11), (1, 12)]
    assert m.path(7, 5) == [(7, 5), (7, 6), (7, 7), (6, 7), (5, 7), (4, 7),
                            (3, 7), (2, 7), (1, 7), (1, 8), (1, 9), (1, 10),
                            (1, 11), (1, 12)]
    with pytest.raises(IndexError):
        m.path(
            0,
            0,
        )
    with pytest.raises(IndexError):
        m.path(5, 0)
Exemple #27
0
def test_extreme_long():
    """time test"""
    A = numpy.full((5000, 5000), 0, dtype=int)
    A[8, 7] = 1
    B = maze.analyze(A)
    assert (B.distances[2, 2] == 11)
Exemple #28
0
def global_empty(request):
    h, w = request.param
    maze = zeros(h, w)
    maze[0, 0] = 1
    amaze = analyze(maze)
    return maze, amaze
Exemple #29
0
def test_analyze_speed(huge):
    for i in range(20):
        amaze = analyze(huge)
Exemple #30
0
def test_isreachable2(maze2):
    sol = analyze(maze2)
    assert sol.is_reachable
    assert not b" " in sol.directions
Exemple #31
0
def gen_norm():
    random.seed(11)
    array = maze.generate_maze(10, 10)
    return maze.analyze(array)
Exemple #32
0
def gen_randomized():
    random.seed(66)
    array = maze.generate_maze(10, 10)
    array = maze.remove_frame(array)
    array = maze.randomize(array)
    return maze.analyze(array)