Advertisement
CaptainSpaceCat

2D Recursive Maze Generator

Jun 9th, 2015
305
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 7.30 KB | None | 0 0
  1. local termW, termH = term.getSize()
  2. local debug = false
  3.  
  4. function drawRelevantImage(img, xPos, yPos)
  5.         for i, v in pairs(img) do
  6.                 for k, e in pairs(v) do
  7.                         if i + yPos - 1 >= 1 and i + yPos - 1 <= termH and k + xPos - 1 >= 1 and k + xPos - 1 <= termW then
  8.                                 term.setCursorPos(k + xPos - 1, i + yPos - 1)
  9.                                 term.setBackgroundColor(e)
  10.                                 term.write(" ")
  11.                         end
  12.                 end
  13.         end
  14. end
  15.  
  16. function win()
  17.     local termW, termH = term.getSize()
  18.     for i = 1, 7 do
  19.         term.setCursorPos(termW/2 - 11, 1)
  20.         term.setBackgroundColor(colors.black)
  21.         term.setTextColor(colors.yellow)
  22.         term.clearLine()
  23.         term.write("You won! Congratulations!")
  24.         sleep(.1)
  25.         term.setCursorPos(termW/2 - 11, 1)
  26.         term.setBackgroundColor(colors.black)
  27.         term.setTextColor(colors.blue)
  28.         term.clearLine()
  29.         term.write("You won! Congratulations!")
  30.         sleep(.1)
  31.     end
  32.     term.setBackgroundColor(colors.black)
  33.     term.clear()
  34.     term.setCursorPos(1, 1)
  35. end
  36.  
  37. function gameGen(mapW, mapH)
  38.     pLocation = math.random(1, 4)
  39.     if pLocation == 1 then
  40.         pLocation = {2, 2}
  41.         gLocation = {mapW*2, mapH*2}
  42.     elseif pLocation == 2 then
  43.         pLocation = {mapW*2, 2}
  44.         gLocation = {2, mapH*2}
  45.     elseif pLocation == 3 then
  46.         pLocation = {2, mapH*2}
  47.         gLocation = {mapW*2, 2}
  48.     elseif pLocation == 4 then
  49.         pLocation = {mapW*2, mapH*2}
  50.         gLocation = {2, 2}
  51.     end
  52.     return pLocation, gLocation
  53. end
  54.  
  55. function mazeGenRecursive(mapW, mapH)
  56.     local maze = {}
  57.     for y = 1, mapH do
  58.         maze[y] = {}
  59.         for x = 1, mapW do
  60.             maze[y][x] = {}
  61.             for i = 1, 4 do
  62.                 maze[y][x][i] = true
  63.             end
  64.         end
  65.     end
  66.    
  67.     local current = {1, 1}   --y, x
  68.     local stack = {}
  69.     local cellsRemaining = mapW * mapH
  70.     while cellsRemaining > 1 do
  71.         local fullCells = {}
  72.         if current[1] > 1 and maze[current[1] - 1][current[2]][1] and maze[current[1] - 1][current[2]][2] and maze[current[1] - 1][current[2]][3] and maze[current[1] - 1][current[2]][4] then
  73.             fullCells[#fullCells + 1] = 1
  74.         end
  75.         if current[1] < mapH and maze[current[1] + 1][current[2]][1] and maze[current[1] + 1][current[2]][2] and maze[current[1] + 1][current[2]][3] and maze[current[1] + 1][current[2]][4]then
  76.             fullCells[#fullCells + 1] = 3
  77.         end
  78.         if current[2] > 1 and maze[current[1]][current[2] - 1][1] and maze[current[1]][current[2] - 1][2] and maze[current[1]][current[2] - 1][3] and maze[current[1]][current[2] - 1][4] then
  79.             fullCells[#fullCells + 1] = 4
  80.         end
  81.         if current[2] < mapW and maze[current[1]][current[2] + 1][1] and maze[current[1]][current[2] + 1][2] and maze[current[1]][current[2] + 1][3] and maze[current[1]][current[2] + 1][4] then
  82.             fullCells[#fullCells + 1] = 2
  83.         end
  84.         if #fullCells > 0 then
  85.             cellsRemaining = cellsRemaining - 1
  86.             stack[#stack + 1] = {current[1], current[2]}
  87.             fullCells = fullCells[math.random(1, #fullCells)]
  88.             maze[current[1]][current[2]][fullCells] = false
  89.             if fullCells == 1 then
  90.                 maze[current[1] - 1][current[2]][3] = false
  91.                 current = {current[1] - 1, current[2]}
  92.             elseif fullCells == 2 then
  93.                 maze[current[1]][current[2] + 1][4] = false
  94.                 current = {current[1], current[2] + 1}
  95.             elseif fullCells == 3 then
  96.                 maze[current[1] + 1][current[2]][1] = false
  97.                 current = {current[1] + 1, current[2]}
  98.             elseif fullCells == 4 then
  99.                 maze[current[1]][current[2] - 1][2] = false
  100.                 current = {current[1], current[2] - 1}
  101.             end
  102.         else
  103.             current = table.remove(stack)
  104.         end
  105.     end
  106.  
  107.     local gen = maze
  108.     maze = {}
  109.     for i = 1, mapH*2 + 1 do
  110.         maze[i] = {}
  111.     end
  112.     for i = 1, mapW*2 + 1 do
  113.         maze[1][i] = 1
  114.         maze[mapH*2 + 1][i] = 1
  115.     end
  116.     for y = 2, mapH*2 do
  117.         for x = 1, mapW*2 + 1 do
  118.             if x == 1 or x == mapW*2 + 1 then
  119.                 maze[y][x] = 1
  120.             elseif y%2 == 0 and x%2 == 0 then
  121.                 maze[y][x] = 2^15
  122.             elseif y%2 == 1 and x%2 == 1 then
  123.                 maze[y][x] = 1
  124.             else
  125.                 if y%2 == 0 then
  126.                     if gen[y/2][(x - 1)/2][2] then
  127.                         maze[y][x] = 1
  128.                     else
  129.                         maze[y][x] = 2^15
  130.                     end
  131.                 elseif y%2 == 1 then
  132.                     if gen[(y - 1)/2][x/2][3] then
  133.                         maze[y][x] = 1
  134.                     else
  135.                         maze[y][x] = 2^15
  136.                     end
  137.                 end
  138.             end
  139.         end
  140.     end
  141.     return maze
  142. end
  143.  
  144. term.setBackgroundColor(colors.black)
  145. term.clear()
  146. local logo = mazeGenRecursive(5, 5)
  147. term.setCursorPos(termW/2 - 4, 3)
  148. term.write("Maze Game")
  149. logo[2][2] = colors.lightBlue
  150. logo[10][10] = colors.orange
  151. drawRelevantImage(logo, termW/2 - 5, 6)
  152. term.setCursorPos(termW/2 - 7, termH - 2)
  153. term.setBackgroundColor(colors.black)
  154. term.setTextColor(colors.white)
  155. term.write("Press any key...")
  156. os.pullEvent("key")
  157. term.setBackgroundColor(colors.black)
  158. term.clear()
  159. term.setCursorPos(1, 1)
  160. term.write("Enter maze width: ")
  161. local mapW = read()
  162. while not tonumber(mapW) do
  163.     term.setCursorPos(1, 1)
  164.     term.clearLine()
  165.     term.write("Enter maze width: ")
  166.     mapW = read()
  167. end
  168. term.setCursorPos(1, 2)
  169. term.write("Enter maze height: ")
  170. local mapH = read()
  171. while not tonumber(mapH) do
  172.     term.setCursorPos(1, 2)
  173.     term.clearLine()
  174.     term.write("Enter maze height: ")
  175.     mapH = read()
  176. end
  177.  
  178. mapW, mapH = tonumber(mapW), tonumber(mapH)
  179. local maze = mazeGenRecursive(mapW, mapH)
  180. local pLocation, gLocation = gameGen(mapW, mapH, maze)
  181. maze[gLocation[2]][gLocation[1]] = 2
  182. if debug then
  183.     paintutils.drawImage(maze, 1, 1)
  184.     term.setCursorPos(pLocation[1], pLocation[2])
  185.     term.setBackgroundColor(colors.lightBlue)
  186.     term.write(" ")
  187. end
  188.  
  189. term.setBackgroundColor(colors.black)
  190. term.setTextColor(colors.white)
  191. term.clear()
  192. term.setCursorPos(1, 1)
  193. print("You are the blue square...")
  194. term.setCursorPos(termW/2 + 1, termH/2 + 1)
  195. term.setBackgroundColor(colors.lightBlue)
  196. term.write(" ")
  197. term.setCursorPos(termW/2 - 7, termH - 1)
  198. term.setBackgroundColor(colors.black)
  199. term.setTextColor(colors.white)
  200. term.write("Press any key...")
  201. os.pullEvent("key")
  202.  
  203. term.setBackgroundColor(colors.black)
  204. term.setTextColor(colors.white)
  205. term.clear()
  206. term.setCursorPos(1, 1)
  207. print("Get to the orange square...")
  208. term.setCursorPos(termW/2 + 1, termH/2 + 1)
  209. term.setBackgroundColor(colors.orange)
  210. term.write(" ")
  211. term.setCursorPos(termW/2 - 7, termH - 1)
  212. term.setBackgroundColor(colors.black)
  213. term.setTextColor(colors.white)
  214. term.write("Press any key...")
  215. os.pullEvent("key")
  216. while true do
  217.   term.setBackgroundColor(colors.black)
  218.   term.clear()
  219.   --term.setCursorPos(10, 1)
  220.   --write(pLocation[1], " ", tostring(pLocation[2]))
  221.   drawRelevantImage(maze, termW/2 - pLocation[1] + 2, termH/2 - pLocation[2] + 2)
  222.   term.setBackgroundColor(colors.lightBlue)
  223.   term.setCursorPos(termW/2 + 1, termH/2 + 1)
  224.   term.write(" ")
  225.   if pLocation[1] == gLocation[1] and pLocation[2] == gLocation[2] then
  226.     win()
  227.     break
  228.   end
  229.   local events = {os.pullEvent()}
  230.   if events[1] == "key" then
  231.     if events[2] == 200 then
  232.       if maze[pLocation[2] - 1][pLocation[1]] ~= 1 then
  233.         pLocation[2] = pLocation[2] - 1
  234.       end
  235.     elseif events[2] == 203 then
  236.       if maze[pLocation[2]][pLocation[1] - 1] ~= 1 then
  237.         pLocation[1] = pLocation[1] - 1
  238.       end
  239.     elseif events[2] == 208 then
  240.       if maze[pLocation[2] + 1][pLocation[1]] ~= 1 then
  241.         pLocation[2] = pLocation[2] + 1
  242.       end
  243.     elseif events[2] == 205 then
  244.       if maze[pLocation[2]][pLocation[1] + 1] ~= 1 then
  245.         pLocation[1] = pLocation[1] + 1
  246.       end
  247.     end
  248.   end
  249. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement