gpt4 book ai didi

python - 没有死胡同的迷宫 python

转载 作者:太空宇宙 更新时间:2023-11-03 19:03:17 26 4
gpt4 key购买 nike

我有一个函数可以生成一个随机迷宫,给定行和列的数字,所有东西都雕刻得非常好......我想做的是删除/移除迷宫内可能有和封闭的任何墙壁,这样迷宫没有“死胡同”。我已经尝试了以下方法,但似乎不起作用...任何人都知道我可能出错的地方

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
|_________|

最佳答案

做完之后 walls.remove(z[0])

你的墙壁数组现在只包含通往死胡同的墙壁的方向。但随后您不会编辑迷宫来移除剩余的一堵墙。

此外,您应该在初始化 z 后立即进行检查,以确保 len(z) == 1 - 如果没有,continue到下一个单元格。这将节省处理时间。

关于python - 没有死胡同的迷宫 python ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15514137/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com