如何增加迷宫中走廊的宽度?
How can I increase corridor width in a maze?
我正在使用来自 link 的 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:
+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
| | |
| | |
| | |
| | |
+ +--------------+--------------+--------------+--------------+ +--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+ + +
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
+--------------+--------------+ + + +--------------+ + + +--------------+ + + +--------------+--------------+ +
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
+--------------+--------------+--------------+--------------+--------------+ +--------------+ +--------------+--------------+ + +--------------+ + + +
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
+ + +--------------+--------------+ +--------------+--------------+ + +--------------+--------------+ + +--------------+--------------+--------------+
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
+ +--------------+--------------+ +--------------+--------------+ +--------------+--------------+--------------+--------------+ +--------------+--------------+ + +
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
+--------------+--------------+ + + + +--------------+ + +--------------+ +--------------+--------------+--------------+--------------+ +
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
+ +--------------+--------------+--------------+--------------+ + +--------------+--------------+ +--------------+--------------+--------------+--------------+--------------+--------------+
| | |
| | |
| | |
| | |
+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
我正在使用来自 link 的 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:
+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+
| | |
| | |
| | |
| | |
+ +--------------+--------------+--------------+--------------+ +--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+ + +
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
+--------------+--------------+ + + +--------------+ + + +--------------+ + + +--------------+--------------+ +
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
| | | | | | | | | |
+--------------+--------------+--------------+--------------+--------------+ +--------------+ +--------------+--------------+ + +--------------+ + + +
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
+ + +--------------+--------------+ +--------------+--------------+ + +--------------+--------------+ + +--------------+--------------+--------------+
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
+ +--------------+--------------+ +--------------+--------------+ +--------------+--------------+--------------+--------------+ +--------------+--------------+ + +
| | | | | | | |
| | | | | | | |
| | | | | | | |
| | | | | | | |
+--------------+--------------+ + + + +--------------+ + +--------------+ +--------------+--------------+--------------+--------------+ +
| | | | | | |
| | | | | | |
| | | | | | |
| | | | | | |
+ +--------------+--------------+--------------+--------------+ + +--------------+--------------+ +--------------+--------------+--------------+--------------+--------------+--------------+
| | |
| | |
| | |
| | |
+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+--------------+