columna1

Maze Generator

Jun 19th, 2013
251
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.07 KB | None | 0 0
  1. print("generate maze instantly? y/n")
  2. instants = read()
  3. print("play the maze like a game? y/n")
  4. games = read()
  5.  
  6. if games == "y" then games = true else games = false end
  7. if instants == "y" then instants = true else instants = false end
  8.  
  9. w,h = term.getSize()
  10. curx = 1
  11. cury = 1
  12. stack = {}
  13. visited = {}
  14. mazex = (h - 1) / 2
  15. mazey = (w - 1) / 2
  16. showstack = true
  17.  
  18. --non color conversions
  19. textcolors = {
  20.     [colors.black] = " ",
  21.     [colors.blue] = "X",
  22.     [colors.lime] = "@",
  23.     [colors.red] = "F"
  24. }
  25.  
  26. canvas = {}
  27. for xx = 1,w do
  28.     canvas[xx] = {}
  29.     for yy = 1,h do
  30.         canvas[xx][yy] = colors.blue
  31.     end
  32. end
  33.  
  34. --first thing is first draw the maze
  35. term.clear()
  36. function draw()
  37.     if term.isColor() then
  38.       for i = 1,w do
  39.         for j = 1,h do
  40.           term.setCursorPos(i,j)
  41.           term.setBackgroundColor(canvas[i][j])
  42.           write(" ")
  43.         end
  44.       end
  45.       term.setBackgroundColor(colors.black)
  46.     else
  47.       for i = 1,w do
  48.         for j = 1,h do
  49.           term.setCursorPos(i,j)
  50.           write(textcolors[canvas[i][j]])
  51.         end
  52.       end
  53.     end
  54. end
  55. draw()
  56.  
  57. function movecell(x,y)
  58.  
  59.   -- removing wall and  x at cells
  60.   canvas[curx * 2][cury * 2] = colors.black
  61.   canvas[x * 2][y * 2] = colors.black
  62.   if x > curx then canvas[(x * 2) - 1][y * 2] = colors.black end
  63.   if y > cury then canvas[x * 2][(y * 2) - 1] = colors.black end
  64.   if x < curx then canvas[(x * 2) + 1][y * 2] = colors.black end
  65.   if y < cury then canvas[x * 2][(y * 2) + 1] = colors.black end
  66.   canvas[curx * 2][cury * 2] = colors.black
  67.   curx = x
  68.   cury = y
  69.   canvas[curx * 2][cury * 2] = colors.lime
  70.   if not instants then draw() end---------------------------------------------------
  71. end
  72.  
  73. function checkvisited(y,x)
  74.   if x <= 0 or y <= 0 or x > mazex or y > mazey then return true end
  75.   local v
  76.   for _,v in pairs(visited) do
  77.     if v[1] == y and v[2] == x then
  78.       return true
  79.     end
  80.   end
  81.   return false
  82. end
  83.  
  84. function addvisited(x,y)
  85.   table.insert(visited,{x,y})
  86. end
  87.  
  88. addvisited(curx,cury)
  89.  
  90. function addstack(x,y)
  91.   table.insert(stack,{x,y})
  92. end
  93.  
  94. function popstack()
  95.   local val = stack[#stack]
  96.   table.remove(stack,#stack);
  97.  
  98.   return val[1],val[2]
  99. end
  100.  
  101. repeat
  102.  
  103. --find unvisited cells next to current one
  104.   local posiblecells = {}
  105.  
  106.   if checkvisited(curx-1,cury) == false then
  107.     table.insert(posiblecells,{curx - 1,cury})
  108.   end
  109.   if checkvisited(curx,cury - 1) == false then
  110.     table.insert(posiblecells,{curx,cury - 1})
  111.   end
  112.   if checkvisited(curx + 1,cury) == false then
  113.     table.insert(posiblecells,{curx + 1,cury})
  114.   end
  115.   if checkvisited(curx,cury + 1) == false then
  116.     table.insert(posiblecells,{curx,cury + 1})
  117.   end
  118.  
  119.   if #posiblecells > 0 then
  120.     addstack(curx,cury)
  121.     local targetx, targety
  122.     local i = math.random(1,#posiblecells)
  123.     targetx = posiblecells[i][1]
  124.     targety = posiblecells[i][2]
  125.    
  126.     addvisited(targetx,targety)
  127.     movecell(targetx,targety)
  128.   else
  129.     local targetx, targety
  130.     targetx, targety = popstack()
  131.     movecell(targetx,targety)
  132.   end
  133.  
  134.   --sleep(0.05)
  135.   os.queueEvent("randomevent")
  136.   os.pullEvent()
  137.  
  138. until #stack == 0
  139.  
  140. if games then
  141.     canvas[w-1][h-1] = colors.red
  142.     draw()
  143.     running = true
  144.     px = 2
  145.     py = 2
  146.     while running do
  147.         changed = false
  148.         lpx = px
  149.         lpy = py
  150.         event,p1 = os.pullEvent("key")
  151.         if p1 == 200 then
  152.             if canvas[px][py-1] == colors.black or canvas[px][py-1] == colors.red then
  153.                 py = py - 1
  154.                 changed = true
  155.             end
  156.         elseif p1 == 208 then
  157.             if canvas[px][py+1] == colors.black or canvas[px][py+1] == colors.red then
  158.                 py = py + 1
  159.                 changed = true
  160.             end
  161.         elseif p1 == 203 then
  162.             if canvas[px-1][py] == colors.black or canvas[px-1][py] == colors.red then
  163.                 px = px - 1
  164.                 changed = true
  165.             end
  166.         elseif p1 == 205 then
  167.             if canvas[px+1][py] == colors.black or canvas[px+1][py] == colors.red then
  168.                 px = px + 1
  169.                 changed = true
  170.             end
  171.         elseif p1 == 16 then
  172.             running = false
  173.         end
  174.         if px == w-1 and py == h - 1 then
  175.             running = false
  176.         end
  177.         if changed then
  178.             canvas[px][py] = colors.lime
  179.             canvas[lpx][lpy] = colors.black
  180.             draw()
  181.         end
  182.     end
  183. else
  184.     draw()
  185.     os.pullEvent()
  186. end
Advertisement
Add Comment
Please, Sign In to add comment