def walk(grid, cur_cell, gif): #Check neighbors grid[cur_cell[0]][cur_cell[1]].visited = True valid_nbrs = [] for dir in Directions: idx = util.nbr_index(cur_cell, dir) if(not util.bounds_check(idx, Rows, Cols) and not grid[idx[0]][idx[1]].visited): #unvisited neighbor valid_nbrs.append((idx, dir)) if(len(valid_nbrs) == 0): if gif: newIMG = util.create_snapshot(gif_arr[-1].copy(), (cur_cell[0] * 2 + 1, cur_cell[1] * 2 + 1), -1) gif_arr.append(newIMG) return #Select random valid neighbor to walk to next x = random.randint(0, len(valid_nbrs) - 1) dir = valid_nbrs[x][1] wall_idx = local_wall_idx(dir) if gif: newIMG = util.create_snapshot(gif_arr[-1].copy(), (cur_cell[0] * 2 + 1, cur_cell[1] * 2 + 1), wall_idx) gif_arr.append(newIMG) grid[cur_cell[0]][cur_cell[1]].walls[wall_idx] = 'X' walk(grid, valid_nbrs[x][0], gif)
def hunt(grid, gif): for r in range(Rows): for c in range(Cols): if(not grid[r][c].visited): #Found cell not already in maze valid_nbrs = [] for dir in Directions: idx = util.nbr_index((r,c), dir) if not util.bounds_check(idx, Rows, Cols) and grid[idx[0]][idx[1]].visited: #valid neighbor that is part of maze valid_nbrs.append(dir) if len(valid_nbrs): #Choose random neighbor in maze to connect to x = random.randint(0, len(valid_nbrs) - 1) wall_idx = local_wall_idx(valid_nbrs[x]) if gif: newIMG = util.create_snapshot(gif_arr[-1].copy(), (r * 2 + 1, c * 2 + 1), wall_idx) gif_arr.append(newIMG) grid[r][c].walls[wall_idx] = 'X' return (r,c) return None
def hunt_and_kill(rows, cols, gif): grid = [[node(['L','R','T','B']) for j in range(cols)] for i in range(rows)] global Directions Directions = ['L','R','T','B'] global Rows Rows = rows global Cols Cols = cols #Choose random start, perform random walk until no unvisited neighbors of current cell --> Deadend #Then Enter Hunt Mode and find first unvisited cell with visited neighbors #Carve a passage from that cell to a random visited neigbor and begin a new walk rand_r = random.randint(0,rows-1) rand_c = random.randint(0,cols-1) cell = (rand_r, rand_c) global gif_arr gif_arr = [] if gif: maze = np.zeros(((2 * rows) + 1, (2 * cols) + 1), dtype=np.uint8) gif_arr.append(maze) newIMG = util.create_snapshot(gif_arr[-1], (rand_r * 2 + 1, rand_c * 2 + 1), -1) gif_arr.append(newIMG) while cell != None: walk(grid, cell, gif) cell = hunt(grid, gif) x = random.randint(0, cols - 1) grid[0][x].walls[2] = 'X' y = random.randint(0, cols - 1) grid[len(grid) - 1][y].walls[3] = 'X' if gif: newIMG = util.create_snapshot(gif_arr[-1].copy(), (0, x * 2 + 1), -1) newIMG = util.create_snapshot(newIMG, (rows * 2, y * 2 + 1), -1) gif_arr.append(newIMG) return gif_arr return grid
def sidewinder_gif(rows, cols): gif_arr = [] maze = np.zeros(((2 * rows) + 1, (2 * cols) + 1), dtype=np.uint8) gif_arr.append(maze) for c in range(cols - 1): newIMG = util.create_snapshot(gif_arr[-1].copy(), (1, c * 2 + 1), 1) gif_arr.append(newIMG) newIMG = util.create_snapshot(gif_arr[-1],(1, (cols-1) * 2 + 1), -1) for r in range(1, rows): run_start = 0 for c in range(cols): #Cur cell is (r,c) #Carve east if 1 and not last column carve_east = random.randint(0, 1) if (carve_east and c != cols - 1): newIMG = util.create_snapshot(gif_arr[-1].copy(), (r * 2 + 1, c * 2 + 1),1) gif_arr.append(newIMG) else: col = random.randint(run_start, c) newIMG = util.create_snapshot(gif_arr[-1].copy(), (r * 2 +1, col * 2 + 1),2) gif_arr.append(newIMG) newIMG = util.create_snapshot(gif_arr[-1], (r*2 + 1, c*2 + 1), -1) run_start = c + 1 x = random.randint(0, cols - 1) y = random.randint(0, cols - 1) newIMG = util.create_snapshot(gif_arr[-1].copy(), (0, x *2 + 1), -1) gif_arr.append(newIMG) newIMG = util.create_snapshot(gif_arr[-1], (cols*2, y *2 + 1), -1) return gif_arr
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