Advertisement
CaptainSpaceCat

Maze Solver

Jun 11th, 2015
323
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.94 KB | None | 0 0
  1. function solveMazeTable(maze)
  2.         local stack = {}
  3.         local visited = {}
  4.         local mapW, mapH = #maze[1], #maze
  5.         for i = 1, mapH do
  6.                 visited[i] = {}
  7.         end
  8.         local sLocation = {1, 1}   --y, x
  9.         visited[1][1] = true
  10.         while true do
  11.                 local adjCells = {}
  12.                 if sLocation[1] > 1 and not maze[sLocation[1]][sLocation[2]][1] and not visited[sLocation[1] - 1][sLocation[2]] then
  13.                         adjCells[#adjCells + 1] = 1
  14.                 end
  15.                 if sLocation[2] < mapW and not maze[sLocation[1]][sLocation[2]][2] and not visited[sLocation[1]][sLocation[2] + 1] then
  16.                         adjCells[#adjCells + 1] = 2
  17.                 end
  18.                 if sLocation[1] < mapH and not maze[sLocation[1]][sLocation[2]][3] and not visited[sLocation[1] + 1][sLocation[2]] then
  19.                         adjCells[#adjCells + 1] = 3
  20.                 end
  21.                 if sLocation[2] > 1 and not maze[sLocation[1]][sLocation[2]][4] and not visited[sLocation[1]][sLocation[2] - 1] then
  22.                         adjCells[#adjCells + 1] = 4
  23.                 end
  24.                 if #adjCells > 0 then
  25.                         stack[#stack + 1] = {sLocation[1], sLocation[2]}
  26.                         adjCells = adjCells[math.random(1, #adjCells)]
  27.                         if adjCells == 1 then
  28.                                 sLocation[1] = sLocation[1] - 1
  29.                         elseif adjCells == 2 then
  30.                                 sLocation[2] = sLocation[2] + 1
  31.                         elseif adjCells == 3 then
  32.                                 sLocation[1] = sLocation[1] + 1
  33.                         elseif adjCells == 4 then
  34.                                 sLocation[2] = sLocation[2] - 1
  35.                         end
  36.                         visited[sLocation[1]][sLocation[2]] = true
  37.                         if sLocation[1] == mapH and sLocation[2] == mapW then
  38.                                 stack[#stack + 1] = {sLocation[1], sLocation[2]}
  39.                                 break
  40.                         end
  41.                 else
  42.                         sLocation = table.remove(stack)
  43.                 end
  44.         end
  45.         return stack
  46. end
  47.  
  48. function solveMazeImage(maze, pX, pY, gX, gY)
  49.         local stack = {}
  50.         visited = {}
  51.     local file = fs.open("debug", "w")
  52.     file.writeLine(tostring(pX) .. " " .. tostring(pY) .. " " .. tostring(gX) .. " " .. tostring(gY))
  53.         local mapW, mapH = #maze[1], #maze
  54.         for i, _ in pairs(maze) do
  55.                 visited[i] = {}
  56.         end
  57.     for y, v in pairs(maze) do
  58.         file.write(tostring(y) .. " ")
  59.         for x, e in pairs(v) do
  60.             file.write(tostring(e) .. ":")
  61.         end
  62.         file.writeLine("")
  63.     end
  64.         local sLocation = {pY, pX}   --y, x
  65.         visited[pY][pX] = true
  66.         while true do
  67.         file.flush()
  68.                 local adjCells = {}
  69.                 if sLocation[1] > 1 and maze[sLocation[1] - 1][sLocation[2]] ~= 1 and not visited[sLocation[1] - 1][sLocation[2]] then
  70.                         adjCells[#adjCells + 1] = 1
  71.  
  72.                 end
  73.                 if sLocation[2] < mapW and maze[sLocation[1]][sLocation[2] + 1] ~= 1 and not visited[sLocation[1]][sLocation[2] + 1] then
  74.                         adjCells[#adjCells + 1] = 2
  75.  
  76.                 end
  77.                 if sLocation[1] < mapH and maze[sLocation[1] + 1][sLocation[2]] ~= 1 and not visited[sLocation[1] + 1][sLocation[2]] then
  78.                         adjCells[#adjCells + 1] = 3
  79.  
  80.                 end
  81.                 if sLocation[2] > 1 and maze[sLocation[1]][sLocation[2] - 1] ~= 1 and not visited[sLocation[1]][sLocation[2] - 1] then
  82.                         adjCells[#adjCells + 1] = 4
  83.  
  84.                 end
  85.                 if #adjCells > 0 then
  86.                         stack[#stack + 1] = {sLocation[1], sLocation[2]}
  87.                         adjCells = adjCells[math.random(1, #adjCells)]
  88.                         if adjCells == 1 then
  89.                                 sLocation[1] = sLocation[1] - 1
  90.             file.writeLine("up")
  91.                         elseif adjCells == 2 then
  92.                                 sLocation[2] = sLocation[2] + 1
  93.             file.writeLine("right")
  94.                         elseif adjCells == 3 then
  95.                                 sLocation[1] = sLocation[1] + 1
  96.             file.writeLine("down")
  97.                         elseif adjCells == 4 then
  98.                                 sLocation[2] = sLocation[2] - 1
  99.             file.writeLine("right")
  100.                         end
  101.                         visited[sLocation[1]][sLocation[2]] = true
  102.                         if sLocation[1] == gY and sLocation[2] == gX then
  103.                                 --stack[#stack + 1] = {sLocation[1], sLocation[2]}
  104.                                 break
  105.                         end
  106.                 else
  107.             file.writeLine("back")
  108.                         sLocation = table.remove(stack)
  109.                 end
  110.         end
  111.         return stack
  112. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement