def creator(): """create a maze""" bias = params["bias"] tweak = params["tweak"] no_revisit = params["visit"] print(".", end="", flush=True) grid = Rectangular_Grid(rows, cols) if tweak is "straight": Weighted_Aldous_Broder.straight_on(grid, p=bias, \ no_revisit=no_revisit) elif tweak is "right": Weighted_Aldous_Broder.right_on(grid, p=bias, \ no_revisit=no_revisit) elif tweak is "left": Weighted_Aldous_Broder.left_on(grid, p=bias, \ no_revisit=no_revisit) elif tweak is "turn": Weighted_Aldous_Broder.turn_on(grid, p=bias, \ no_revisit=no_revisit) else: # tweak is "magnet" if not no_revisit: bias /= 6 Weighted_Aldous_Broder.magnet_on(grid, "east", p=bias, \ no_revisit=no_revisit) return grid
def carve(self, shape): """carve a maze in the given partition This method assumes that partitioning is into rectangles with sides parallel to the axes. Parameters: shape - a rectangle specified by (row,column) indices in format [[r0,c0],[r1,c1]] where (r0,c0) is the lower left corner and (r1,c1) is the upper right corner. """ [[r0, c0], [r1, c1]] = shape shadow_maze = Rectangular_Grid(r1 - r0 + 1, c1 - c0 + 1) self.carve_shadow(shadow_maze) # get the edges in the shadow edges = {} for i in range(r0, r1 + 1): for j in range(c0, c1 + 1): cell = shadow_maze[i - r0, j - c0] for nbr in cell.passages(): edge = frozenset([cell, nbr]) edges[edge] = 1 # carve the shadowed edges in the grid for edge in edges: cell, nbr = edge i, j = cell.index h, k = nbr.index cell = self.grid[i + r0, j + c0] nbr = self.grid[h + r0, k + c0] cell.makePassage(nbr)
def make_maze(m, n, maze_name, nonrandom): """create a maze""" grid = Rectangular_Grid(m, n, name=maze_name) if nonrandom: # deterministic algorithm DFS.deterministic_on(grid, start=grid[m-1,n-1]) else: # randomized algorithm DFS.on(grid) return grid
def make_maze(m, n, maze_name, which): """create a maze""" grid = Rectangular_Grid(m, n, name=maze_name) if which == "BFS": Tree_Search.bfs_on(grid) else: Tree_Search.heap_on(grid) return grid
def make_grid(m, n): """create a rectangular grid Arguments: m, n - respectively the number of rows and columns""" from rectangular_grid import Rectangular_Grid print("Grid: Rectangular_Grid(%d, %d)" % (m, n)) grid = Rectangular_Grid(m, n) return grid
def make_square_grid(m, n, inset=0.15): """create an ordinary rectangular grid Arguments: m, n - respectively the number of rows and columns""" from rectangular_grid import Rectangular_Grid print("Grid: Rectangular_Grid(%d, %d)" % (m, n)) grid = Rectangular_Grid(m, n, inset=inset) return grid
def make_rectangular_maze(m, n, maze_name): """create a maze""" from rectangular_grid import Rectangular_Grid from hunt_and_kill import Hunt_and_Kill print("Create grid %s - %d rows, %d columns" % (maze_name, m, n)) grid = Rectangular_Grid(m, n, name=maze_name) print("Carve maze %s - hunt and kill algorithm" % maze_name) Hunt_and_Kill.on(grid) return grid
def make_mask(pathname, debug=False): from rectangular_grid import Rectangular_Grid mask = Mask() if debug: print('make_mask: Creating mask from "' + pathname + '"...') n = 0 lines = [] with open(pathname) as fp: for line in fp: if len(line) < 2: continue if line[0] == "#": if debug: print(line[:-1]) continue lines.append(line[:-1]) n2 = len(line) - 1 n = max(n, n2) lines.reverse() m = len(lines) for i in range(m): line = lines[i] n2 = len(line) if n2 < n: line += ' ' * (n-n2) for j in range(n): if line[j] not in ['x', 'X']: mask[i, j] = True if debug: print('Mask: m=%d, n=%d, enabled=%d' % (m, n, len(mask))) print('Grid: creating rectangular grid') if m * n is 0: raise ValueError('m=%d, n=%d: empty grid' % (m, n)) grid = Rectangular_Grid(m, n) if debug: print('Masked_Grid: creating masked grid') masked = Masked_Grid(grid, mask) if debug: print('make_mask: complete') return grid, masked
def create_grid(args): """create the grid""" from rectangular_grid import Rectangular_Grid from weave_grid import Preweave_Grid from multilevel_grid import Multilevel_Grid subgrids = [] m, n = args.dim for level in range(args.levels): subgrid = Preweave_Grid(m, n) if args.weave \ else Rectangular_Grid(m, n, inset=0.15) subgrids.append(subgrid) subgrid.name = "Floor #" + str(level) if level \ else "Ground Floor" grid = Multilevel_Grid() for subgrid in subgrids: grid.add_grid(subgrid) return grid
def main(m, n, inset): """create an inset maze""" print("create grid: Rectangular_Grid(%d, %d, inset=%f)" % (m, n, inset)) grid = Rectangular_Grid(m, n, inset=inset) print("Create perfect maze using randomized DFS...") DFS.on(grid) layout = Color_Layout(grid, plt, title="Inset Maze Demonstration") print("Creating color palette...") layout.palette[0] = 'violet' layout.palette[1] = 'powderblue' layout.palette[2] = 'red' layout.palette[3] = 'green' for i in range(m): for j in range(n): layout.color[grid[i, j]] = (i + j) % 2 layout.color[grid[0, 0]] = 2 layout.color[grid[m - 1, n - 1]] = 3 print("Plotting...") layout.draw_grid() layout.render("demos/inset_demo.png")
def make_grid(m, n, maze_name, inset): """create a grid""" print("%s: Rectangular_Grid(%d, %d)" % (maze_name, m, n)) from rectangular_grid import Rectangular_Grid grid = Rectangular_Grid(m, n, name=maze_name, inset=inset) return grid
[1] Jamis Buck. Mazes for Programmers. 2015 (Pragmatic Bookshelf). Book (978-1-68050-055-4). """ from rectangular_grid import Rectangular_Grid from binary_tree import Binary_Tree from sidewinder import Sidewinder from plotter_graphviz import GraphViz_Plotter as Plot1 from plotter_graphviz import GraphViz_Plotter_Rectangular as Plot2 # PASSAGE CARVER m=5 n=7 grid = Rectangular_Grid(m, n, name="BinaryTree1") Binary_Tree.on(grid) source = grid[m-1, n-1] # start cell terminus = grid[0, 0] # finish cell source.kwargs["content"] = "S" terminus.kwargs["content"] = "T" print(grid.unicode()) # 1. tree structure of maze unravelled # (crude but revealing) pathname = "demos/graphviz1" print("saving file %s.dot" % pathname)
def creator(): """create a maze""" print(".", end="", flush=True) grid = Rectangular_Grid(rows, cols) Tree_Search.bfs_on(grid) return grid
[1] Jamis Buck. Mazes for Programmers. 2015 (Pragmatic Bookshelf). Book (978-1-68050-055-4). """ from rectangular_grid import Rectangular_Grid from binary_tree import Binary_Tree from norms import distances from layout_graphviz import Layout # PASSAGE CARVER m=5 n=7 grid = Rectangular_Grid(m, n, name="BinaryTree1") Binary_Tree.on(grid) source = grid[m-1, n-1] # start cell terminus = grid[0, 0] # finish cell norms = distances(terminus) for cell in norms.metrics: cell.kwargs["content"] = "%d" % (norms.metrics[cell] % 10) source.kwargs["content"] = "S" terminus.kwargs["content"] = "T" print(grid.unicode()) # TEST # 1 - RAW LAYOUT
def creator(): """create a maze""" print(".", end="", flush=True) grid = Rectangular_Grid(rows, cols) Aldous_Broder.on(grid) return grid
def creator(): """create a maze""" print(".", end="", flush=True) grid = Rectangular_Grid(rows, cols) Recursive_Backtracker.on(grid) return grid
def make_2D_grid(m, n, maze_name): """create a rectangular grid""" grid = Rectangular_Grid(m, n, name=maze_name, inset=0.1) return grid
def make_grid(): """create a rectangular grid""" grid = Rectangular_Grid(4, 5) return grid
def creator(): """create a maze""" print(".", end="", flush=True) grid = Rectangular_Grid(rows, cols) ABW.hybrid_on(grid) return grid
def creator(): """create a maze""" print(".", end="", flush=True) grid = Rectangular_Grid(rows, cols) Hunt_and_Kill.on(grid) return grid
def creator(): """create a maze""" print(".", end="", flush=True) grid = Rectangular_Grid(rows, cols) Binary_Tree.on(grid) return grid