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 = 50 --~ Width of the map ~--
  22. local height = 50 --~ 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] = true
  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 not 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] = false
  81. end
  82. end
  83. end
  84. end
  85.  
  86. --~ Display it as an ascii map ~--
  87. output = ""
  88. for x = 1, width do
  89. output = output .. "\n"
  90. for y = 1, height do
  91. if map[x][y] then
  92. output = output .. "#"
  93. else
  94. output = output .. " "
  95. end
  96. end
  97. end
  98.  
  99. print(output)
Success #stdin #stdout 0.02s 2788KB
stdin
Standard input is empty
stdout
##################################################
##############   ##########  ### ## ## # # # #####
###############   ##########  #         #    #####
########## #  #   ###  #####      #     #     ####
##  #####        # ##  # #            #         ##
##  ####     #         #  #   #   # ##     #  # ##
#             ##  #   ##  #    #   #    ####  ####
##     #        #  # ###  # #   ##    ## ##   ####
#####  #             ##      #    # #   ###   ####
########     # # ##       #   # #        #####  ##
## # #   #  #   # #     ##      #  #       ###  ##
##  #   #       ##      ##  #    #  #  #    # # ##
#    # #       # #  #   ##          #   #      ###
###   #     #   #  #   #  #  ##  ##  #      #  ###
####      ###   #  ## #        # #  ###    #  ####
#   # ## ###  ###   ##  #   #      ###  ##    ####
##    #  ##    ###         #  #     ##   #     ###
#          #        #   ##   #  #  ##           ##
#   # # #  #        #   #    #          #      ###
##              ##  #   #     ## #   #         ###
#  #  #     ###      ###   #   #   #  ##   # #  ##
##  ### ## ###     #  ##   #             #  #  ###
#  #        #    #        ## ##   #       ##  # ##
##     #   #      #  ##  ##         #  #  ##   ###
##     #   #      #   ##     # #    #   #     # ##
## ###    # #  #         #  #      #           ###
#    ##  #        #   # #         #   # # # #   ##
###  ##        # ##      #  #      ## #  #  #   ##
#     #     #   ###            # #    #         ##
##   #     ## #####      #   ####  #     #   ## ##
###  #  #      ####     #  #  ###          # #####
###             #     #   #### ###    #       ####
#  #         #   #        ####  ### #        #####
##    ##    #      # ##     #    #     # #### ####
# #    ##### ##    #    #           ##  ###   ####
##  #   ###   ##         #   ##        # ##    ###
###  ##  #        # # #   # ####      ###  #    ##
#           #    #    #  #   #   #   ###      # ##
# #  #     # #  #       #    #  #     #     ### ##
##### ##  #   #           ##   # ##      ##### ###
#########        #  ###       #         ## ##  ###
#########         #### #              # ##      ##
############     # ####     ## ### ##           ##
#############  #    ###       # #  ## #      # ###
################       #                     #####
###############           #     # #    #      ####
################    ##     #   ##  ##  ## #    ###
################# ##### ##    ### #  ##  #### # ##
##################################################
##################################################