我正在从此处的链接中使用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
答案 0 :(得分:2)
提供的代码是硬编码的;此外,内存中的迷宫表示与迷宫生成和迷宫视觉输出混合在一起,使其相当不灵活且难以修改。将这种迷宫与搜索算法结合使用的可能性也很小。
这个迷宫也缺少入口和出口。
尽管如此,我还是添加了几个参数,这些参数可以对水平和垂直方向上的迷宫走廊进行一定程度的缩放。
进一步发展将需要对生成器进行重构,以将生成与内存中的表示形式与图形输出分离开来,并允许通过迷宫的可变路径表示形式。
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')
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
| | | | |
+ +--+--+ + + +--+ + +--+--+ + + +--+--+
| | | | | | | | | | | | |
+ + + + +--+ + +--+ + + + + +--+ + +
| | | | | | | | | | | |
+--+--+ +--+ +--+ + +--+--+ + +--+ + + +
| | | | | | | | | | | |
+ +--+--+ + + + + +--+ + +--+ + + + +
| | | | | | | | | | | | |
+ + + +--+--+--+ + + +--+--+ +--+ + + +
| | | | | | | | |
+--+ +--+--+--+--+--+ + + + +--+ + +--+ +
| | | | | | | | | |
+ +--+--+ +--+--+ +--+ + + + +--+--+ + +
| | | | |
+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+--+
+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+
| | | | |
| | | | |
+ +------+ +------+ + + + +------+ + +------+ +------+------+------+
| | | | | | | | | |
| | | | | | | | | |
+------+ +------+------+------+------+ +------+ + +------+ +------+ +------+ +
| | | | | | | | |
| | | | | | | | |
+ + + +------+------+ + +------+ +------+ +------+ +------+ + +
| | | | | | | | | |
| | | | | | | | | |
+ +------+------+------+ +------+------+ +------+ +------+------+------+------+ + +
| | | | | | |
| | | | | | |
+ + +------+ +------+------+ +------+ +------+------+------+------+------+------+ +
| | | | | | |
| | | | | | |
+ +------+ +------+ +------+------+ +------+ +------+------+------+------+ + +
| | | | | | | | | | |
| | | | | | | | | | |
+ + + + +------+------+ +------+------+------+ + +------+ + + +
| | | | |
| | | | |
+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+------+
+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+
| | | | | |
| | | | | |
| | | | | |
+ + + +----------+ +----------+ + +----------+----------+----------+ + +----------+----------+ +
| | | | | | | |
| | | | | | | |
| | | | | | | |
+ +----------+----------+ +----------+ +----------+----------+ +----------+----------+----------+----------+----------+ + +
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
+----------+----------+ +----------+----------+----------+ +----------+ + + + + + +----------+ +
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
+ + + +----------+----------+----------+----------+ +----------+ +----------+----------+----------+----------+ + +
| | | | | | | |
| | | | | | | |
| | | | | | | |
+ +----------+----------+ +----------+ +----------+----------+ + +----------+----------+----------+ + + +
| | | | | | | | | | |
| | | | | | | | | | |
| | | | | | | | | | |
+ +----------+ +----------+ +----------+ +----------+----------+----------+----------+ + + + + +
| | | | | | | | |
| | | | | | | | |
| | | | | | | | |
+ + +----------+ +----------+ +----------+----------+----------+----------+----------+----------+ + + + +
| | | | |
| | | | |
| | | | |
+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+----------+
+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
| | |
| | |
| | |
| | |
+ +--------------+--------------+--------------+--------------+ +--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+ + +
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
+--------------+--------------+ + + +--------------+ + + +--------------+ + + +--------------+--------------+ +
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
+--------------+--------------+--------------+--------------+--------------+ +--------------+ +--------------+--------------+ + +--------------+ + + +
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
+ + +--------------+--------------+ +--------------+--------------+ + +--------------+--------------+ + +--------------+--------------+--------------+
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
+ +--------------+--------------+ +--------------+--------------+ +--------------+--------------+--------------+--------------+ +--------------+--------------+ + +
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
+--------------+--------------+ + + + +--------------+ + +--------------+ +--------------+--------------+--------------+--------------+ +
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
+ +--------------+--------------+--------------+--------------+ + +--------------+--------------+ +--------------+--------------+--------------+--------------+--------------+--------------+
| | |
| | |
| | |
| | |
+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+