1
我試圖編程一個隨機迷宮生成器,我試圖修改代碼here。我的問題是,我需要將牆壁表示爲'1',並將開放式走道表示爲'0',我已經玩過一次,似乎無法找到解決方案。任何幫助大量讚賞。Python代碼修改
下面是完成
from random import shuffle, randrange
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
if __name__ == '__main__':
print(make_maze())
到目前爲止您嘗試了什麼?乍一看,我首先修改'ver'和'hor',(如'ver = [['100'] * w + ['1']','hor = [[「111」] * w + ['1']代表範圍(h + 1)]中的_ – cowbert