随机生成的迷宫

时间:2018-04-27 09:54:55

标签: python maze

我正在尝试创建一个符合非常具体标准的随机生成的迷宫: 我希望它在2D列表中,其中墙是假的,而且段落是真的 从x点到y点应该只有一个溃败。 这就是我到目前为止所拥有的:

def generate(height,width):
    maze_lst=[]
    theight = height*2+1
    twidth = width*2+1
    maze_lst = gen_base(theight, twidth)
    maze_lst = entrance(theight, twidth, maze_lst)
    #maze_lst = route(theght, twidth, maze_lst)
    return maze_lst

def gen_base(height, width):
    maze_lst = []
    for i in range(height):
        maze_lst.append([])
        for j in range(width):
            if int(i/2) == i/2:
                maze_lst[i].append(False)
            elif int(j/2) == j/2:
                maze_lst[i].append(False)
            else:
                maze_lst[i].append(True)
    return maze_lst

def entrance(height, width, maze_lst):
    oside=0
    oblock=0
    for i in range(2):
        side = randint(1,4)
        if side == 1:
            block = randint(1,int(width/2))
            maze_lst[0][block*2-1] = True
        if side == 2:
            block = randint(1,int(height/2))
            maze_lst[block*2-1][width-1] = True
        if side == 3:
            block = randint(1,int(width/2))
            maze_lst[height-1][block*2-1] = True
        if side == 4:
            block = randint(1,int(height/2))
            maze_lst[block*2-1][0] = True

        if side==oside and block==oblock:
            maze_lst=generate(height,width)
        else:
            oside=side
            oblock=block
    return maze_lst

def route(height, width, maze_lst):
    ?
    return maze_lst

有人可以帮忙吗?

1 个答案:

答案 0 :(得分:0)

many different algorithms来创建迷宫。维基百科有一个Python example