Catsss

Maze Generation and Solving CC

Nov 7th, 2021 (edited)
725
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.45 KB | None | 0 0
  1. local monitor = peripheral.wrap("right")
  2. local w, h = monitor.getSize()
  3. local size = tonumber(read())
  4. local matrix = {}
  5. local focusedcell = nil
  6. local queue = {}
  7. local lengths = {}
  8. monitor.clear()
  9. monitor.setTextScale(0.5)
  10. term.redirect(monitor)
  11.  
  12. function getNeighbors(mat, row, col, radius)
  13.     local rows, cols = #mat, #mat[1]
  14.     local out = {}
  15.     for i = row - radius, row + radius do
  16.             if mat[i] and mat[i][col]then
  17.                 if mat[i][col] ~= mat[row][col] then
  18.                     if not mat[i][col]["wall"] == true then
  19.                         if mat[i][col]["visited"] == false then
  20.                             table.insert(out, mat[i][col])
  21.                         end
  22.                     end
  23.                 end
  24.             end
  25.     end
  26.     for j = col - radius, col + radius do
  27.             if mat[row] and mat[row][j] then
  28.                 if mat[row][j] ~= mat[row][col] then
  29.                     if not mat[row][j]["wall"] == true then
  30.                         if mat[row][j]["visited"] == false then
  31.                             table.insert(out, mat[row][j])
  32.                             table.insert(out, mat[row][j])
  33.                         end
  34.                     end
  35.                 end
  36.             end
  37.     end
  38.     return out
  39. end
  40.  
  41.  
  42. function mazeGen(focus)
  43.     --monitor.setCursorPos(focus["x"], focus["y"])
  44.     --monitor.write("e")
  45.     local neighbors = getNeighbors(matrix, focus["x"], focus["y"], 2)
  46.     focus["visited"] = true
  47.    
  48.    
  49.    
  50.     if #neighbors ~= 0 then
  51.         local ranNeigh = neighbors[math.random(1,#neighbors)]
  52.         local differencex, differencey = (ranNeigh["x"]-focus["x"]), (ranNeigh["y"]-focus["y"])
  53.  
  54.         --print(differencex, differencey)
  55.    
  56.         --[[monitor.setCursorPos(focus["x"]+(differencex/2), focus["y"]+(differencey/2))
  57.         monitor.write(" ")]]
  58.         paintutils.drawPixel(focus["x"]+(differencex/2), focus["y"]+(differencey/2), colors.black)
  59.         matrix[focus["x"]+(differencex/2)][focus["y"]+(differencey/2)]["visited"] = true
  60.         matrix[focus["x"]+(differencex/2)][focus["y"]+(differencey/2)]["wall"] = false
  61.        
  62.    
  63.         table.insert(queue, focus)
  64.         sleep(0.05)
  65.         mazeGen(ranNeigh)
  66.      else
  67.        
  68.         while #queue > 0 do
  69.             local check = queue[#queue]
  70.             local nei = getNeighbors(matrix, check["x"], check["y"], 2)
  71.             --print(#nei)
  72.             if #nei == 0 then
  73.                 table.insert(lengths, {["point"] = check, ["length"] = #queue})
  74.                 table.remove(queue, #queue)
  75.                 if #queue == 0 then
  76.                     break
  77.                 end
  78.             else
  79.                 --print("Found! "..check["x"].." ".. check["y"])
  80.                 focus = check
  81.                 break
  82.             end
  83.         end
  84.         if #queue == 0 then
  85.            --print("Maze done!")
  86.            return 0
  87.         end
  88.         mazeGen(focus)
  89.        
  90.      end
  91. end
  92.  
  93. for x = 1, w*1 do
  94.     matrix[x] = {}
  95.     for y = 1, h*1 do
  96.         if y % 2 == 0 or x % 2 == 0 then
  97.             matrix[x][y] = {["x"] = x, ["y"] = y, ["visited"] = false, ["neighbors"] = {}, ["wall"] = true}
  98.             paintutils.drawPixel(x, y, colors.white)
  99.         else
  100.              matrix[x][y] = {["x"] = x, ["y"] = y, ["visited"] = false, ["neighbors"] = {}, ["wall"] = false}
  101.         end
  102.        
  103.     end
  104. end
  105.  
  106. local focusedcell = matrix[1][2]
  107. local startingcell = matrix[1][2]
  108.  
  109. --[[print("Caching Neighbors")
  110. for i,v in pairs(matrix) do
  111.     for o,b in pairs(matrix[i]) do
  112.         sleep()
  113.         print(b["x"], b["y"])
  114.         b["neighbors"] = getNeighbors(matrix, i, o, 2)
  115.     end
  116. end]]
  117.  
  118. while focusedcell["wall"] == true do
  119.     focusedcell = matrix[math.random(1,w)][math.random(1,h)]
  120.     startingcell = focusedcell
  121. end
  122.  
  123. monitor.setCursorPos(startingcell["x"], startingcell["y"])
  124. monitor.write("s")
  125.  
  126. --print("Found a non-wall cell to start from~!")
  127.  
  128. --local neighbors = focusedcell["neighbors"]
  129. --[[for i,v in pairs(neighbors) do
  130.     print(v["x"], v["y"])
  131.     monitor.setCursorPos(v["x"], v["y"])
  132.     monitor.write("e")
  133. end]]
  134.  
  135. mazeGen(focusedcell)
  136.  
  137. table.sort(lengths, function(a,b)
  138.     local aNum = a["length"] -- Coin value of a
  139.     local bNum = b["length"] -- Coin value of b
  140.     return aNum > bNum -- Return their comparisons, < for ascending, > for descending
  141. end)
  142.  
  143. --print("Point: "..lengths[1]["point"]["x"].." "..lengths[1]["point"]["y"].. " Highest Length: "..lengths[1]["length"])
  144.  
  145. monitor.setCursorPos(lengths[1]["point"]["x"], lengths[1]["point"]["y"])
  146. monitor.write("e")
  147.  
  148. monitor.setCursorPos(startingcell["x"], startingcell["y"])
  149. monitor.write("s")
Add Comment
Please, Sign In to add comment