ecco7777

Labyrinth Generator

Feb 20th, 2017
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 1.56 KB | None | 0 0
  1. size=15
  2. wayLenght=20
  3. minLenght=20
  4. trys=0
  5. avtrys=0
  6.  
  7. function newMap(size)
  8. map={}
  9. for x=1,size do
  10. map[x]={}
  11. for y=1,size do
  12. map[x][y]=0
  13. end
  14. end
  15. return map
  16. end
  17.  
  18. function goForward(map,dir)
  19. if x+2<=size and y+2<=size and x+2>0 and y+2>0 and x-2<=size and y-2<=size and x-2>0 and y-2>0 then
  20. if dir==1 then
  21. if map[x][y+2]==nil or map[x][y+2]==1 then return false else map[x][y+2]=1 map[x][y+1]=1 y=y+2 end
  22. end
  23. if dir==3 then
  24. if map[x][y-2]==nil or map[x][y-2]==1 then return false else map[x][y-2]=1 map[x][y-1]=1 y=y-2 end
  25. end
  26. if dir==2 then
  27. if map[x+2][y]==nil or map[x+2][y]==1 then return false else map[x+2][y]=1 map[x+1][y]=1 x=x+2 end
  28. end
  29. if dir==4 then
  30. if map[x-2][y]==nil or map[x-2][y]==1 then return false else map[x-2][y]=1 map[x-1][y]=1 x=x-2  end
  31. end
  32. else
  33. return false
  34. end
  35. end
  36.  
  37. function getPathLenght(map)
  38. wert=0
  39. for x=1,size do
  40. for y=1,size do
  41. wert=wert+map[x][y]
  42. end
  43. end
  44. return wert
  45. end
  46.  
  47. function writeMap(map,file)
  48. shell.run("rm "..file)
  49. fp=fs.open(file,"w")
  50. size=#map[1]
  51. for x=1,size do
  52. for y=1,size do
  53. fp.write(tostring(map[x][y]))
  54. end
  55. fp.write("\n")
  56. end
  57. fp.close()
  58. end
  59.  
  60. while true do
  61. way=newMap(size)
  62. x=math.random(1,size)
  63. y=math.random(1,size)
  64. way[x][y]=1
  65. for i=1,wayLenght do
  66. goForward(way,math.random(1,4))
  67. end
  68. --writeMap(way,"weg")
  69.  
  70. if getPathLenght(way)>=minLenght then
  71. paintutils.drawImage(way,1,1)
  72. term.setBackgroundColor(colors.black)
  73. term.setCursorPos(1,1)
  74. term.write(trys)
  75. avtrys=(avtrys+trys)/2
  76. term.setCursorPos(1,2)
  77. term.write(avtrys)
  78. sleep(0.5)
  79. term.clear()
  80. trys=0
  81. else trys=trys+1
  82. end
  83. end
Add Comment
Please, Sign In to add comment