def aldous_broder(maze): """Complete the maze using the Aldous-Broder algorithm. (If the starting maze has no passages, then this algorithm will produce all possible mazes with equal probability.) """ while maze.has_empty_cells(): d = random_direction() if maze.cell_is_empty(d): maze.carve(d) else: maze.move(d) return maze
def _lerw(maze, c, stopping_set): """Perform a loop-erased random walk from starting position c until an element of stopping_set is hit. """ path = [c] path_indices_by_cell = {c: [0]} maze.move(*c) while maze.cursor_cell() not in stopping_set: if maze.move(random_direction()): c = maze.cursor_cell() if c in path_indices_by_cell and path_indices_by_cell[c]: prev_index = path_indices_by_cell[c][-1] for d in path[(prev_index + 1):]: path_indices_by_cell[d].pop() path = path[:(prev_index + 1)] else: path_indices_by_cell.setdefault(c, []).append(len(path)) path.append(c) return path