我正在使用链接中的Python迷宫生成器代码(也可以在下面看到):https://rosettacode.org/wiki/Maze_generation
当我将迷宫转换为高度图时,我正在尝试增加迷宫走廊的宽度。然而,走廊太窄,无法让我的角色进入迷宫。
我尝试更改下面代码的一些设置,但无法在不使迷宫行为异常的情况下获得所需的结果。
def make_maze(w = 16, h = 8):
vis = [[0] * w + [1] for _ in range(h)] + [[1] * (w + 1)]
ver = [["| "] * w + ['|'] for _ in range(h)] + [[]]
hor = [["+--"] * w + ['+'] for _ in range(h + 1)]
def walk(x, y):
vis[y][x] = 1
d = [(x - 1, y), (x, y + 1), (x + 1, y), (x, y - 1)]
shuffle(d)
for (xx, yy) in d:
if vis[yy][xx]: continue
if xx == x: hor[max(y, yy)][x] = "+ "
if yy == y: ver[y][max(x, xx)] = " "
walk(xx, yy)
walk(randrange(w), randrange(h))
s = ""
for (a, b) in zip(hor, ver):
s += ''.join(a + ['\n'] + b + ['\n'])
return s
最佳答案
提供的代码是硬编码的;此外,内存中的迷宫表示与迷宫生成和迷宫视觉输出混合在一起,使其相当不灵活且难以修改。将该迷宫与搜索算法结合使用的可能性也很小。
这个迷宫也缺少入口和导出。
尽管如此,我还是添加了几个参数,允许在水平和垂直方向上对迷宫走廊进行一定程度的缩放。
更进一步,需要重构生成器,将生成与内存中的表示、图形输出分开,并允许表示穿过迷宫的各种路径。
import random
def make_maze(w = 16, h = 8, scale=0):
h0, h1, h2, h3 = "+--", "+ ", "| ", " "
h0 += scale * '----'
h1 += scale * ' '
h2 += scale * ' '
h3 += scale * ' '
vis = [[0] * w + [1] for _ in range(h)] + [[1] * (w + 1)]
ver = [[h2] * w + ['|'] for _ in range(h)] + [[]]
hor = [[h0] * w + ['+'] for _ in range(h + 1)]
def walk(x, y):
vis[y][x] = 1
d = [(x - 1, y), (x, y + 1), (x + 1, y), (x, y - 1)]
random.shuffle(d)
for (xx, yy) in d:
if vis[yy][xx]: continue
if xx == x: hor[max(y, yy)][x] = h1
if yy == y: ver[y][max(x, xx)] = h3
walk(xx, yy)
walk(random.randrange(w), random.randrange(h))
s = ""
for (a, b) in zip(hor, ver):
s += ''.join(a + ['\n'] + b + ['\n'])
for _ in range(scale):
s += ''.join(b + ['\n'])
return s
print(make_maze(scale=0))
print('\n\n')
print(make_maze(scale=1))
print('\n\n')
print(make_maze(scale=2))
print('\n\n')
print(make_maze(scale=3))
print('\n\n')
输出迷宫规模:0:
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| | | | |
+ +--+--+ + + +--+ + +--+--+ + + +--+--+
| | | | | | | | | | | | |
+ + + + +--+ + +--+ + + + + +--+ + +
| | | | | | | | | | | |
+--+--+ +--+ +--+ + +--+--+ + +--+ + + +
| | | | | | | | | | | |
+ +--+--+ + + + + +--+ + +--+ + + + +
| | | | | | | | | | | | |
+ + + +--+--+--+ + + +--+--+ +--+ + + +
| | | | | | | | |
+--+ +--+--+--+--+--+ + + + +--+ + +--+ +
| | | | | | | | | |
+ +--+--+ +--+--+ +--+ + + + +--+--+ + +
| | | | |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
输出迷宫比例:1:
+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+
| | | | |
| | | | |
+ +------+ +------+ + + + +------+ + +------+ +------+------+------+
| | | | | | | | | |
| | | | | | | | | |
+------+ +------+------+------+------+ +------+ + +------+ +------+ +------+ +
| | | | | | | | |
| | | | | | | | |
+ + + +------+------+ + +------+ +------+ +------+ +------+ + +
| | | | | | | | | |
| | | | | | | | | |
+ +------+------+------+ +------+------+ +------+ +------+------+------+------+ + +
| | | | | | |
| | | | | | |
+ + +------+ +------+------+ +------+ +------+------+------+------+------+------+ +
| | | | | | |
| | | | | | |
+ +------+ +------+ +------+------+ +------+ +------+------+------+------+ + +
| | | | | | | | | | |
| | | | | | | | | | |
+ + + + +------+------+ +------+------+------+ + +------+ + + +
| | | | |
| | | | |
+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+
输出迷宫规模:2:
+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+
| | | | | |
| | | | | |
| | | | | |
+ + + +----------+ +----------+ + +----------+----------+----------+ + +----------+----------+ +
| | | | | | | |
| | | | | | | |
| | | | | | | |
+ +----------+----------+ +----------+ +----------+----------+ +----------+----------+----------+----------+----------+ + +
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
+----------+----------+ +----------+----------+----------+ +----------+ + + + + + +----------+ +
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
+ + + +----------+----------+----------+----------+ +----------+ +----------+----------+----------+----------+ + +
| | | | | | | |
| | | | | | | |
| | | | | | | |
+ +----------+----------+ +----------+ +----------+----------+ + +----------+----------+----------+ + + +
| | | | | | | | | | |
| | | | | | | | | | |
| | | | | | | | | | |
+ +----------+ +----------+ +----------+ +----------+----------+----------+----------+ + + + + +
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
+ + +----------+ +----------+ +----------+----------+----------+----------+----------+----------+ + + + +
| | | | |
| | | | |
| | | | |
+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+
输出迷宫规模:3:
+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
| | |
| | |
| | |
| | |
+ +--------------+--------------+--------------+--------------+ +--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+ + +
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
+--------------+--------------+ + + +--------------+ + + +--------------+ + + +--------------+--------------+ +
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
+--------------+--------------+--------------+--------------+--------------+ +--------------+ +--------------+--------------+ + +--------------+ + + +
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
+ + +--------------+--------------+ +--------------+--------------+ + +--------------+--------------+ + +--------------+--------------+--------------+
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
+ +--------------+--------------+ +--------------+--------------+ +--------------+--------------+--------------+--------------+ +--------------+--------------+ + +
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
+--------------+--------------+ + + + +--------------+ + +--------------+ +--------------+--------------+--------------+--------------+ +
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
+ +--------------+--------------+--------------+--------------+ + +--------------+--------------+ +--------------+--------------+--------------+--------------+--------------+--------------+
| | |
| | |
| | |
| | |
+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
关于python-3.x - 如何增加迷宫中的走廊宽度?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56918471/