我有一个函数可以生成一个随机迷宫,给定行数和列数,一切都被完美地雕刻出来......我想做的是删除/移除迷宫内可能有的任何墙壁和外壳,以便迷宫没有“死胡同”。我已经尝试了以下,但似乎它不起作用......任何人都知道我可能哪里出错了
def random_maze_without_deadends(row,cols):
maze = random_maze(row,cols) #this will generate a random maze and carve out a maze where all cells are defaulted to no value
for i in xrange(row):
for j in xrange(cols):
z = maze.open_directions(i,j) # assume maze.open_direction open's up the maze by Returning a list of open (non-wall) directions from a cell given by row column
walls = ['N','S','W','E'] #preassigned values for north south west and east respectively to check open 'walls' of cells
if i == 0:
walls.remove('N')
if i == i -1:
walls.remove('S')
if j == 0:
walls.remove('W')
if j == j-1:
walls.remove('E')
if len(z) == 1:
walls.remove(z[0])
return maze
以下是前面的代码也使用的:
class MazeCell:
def __init__(self, r, c):
self.row = r
self.col = c
self.directions = ["N", "S", "E", "W"]
random.shuffle(self.directions)
def random_maze(rows,cols):
maze = Maze(rows, cols)
carve_passages_stack(maze)
return maze
我的主要问题基本上是 deadend 函数的逻辑有什么问题?感谢您试图弄清楚我的意思。
更新-这是我当前的输出:
_________
| | _ | |
| |_ | | |
|_____| | |
| __x|_ | <---This part should get opened up---where the x is as north south and east are clos
|_________|