def test_get_new_maze(set_seed, iteration): # generate a few mazes and check them for consistency local_seed = random.randint(1, 2**31 - 1) * iteration maze_str = mg.get_new_maze(8, 16, nfood=15, seed=local_seed) maze = mg.str_to_maze(maze_str) height, width = maze.shape # check that the returned maze has all the pacmen for pacman in (b'0', b'1', b'2', b'3'): assert np.any(maze == pacman) # now that we now we have a pacman, check that we have it only once # and remove it by putting an empty space instead row, col = np.nonzero(maze == pacman) assert len(row) == 1 assert len(col) == 1 maze[row, col] = mg.E # check that we have in total twice nfood in the maze assert (maze == mg.F).sum() == 15 * 2 # remove the food for computing dead ends and chambers maze[maze == mg.F] = mg.E # check that the returned maze is center-mirror symmetric left_maze = np.flipud(np.fliplr(maze[:, width // 2:])) assert np.all(left_maze == maze[:, :width // 2]) # check that we don't have any dead ends # no node in the graph should have only one connection graph = mg.walls_to_graph(maze) for node, degree in graph.degree(): assert degree > 1 # now check that we don't have chambers, i.e. the connectivity of the # graph is > 1 assert nx.node_connectivity(graph) > 1
def test_find_chamber(): # This maze has one single chamber, whose entrance is one of the # nodes (1,2), (1,3) or (1,4) maze_chamber = """############ # # # # # # # ### # # # # # ############""" maze = mg.str_to_maze(maze_chamber) maze_orig = maze.copy() mg.remove_all_dead_ends(maze) # first, check that the chamber is not mistaken for a dead end assert np.all(maze_orig == maze) # now check that we detect it graph = mg.walls_to_graph(maze) # there are actually two nodes that can be considered entrances entrance, chamber = mg.find_chamber(graph) assert entrance in ((1, 2), (1, 3), (1, 4)) # check that the chamber contains the right nodes. Convert to set, because # the order is irrelevant if entrance == (1, 4): expected_chamber = {(1, 1), (1, 2), (1, 3), (2, 1), (2, 2), (3, 1), (3, 2)} elif entrance == (1, 3): expected_chamber = {(1, 1), (1, 2), (2, 1), (2, 2), (3, 1), (3, 2)} else: expected_chamber = {(1, 1), (2, 1), (2, 2), (3, 1), (3, 2)} assert set(chamber) == expected_chamber # now remove the chamber and verify that we don't detect anything # we just remove wall (4,1) manually maze = mg.str_to_maze(maze_chamber) maze[1, 4] = mg.E # REMEMBER! Indexing is maze[y,x]!!! graph = mg.walls_to_graph(maze) entrance, chamber = mg.find_chamber(graph) assert entrance is None assert chamber == []
def test_find_one_dead_end(): # this maze has exactly one dead end at coordinate (1,1) maze_dead = """######## # # # # # # # ########""" maze = mg.str_to_maze(maze_dead) graph = mg.walls_to_graph(maze) width = maze.shape[1] dead_ends = mg.find_dead_ends(graph, width) assert len(dead_ends) == 1 assert dead_ends[0] == (1, 1)
def test_find_multiple_dead_ends_on_the_right(set_seed): # this maze has exactly three dead ends at coordinates (10,1), (10,5), (8,5) maze_dead = """############ # # # # # # # # # # # # # # # ############""" maze = mg.str_to_maze(maze_dead) graph = mg.walls_to_graph(maze) width = maze.shape[1] dead_ends = mg.find_dead_ends(graph, width) assert len(dead_ends) == 3 dead_ends.sort() assert dead_ends[2] == (10, 5) assert dead_ends[1] == (10, 1) assert dead_ends[0] == (8, 5)
def test_conversion_to_nx_graph(): maze_str = """################## # ## # # # # ### # # # # ## # # ## # # # # ### # # # # ## # ##################""" maze = mg.str_to_maze(maze_str) graph = mg.walls_to_graph(maze) # now derive a maze from a graph manually # - start with a maze full of walls newmaze = mg.empty_maze(maze.shape[0], maze.shape[1]) newmaze.fill(mg.W) # - loop through each node of the graph and remove a wall at the # corresponding coordinate for node in graph.nodes(): newmaze[node[1], node[0]] = mg.E assert np.all(maze == newmaze)
def test_remove_all_chambers(set_seed, maze_chamber): maze = mg.str_to_maze(maze_chamber) mg.remove_all_chambers(maze) # there are no more chambers if the connectivity of the graph is larger than 1 graph = mg.walls_to_graph(maze) assert nx.node_connectivity(graph) > 1