def generic_maze(**kwargs): supershape = kwargs.pop('supershape', None) grid = pmz.PolyGrid(supershape=supershape) if 'complexity' not in kwargs: kwargs['complexity'] = .5 # speed things up when possible grid.create_rectangle(**kwargs) return pmz.Maze(grid)
def test_image_returns_None_for_empty_grid(self): # make and confirm an empty grid empty_grid = pmz.PolyGrid() self.assertEqual(len(tuple(empty_grid.shapes())), 0) # confirm image is None maze = pmz.Maze(empty_grid) self.assertIsNone(maze.image())
def generic_shape(supershape=None, index=None): # provide test defaults grid = pmz.PolyGrid(supershape=supershape) index = index or (1, 2) # get a shape shape = grid.create(index) return shape
def supershape_with_neighbors(ss): """Make a neighborhood of all component shapes + all border shapes.""" grid = pmz.PolyGrid(supershape=ss) for comp_index, comp_data in ss.components().items(): for n_index in comp_data['edges']: if grid.get(n_index) is None: grid.create(n_index) return grid
def image_maze(): image = PIL.Image.open(os.path.join(_image_directory, 'globe_source.png')) supershape_name, supershape = next(_supershapes_cycle) grid = pmz.PolyGrid(supershape=supershape) grid.create_from_image(image, complexity=100) maze = pmz.Maze(grid) filename = os.path.join(_image_directory, 'Globe ({}).png'.format(supershape_name)) maze.image().save(filename, format='PNG') print('Saved {}'.format(filename))
def unicode_string_maze(): s = u'迷\\n路' # note literal \n is interpreted as newline supershape_name, supershape = next(_supershapes_cycle) grid = pmz.PolyGrid(supershape=supershape) grid.create_string(s, complexity=20) maze = pmz.Maze(grid) filename = os.path.join(_image_directory, 'Unicode String ({}).png'.format(supershape_name)) maze.image().save(filename, format='PNG') print('Saved {}'.format(filename))
def ascii_string_maze(): s = 'Mazes.' supershape_name, supershape = next(_supershapes_cycle) grid = pmz.PolyGrid(supershape=supershape) grid.create_string(s, height=50) maze = pmz.Maze(grid) filename = os.path.join(_image_directory, 'ASCII String ({}).png'.format(supershape_name)) maze.image().save(filename, format='PNG') print('Saved {}'.format(filename))
def generic_grid(supershape=None, neighborhood_center_index=None): grid = pmz.PolyGrid(supershape=supershape) if neighborhood_center_index: # create a neighborhood based on whatever creator is being used # the neighborhood is defined as a central shape with a neighbor # on each edge center_shape = grid.create(neighborhood_center_index) for n_index in center_shape.n_indexes(): grid.create(n_index) return grid
def custom_rectangle_mazes(): aspect_close_to_golden_rectangle = 0.625 for complexity in (1, 1.5, 2, 4): supershape_name, supershape = next(_supershapes_cycle) grid = pmz.PolyGrid(supershape=supershape) grid.create_rectangle(complexity=complexity, aspect=aspect_close_to_golden_rectangle) maze = pmz.Maze(grid) filename = os.path.join( _image_directory, 'Rectangle (Complexity {}) ({}).png' ''.format(complexity, supershape_name)) maze.image().save(filename, format='PNG') print('Saved {}'.format(filename))
def test_mazify_grid_eliminates_isolated_shapes(self): grid = pmz.PolyGrid() neighbor_1_index = (1, 1) # any index isolated_index = (30, 30) # no way these are connected # create the neighbors and the isolated shape grid.create(isolated_index) neighbor_1 = grid.create(neighbor_1_index) neighbor_2_index, _ = tuple(neighbor_1.neighbors())[0] neighbor_2 = grid.create(neighbor_2_index) # mazify and confirm the isolated shape has been removed pmz.Maze(grid) final_shapes = tuple(grid.shapes()) final_shapes_spec = (neighbor_1, neighbor_2) _assertCountEqual(self, final_shapes, final_shapes_spec)
def test_produces_an_empty_grid(self): grid = pmz.PolyGrid() self.assertEqual(len(tuple(grid.shapes())), 0)
def make_maze(complexity): grid = pmz.PolyGrid(supershape=preloaded_shape) grid.create_rectangle(complexity=complexity) pmz.Maze(grid)