def binary_tree_maze(rows, cols, gif): grid = [[node(['L', 'R', 'T', 'B']) for j in range(cols)] for i in range(rows)] dirArr = [1, 3] #r,b x = random.randint(0, cols - 1) grid[0][x].walls[2] = 'X' gif_arr = [] if gif: maze = np.zeros(((2 * rows) + 1, (2 * cols) + 1), dtype=np.uint8) gif_arr.append(maze) util.mark_node((0, x * 2 + 1), gif_arr) for i in range(rows): for j in range(cols): if i == rows - 1: x = 0 elif j == cols - 1: x = 1 else: x = random.randint(0, 1) grid[i][j].walls[dirArr[x]] = 'X' if gif: if i == rows - 1 and j == cols - 1: util.mark_change(util.grid_to_image((i, j)), gif_arr, -1) continue util.mark_change(util.grid_to_image((i, j)), gif_arr, dirArr[x]) x = random.randint(0, cols - 1) grid[rows - 1][x].walls[3] = 'X' if gif: util.mark_node((rows * 2, x * 2 + 1), gif_arr) return gif_arr return grid
def simplified_random_prims(rows, cols, gif): #Top Entrance x = random.randint(0, cols - 1) grid = [[node(['L', 'R', 'T', 'B']) for j in range(cols)] for i in range(rows)] grid[0][x].walls[2] = 'X' gif_arr = [] if gif: maze = np.zeros(((2 * rows) + 1, (2 * cols) + 1), dtype=np.uint8) gif_arr.append(maze) util.mark_node((0, x * 2 + 1), gif_arr) #Select random cell r = random.randrange(rows) c = random.randrange(cols) adj_cells = {(r, c)} #Set containing neighboring cells while len(adj_cells): #Select random cell from adjacent list cell = random.sample(adj_cells, 1)[0] adj_cells.remove(cell) grid[cell[0]][cell[1]].visited = True #Randomly scan around to figure out what wall to tear down directions = ['R', 'B', 'T', 'L'] random.shuffle(directions) separate = True for dir in directions: nbr = util.nbr_index((cell[0], cell[1]), dir) if util.bounds_check(nbr, rows, cols): continue if grid[nbr[0]][nbr[1]].visited: #First time tear down wall if separate: wall_idx = util.conv_nbr_wall(util.conv_idx_dir(cell, nbr)) grid[cell[0]][cell[1]].walls[wall_idx] = 'X' separate = False if gif: util.mark_change(util.grid_to_image(cell), gif_arr, wall_idx) continue elif gif: util.mark_node(util.grid_to_image(cell), gif_arr) adj_cells.add(nbr) x = random.randint(0, cols - 1) grid[len(grid) - 1][x].walls[3] = 'X' if gif: util.mark_node((rows * 2, x * 2 + 1), gif_arr) return gif_arr return grid
def random_kruskals(rows, cols, gif): #1 - Add list of all walls wall_arr = [] for i in range(rows): for j in range(cols): for direction in ['R', 'B']: nbr = util.nbr_index((i, j), direction) if not util.bounds_check(nbr, rows, cols): wall_arr.append(wall((i, j), nbr)) cell_set = disjoint_set(rows * cols, cols) grid = [[node(['L', 'R', 'T', 'B']) for j in range(cols)] for i in range(rows)] x = random.randint(0, cols - 1) grid[0][x].walls[2] = 'X' gif_arr = [] if gif: maze = np.zeros(((2 * rows) + 1, (2 * cols) + 1), dtype=np.uint8) gif_arr.append(maze) util.mark_node((0, x * 2 + 1), gif_arr) #2 sequence = random.sample(range(len(wall_arr)), len(wall_arr)) for i in sequence: cellA = wall_arr[i].separate[0] cellB = wall_arr[i].separate[1] cell_indexA = cellA[0] * cols + cellA[1] cell_indexB = cellB[0] * cols + cellB[1] separateSets = cell_set.union(cell_indexA, cell_indexB) #1 if (separateSets): #1 #2 is done in cell_set.union wall_idx = util.conv_nbr_wall(util.conv_idx_dir(cellA, cellB)) grid[cellA[0]][cellA[1]].walls[wall_idx] = 'X' if gif: util.mark_change(util.grid_to_image(cellA), gif_arr, wall_idx, util.grid_to_image(cellB)) elif gif: util.mark_node(util.grid_to_image(cellA), gif_arr) x = random.randint(0, cols - 1) grid[len(grid) - 1][x].walls[3] = 'X' if gif: util.mark_node((rows * 2, x * 2 + 1), gif_arr) return gif_arr return grid
def aldousBroder(rows, cols, gif): grid = [[node((i, j), ['L', 'R', 'T', 'B'], False) for j in range(cols)] for i in range(rows)] x = random.randint(0, cols - 1) grid[0][x].walls[2] = 'X' neighbors = [] idx = util.convert_2d(random.randint(0, rows * cols - 1), cols) current = grid[idx[0]][idx[1]] current.visited = True unvisited = rows * cols - 1 gif_arr = [] if gif: maze = np.zeros(((2 * rows) + 1, (2 * cols) + 1), dtype=np.uint8) gif_arr.append(maze) util.mark_node((0, x * 2 + 1), gif_arr) previous = current while unvisited != 0: neighbors = util.getNeighbor(grid, current, rows, cols, previous) rndm_nbr = neighbors[random.randint(0, len(neighbors) - 1)] if gif: util.mark_node(util.grid_to_image(current.index), gif_arr, util.grid_to_image(rndm_nbr.index), 125) if not rndm_nbr.visited: wall_idx = util.conv_nbr_wall(util.conv_idx_dir(current.index, rndm_nbr.index)) grid[current.index[0]][current.index[1]].walls[wall_idx] = 'X' rndm_nbr.visited = True unvisited -= 1 if gif: util.mark_change(util.grid_to_image(current.index), gif_arr, wall_idx, util.grid_to_image(rndm_nbr.index)) elif gif: gif_arr.append(gif_arr[-2]) previous = current current = rndm_nbr x = random.randint(0, cols - 1) grid[rows - 1][x].walls[3] = 'X' if gif: util.mark_node((rows * 2, x * 2 + 1), gif_arr) return gif_arr return grid
def random_DFS(rows, cols, gif): stack = deque() grid = [[node((i, j), ['L', 'R', 'T', 'B'], False) for j in range(cols)] for i in range(rows)] x = random.randint(0, cols - 1) grid[0][x].visited = True grid[0][x].walls[2] = 'X' stack.append(grid[0][x]) gif_arr = [] if gif: maze = np.zeros(((2 * rows) + 1, (2 * cols) + 1), dtype=np.uint8) gif_arr.append(maze) util.mark_node((0, x * 2 + 1), gif_arr) while len(stack) != 0: curr = stack.pop() neighbors = util.neighborCheck(grid, curr, rows, cols) if len(neighbors) > 0: stack.append(curr) nbr_dir = neighbors[random.randint(0, len(neighbors) - 1)] new_index = util.nbr_index(curr.index, curr.walls[nbr_dir]) new_curr = grid[new_index[0]][new_index[1]] curr.walls[nbr_dir] = 'X' new_curr.visited = True stack.append(new_curr) if gif: util.mark_change(util.grid_to_image(curr.index), gif_arr, nbr_dir) elif gif: util.mark_node(util.grid_to_image(curr.index), gif_arr) x = random.randint(0, cols - 1) grid[rows - 1][x].walls[3] = 'X' if gif: util.mark_node((rows * 2, x * 2 + 1), gif_arr) return gif_arr return grid
def random_prims(rows, cols, gif): directions = ['L', 'R', 'T', 'B'] grid = [[node(['L', 'R', 'T', 'B']) for j in range(cols)] for i in range(rows)] r = random.randrange(rows) c = random.randrange(cols) grid[r][c].visited = True gif_arr = [] if gif: maze = np.zeros(((2 * rows) + 1, (2 * cols) + 1), dtype=np.uint8) gif_arr.append(maze) #Set of tuples of tuples --> cell 1 and neighbor walls_list = set() for dir in directions: nbr = util.nbr_index((r, c), dir) if not util.bounds_check(nbr, rows, cols): walls_list.add(((r, c), nbr)) while len(walls_list): #Pick random wall wall = random.choice(tuple(walls_list)) walls_list.remove(wall) cellA = wall[0] cellB = wall[1] if not grid[cellB[0]][cellB[1]].visited: #Other cell not visited yet --> Tear down wall grid[cellB[0]][cellB[1]].visited = True dir = util.conv_idx_dir(cellA, cellB) wall_idx = util.conv_nbr_wall(dir) grid[cellA[0]][cellA[1]].walls[wall_idx] = 'X' if gif: util.mark_change(util.grid_to_image(cellA), gif_arr, wall_idx, util.grid_to_image(cellB)) for dir in directions: #Add unvisited neighbor walls to set nbr = util.nbr_index(cellB, dir) if not util.bounds_check( nbr, rows, cols) and not grid[nbr[0]][nbr[1]].visited: walls_list.add((cellB, nbr)) x = random.randint(0, cols - 1) grid[0][x].walls[2] = 'X' y = random.randint(0, cols - 1) grid[rows - 1][y].walls[3] = 'X' if gif: newIMG = util.create_snapshot(gif_arr[-1].copy(), (0, 2 * x + 1), -1) newIMG = util.create_snapshot(newIMG, (rows * 2, 2 * y + 1), -1) gif_arr.append(newIMG) return gif_arr return grid