Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
我的Lua迷宫生成器正在编织_Lua_Maze - Fatal编程技术网

我的Lua迷宫生成器正在编织

我的Lua迷宫生成器正在编织,lua,maze,Lua,Maze,我正在构建一个Lua脚本,该脚本使用递归回溯程序的一个版本生成一个迷宫,该版本是用堆栈而不是递归实现的。现在迷宫出来了,我似乎不知道在我的逻辑中这是在哪里发生的。以下函数以x和y作为生成迷宫的起点,迷宫是表格的二维结构表: local function buildMazeInternal(x,y,maze) local stack = {} local directions = {'North','East','South','West'} table.insert(

我正在构建一个Lua脚本,该脚本使用递归回溯程序的一个版本生成一个迷宫,该版本是用堆栈而不是递归实现的。现在迷宫出来了,我似乎不知道在我的逻辑中这是在哪里发生的。以下函数以x和y作为生成迷宫的起点,迷宫是表格的二维结构表:

local function buildMazeInternal(x,y,maze)

    local stack = {}
    local directions = {'North','East','South','West'}

    table.insert(stack,{x=x,y=y})

    while #stack > 0 do
        local index = 1
        local nextX = x
        local nextY = y
        local braid = false 

        for i = #directions, 2, -1 do -- backwards
            local r = calc:Roll(1,i) -- select a random number between 1 and i
            directions[i], directions[r] = directions[r], directions[i] -- swap the randomly selected item to position i
        end

        while index <= #directions and nextX == x and nextY == y do
            if directions[index] == 'North' and y > 1 and not maze[y-1][x].Visited then
                maze[y][x].North = true
                maze[y-1][x].South = true
                nextY = y-1
            elseif directions[index] == 'East' and x < width and not maze[y][x+1].Visited then
                maze[y][x].East = true
                maze[y][x+1].West = true
                nextX = x+1
            elseif directions[index] == 'South' and y < height and not maze[y+1][x].Visited then
                maze[y][x].South = true
                maze[y+1][x].North = true
                nextY = y+1
            elseif directions[index] == 'West' and x > 1 and not maze[y][x-1].Visited then
                maze[y][x].West = true
                maze[y][x-1].East = true
                nextX = x-1
            else
                index = index + 1
            end
        end

        if nextX ~= x or nextY ~= y then
            x = nextX
            y = nextY
            maze[y][x].Visited = true
            table.insert(stack,{x=x,y=y})
        else    
            x = stack[#stack].x
            y = stack[#stack].y
            table.remove(stack)
        end
    end
end

我知道我忽略了一些事情,但我似乎无法确定。请注意,calc:Roll1100方法在我的应用程序中是一种.net方法,用于模拟滚动骰子,在这种情况下,1*100面骰子,它可以替换为调用math.Random1100以在我的应用程序之外使用。

我至少看到一个问题。当你想上升时,你要检查上升的单元格是否被访问过,如果是,你就跳过上升

这似乎不正确。如果您想向上移动,但从当前单元格向上移动的单元格已被访问,但有一个向下的出口,您应该仍然能够向上移动,而不是因为它已被访问而跳过

这同样适用于其他方向


这就是我所得到的。

我在Reddit上发布后找到了答案。我没有将初始单元格设置为“已访问”,允许它通过两次。修正方法是在表格前面添加迷宫[y][x]。Visited=true。insertstack,{x=x,y=y}。

这意味着迷宫中有死胡同或路径,它们既不会导致迷宫死角,也不会解决迷宫问题。我不确定我是否在跟随。我从来都不想进入以前访问过的牢房,因为那会造成死胡同。所以,如果我的下一个方向是北方,而北方的牢房已经被人参观过,我就不应该去北方。我继续下一个可能的方向。好吧,你把事情搞混了。向一个方向填充一个细胞和选择下一个处理哪个细胞是两件不同的事情。我想说的是,你在错误地跳过填充某些单元格。你也可能错误地选择了下一个单元格,但我的回答中没有提到。我不知道这种情况发生在哪里。当决定向北移动时,向北移动单元的状态已被检查为未访问。一个单元格只能有一个或两个出口。第一个出口在第一次访问小区时确定,第二个出口在确定下一个小区时确定。我可能仍然误解了你所指出的问题。