Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function solveMazeTable(maze)
- local stack = {}
- local visited = {}
- local mapW, mapH = #maze[1], #maze
- for i = 1, mapH do
- visited[i] = {}
- end
- local sLocation = {1, 1} --y, x
- visited[1][1] = true
- while true do
- local adjCells = {}
- if sLocation[1] > 1 and not maze[sLocation[1]][sLocation[2]][1] and not visited[sLocation[1] - 1][sLocation[2]] then
- adjCells[#adjCells + 1] = 1
- end
- if sLocation[2] < mapW and not maze[sLocation[1]][sLocation[2]][2] and not visited[sLocation[1]][sLocation[2] + 1] then
- adjCells[#adjCells + 1] = 2
- end
- if sLocation[1] < mapH and not maze[sLocation[1]][sLocation[2]][3] and not visited[sLocation[1] + 1][sLocation[2]] then
- adjCells[#adjCells + 1] = 3
- end
- if sLocation[2] > 1 and not maze[sLocation[1]][sLocation[2]][4] and not visited[sLocation[1]][sLocation[2] - 1] then
- adjCells[#adjCells + 1] = 4
- end
- if #adjCells > 0 then
- stack[#stack + 1] = {sLocation[1], sLocation[2]}
- adjCells = adjCells[math.random(1, #adjCells)]
- if adjCells == 1 then
- sLocation[1] = sLocation[1] - 1
- elseif adjCells == 2 then
- sLocation[2] = sLocation[2] + 1
- elseif adjCells == 3 then
- sLocation[1] = sLocation[1] + 1
- elseif adjCells == 4 then
- sLocation[2] = sLocation[2] - 1
- end
- visited[sLocation[1]][sLocation[2]] = true
- if sLocation[1] == mapH and sLocation[2] == mapW then
- stack[#stack + 1] = {sLocation[1], sLocation[2]}
- break
- end
- else
- sLocation = table.remove(stack)
- end
- end
- return stack
- end
- function solveMazeImage(maze, pX, pY, gX, gY)
- local stack = {}
- visited = {}
- local file = fs.open("debug", "w")
- file.writeLine(tostring(pX) .. " " .. tostring(pY) .. " " .. tostring(gX) .. " " .. tostring(gY))
- local mapW, mapH = #maze[1], #maze
- for i, _ in pairs(maze) do
- visited[i] = {}
- end
- for y, v in pairs(maze) do
- file.write(tostring(y) .. " ")
- for x, e in pairs(v) do
- file.write(tostring(e) .. ":")
- end
- file.writeLine("")
- end
- local sLocation = {pY, pX} --y, x
- visited[pY][pX] = true
- while true do
- file.flush()
- local adjCells = {}
- if sLocation[1] > 1 and maze[sLocation[1] - 1][sLocation[2]] ~= 1 and not visited[sLocation[1] - 1][sLocation[2]] then
- adjCells[#adjCells + 1] = 1
- end
- if sLocation[2] < mapW and maze[sLocation[1]][sLocation[2] + 1] ~= 1 and not visited[sLocation[1]][sLocation[2] + 1] then
- adjCells[#adjCells + 1] = 2
- end
- if sLocation[1] < mapH and maze[sLocation[1] + 1][sLocation[2]] ~= 1 and not visited[sLocation[1] + 1][sLocation[2]] then
- adjCells[#adjCells + 1] = 3
- end
- if sLocation[2] > 1 and maze[sLocation[1]][sLocation[2] - 1] ~= 1 and not visited[sLocation[1]][sLocation[2] - 1] then
- adjCells[#adjCells + 1] = 4
- end
- if #adjCells > 0 then
- stack[#stack + 1] = {sLocation[1], sLocation[2]}
- adjCells = adjCells[math.random(1, #adjCells)]
- if adjCells == 1 then
- sLocation[1] = sLocation[1] - 1
- file.writeLine("up")
- elseif adjCells == 2 then
- sLocation[2] = sLocation[2] + 1
- file.writeLine("right")
- elseif adjCells == 3 then
- sLocation[1] = sLocation[1] + 1
- file.writeLine("down")
- elseif adjCells == 4 then
- sLocation[2] = sLocation[2] - 1
- file.writeLine("right")
- end
- visited[sLocation[1]][sLocation[2]] = true
- if sLocation[1] == gY and sLocation[2] == gX then
- --stack[#stack + 1] = {sLocation[1], sLocation[2]}
- break
- end
- else
- file.writeLine("back")
- sLocation = table.remove(stack)
- end
- end
- return stack
- end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement