Advertisement
incinirate

Maze Generator Using Krugrall's Randomized Algorithm

Jun 11th, 2014
210
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 2.46 KB | None | 0 0
  1. shell.run("clear")
  2. print("Enter maze size")
  3. write("X:");x=math.floor(tonumber(read())/2)
  4. write("Y:");y=math.floor(tonumber(read())/2)
  5. print("Horozontal Influence (NYI)")
  6. print("Vertical Influence (NYI)")
  7. read()
  8. shell.run("clear")
  9. visited={}
  10. nodes={}
  11.  
  12. for i=1,y*2-1 do
  13.   if i%2==1 then
  14.     nodes[i]=string.rep("- ",x):sub(1,x*2-1)
  15.   else
  16.     nodes[i]=string.rep(" ",x*2-1)
  17.   end
  18. end
  19.  
  20. term.setBackgroundColor(colors.white)
  21. for i=1,x do
  22.   visited[i]={}
  23.   for j=1,y do
  24.     visited[i][j]=false
  25.     term.setCursorPos(i*2-1,j*2-1)
  26.     write(" ")
  27.   end
  28. end
  29. visited[1][1]=true
  30.  
  31. function done()
  32.   for i=1,x do
  33.     for j=1,y do
  34.       if visited[i][j]==false then return false end
  35.     end
  36.   end
  37.   return true
  38. end
  39.  
  40. function fn(sx,sy)
  41.   u,r,d,l=false,false,false,false
  42.   if sy>1 then if not visited[sx][sy-1] then u=true end end
  43.   if sy<y then if not visited[sx][sy+1] then d=true end end
  44.   if sx>1 then if not visited[sx-1][sy] then l=true end end
  45.   if sx<x then if not visited[sx+1][sy] then r=true end end
  46.   return {u,r,d,l}
  47.  
  48. end
  49.  
  50. function nt(tbl)
  51.   local num=0
  52.   for i=1,#tbl do
  53.     if tbl[i] then num=num+1 end
  54.   end
  55.   return num
  56. end
  57.  
  58. function npos(xps,yps)
  59.   nodes[yps]=nodes[yps]:sub(1,xps-1).."-"..nodes[yps]:sub(xps+1,x*2-1)
  60. end
  61.  
  62. while not done() do
  63.   lst=nil
  64.   lst={}
  65.   for i=1,x do
  66.     for j=1,y do
  67.       if visited[i][j] and nt(fn(i,j))>0 then table.insert(lst,{i,j}) end
  68.     end
  69.   end
  70.   rnd=#lst
  71.   rnd=math.random(1,rnd)
  72.   nbr=fn(lst[rnd][1],lst[rnd][2])
  73.   nls=nil
  74.   nls={}
  75.   for i=1,4 do
  76.     if nbr[i] then table.insert(nls,i) end
  77.   end
  78.   dir=nls[math.random(1,#nls)]
  79.   if dir==1 then --Up
  80.     visited[lst[rnd][1]][lst[rnd][2]-1]=true
  81.     npos(lst[rnd][1]*2-1,lst[rnd][2]*2-2)
  82.     term.setCursorPos(lst[rnd][1]*2-1,lst[rnd][2]*2-2)
  83.   elseif dir==2 then --Right
  84.     visited[lst[rnd][1]+1][lst[rnd][2]]=true
  85.     npos(lst[rnd][1]*2,lst[rnd][2]*2-1)
  86.     term.setCursorPos(lst[rnd][1]*2,lst[rnd][2]*2-1)
  87.   elseif dir==3 then --Down
  88.     visited[lst[rnd][1]][lst[rnd][2]+1]=true
  89.     npos(lst[rnd][1]*2-1,lst[rnd][2]*2)
  90.     term.setCursorPos(lst[rnd][1]*2-1,lst[rnd][2]*2)
  91.   elseif dir==4 then --Left
  92.     visited[lst[rnd][1]-1][lst[rnd][2]]=true
  93.     npos(lst[rnd][1]*2-2,lst[rnd][2]*2-1)
  94.     term.setCursorPos(lst[rnd][1]*2-2,lst[rnd][2]*2-1)
  95.   end
  96.   write(" ")
  97.   sleep(0)
  98. end
  99. os.pullEvent()
  100. handle=fs.open("LstMz","w")
  101. for i=1,y*2-1 do
  102.   handle.writeLine(nodes[i])
  103. end
  104. handle.close()
  105. term.setBackgroundColor(colors.black)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement