fork download
  1. --~ A reverse ipairs. Taken from http://l...content-available-to-author-only...s.org/wiki/IteratorsTutorial ~--
  2. --[[parameters
  3. t: the table to iterate over
  4. returns
  5. iterator function, the table and the index]]
  6. function ripairs(t)
  7. local function ripairs_it(t,i)
  8. i=i-1
  9. local v=t[i]
  10. if v==nil then return v end
  11. return i,v
  12. end
  13. return ripairs_it, t, #t+1
  14. end
  15.  
  16. --~ Seed the random numbers ~--
  17. math.randomseed(os.time())
  18. math.random()math.random()math.random()
  19.  
  20. --~ Parameters ~--
  21. local width = 45 --~ Width of the map ~--
  22. local height = 45 --~ Height of the map ~--
  23. local border = 2 --~ Width around the border of the map. At least 2 ~--
  24. local kill_no = 6 --~ Minimum number of empty cells for a miner to die ~--
  25. local spawn_prob = 0.6 --~ Chance for a miner to spawn a clone per frame ~--
  26.  
  27. local cell_kill_no = 6 --~ Minimum number of empty cells for that cell to be removed ~--
  28.  
  29. local map = {}
  30. local miners = {}
  31. local draw = {}
  32.  
  33. for x = 1, width do
  34. map[x] = {}
  35. for y = 1, height do
  36. map[x][y] = "#"
  37. end
  38. end
  39.  
  40. table.insert(miners, {
  41. x = math.random(border, width-border),
  42. y = math.random(border, height-border)
  43. })
  44.  
  45. --~ Main miner loop ~--
  46. while #miners ~= 0 do
  47. --~ Scan all the cells around the miner to check for empty ones --~
  48. for i, miner in ripairs(miners) do
  49. local empty = 0
  50. for x = -1, 1 do
  51. for y = -1, 1 do
  52. if map[miner.x+x][miner.y+y] == " " or
  53. (miner.x+x < border or miner.x+x > width-border) or
  54. (miner.y+y < border or miner.y+y > height-border) then
  55. empty = empty + 1
  56. end
  57. end
  58. end
  59. --~ If there are enough empty ones, remove the miner ~--
  60. if empty >= kill_no then
  61. table.remove(miners, i)
  62. else
  63. --~ Randomly spawn in another miner at this spot ~--
  64. if math.random() <= spawn_prob then
  65. table.insert(miners, {
  66. x = miner.x,
  67. y = miner.y
  68. })
  69. end
  70.  
  71. --~ Randomly move the miner, making sure it's not out of bounds ~--
  72. local xDir, yDir = math.random(-1, 1), math.random(-1, 1)
  73. if (miner.x+xDir >= border and miner.x+xDir <= width-border) and (miner.y+yDir >= border and miner.y+yDir <= height-border) then
  74. miner.x = miner.x + xDir
  75. miner.y = miner.y + yDir
  76. end
  77.  
  78. --~ Remove the cell the miner is on ~--
  79. if map[miner.x][miner.y] then
  80. map[miner.x][miner.y] = " "
  81. end
  82. end
  83. end
  84. end
  85.  
  86. for x = 2, #map-1 do
  87. for y = 2, #map[x]-1 do
  88. --~ Count the empty squares around this cell ~--
  89. local empty = 0
  90. for dX = -1, 1 do
  91. for dY = -1, 1 do
  92. if map[x+dX][y+dY] == " " then
  93. empty = empty + 1
  94. end
  95. end
  96. end
  97.  
  98. --~ If there is enough many, remove the block ~--
  99. if empty >= cell_kill_no then
  100. map[x][y] = " "
  101. end
  102. end
  103. end
  104.  
  105. --~ Display it as an ascii map ~--
  106. for x = 1, width do
  107. for y = 1, height do
  108. io.write(map[x][y])
  109. io.write (" ")
  110. end
  111. io.write("\n")
  112. end
Success #stdin #stdout 0.02s 2836KB
stdin
Standard input is empty
stdout
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# # # # # # #   # #   #       # # # # #   #   #     # # # # # # # # # # #     #   # # # # 
# # # # # #             #       # # #             # # # # # # # # # #             # # # # 
# # # # #                         # #             # # # # # # # # # #             # # # # 
# # # # #           #                             # # # # # # # # # #       # #     # # # 
# # # #           # # #                           # # # # # # # # #         # #       # # 
# # # #           # #                             # # # # # # # #                   # # # 
# # # #                                           # # # # # #                         # # 
# # # #                                           # # # # #                           # # 
# # # # #                                         # # # #       # #                 # # # 
# # # # #                                                       # #     # #           # # 
# # # # # #                 #                                     #   # # #         # # # 
# # # # # #               # # #                                     #   #           # # # 
# # # # # #                 # #                                                       # # 
# # # # # # #                 #                                                       # # 
# # # # # #                                                                         # # # 
# # # # # # #                                                                     # # # # 
# # # # # # # #         # #                                                     # # # # # 
# # # # # # # # # #     # #     # #                                           # # # # # # 
# # # # # # # # # #           # # #                                         # # # # # # # 
# # # # # # # # # # #           # #                                             # # # # # 
# # # # # # # # # # # #         # #                                               # # # # 
# # # # # # # # # # #                                                                 # # 
# # # # # # # # # # #                                                                 # # 
# # # # # # # #                                                                     # # # 
# # # # # # # #                                                                     # # # 
# # # # # # # # #           # # #                                                     # # 
# # # # # # # # # #         # # #                           # #                       # # 
# # # # # # # # # # # #       #                             # #                     # # # 
# # # # # # # # # # # #                                                               # # 
# # # # # # # # # # #                                       #                         # # 
# # # # # # # # # # # # #                               # # # #                       # # 
# # # # # # # # # # # # #                           # # # # # #       #               # # 
# # # # # # # # # # # # #                         # # # # # #       # #               # # 
# # # # # # # # # # # # # #                       # # # # # #       #           # #   # # 
# # # # # # # # # # # # # #                     # # # # # # #     #           # # # # # # 
# # # # # # # # # # # #                           # # # # # # # # # # #     # # # # # # # 
# # # # # # # # # # # #                         # # # # # # # # # # # # # # # # # # # # # 
# # # # # # # # # # # # #                 # # # # # # # # # # # # # # # # # # # # # # # # 
# # # # # # # # # # # #                 # # # # # # # # # # # # # # # # # # # # # # # # # 
# # # # # # # # # # # # #       # #       # # # # # # # # # # # # # # # # # # # # # # # # 
# # # # # # # # # # # #       # #       # # # # # # # # # # # # # # # # # # # # # # # # # 
# # # # # # # # # # # # #     # #     # # # # # # # # # # # # # # # # # # # # # # # # # # 
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # 
# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # #