Python代码修改

时间:2017-10-04 18:08:20

标签: python maze

我正在尝试编写随机迷宫生成器,并且我试图修改提到的here代码。我的问题是我需要将墙壁表示为“1' 1'并且开放的走道被表示为' 0',我有一个游戏,我似乎无法找到解决方案。任何帮助都非常感激。

这是完成代码

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())

1 个答案:

答案 0 :(得分:0)

从代码看起来,0表示开放空格。 只需将空格替换为0,将其余部分替换为1(在单引号内找到文本)。