Advertisement
programcreator

Game of Life by Conan

Feb 20th, 2016
274
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 3.01 KB | None | 0 0
  1. --]] Game of life, by Redxone(Lewisk3) [[--
  2. -- Rules:
  3. -- if less than 2, die
  4. -- if more then 3, die
  5. -- if has 2 or 3, live
  6. -- if more then 3, die
  7. -- if has exactly 3 and is dead, live
  8. os.loadAPI("grid")
  9. ------------------------------------
  10. term.setBackgroundColor(colors.black)
  11. term.clear()
  12. term.setCursorPos(1,1)
  13. term.setBackgroundColor(colors.gray)
  14. term.setTextColor(colors.white)
  15. term.clearLine()
  16. print("Creating cell universe...")
  17. local w, h = term.getSize()
  18. local verse = grid.create(w,h)
  19. local isPaused = false
  20. local running = true
  21. local generation = 0
  22. local speed = 0.2
  23. local genTimer = os.startTimer(speed)
  24. function newCell(alive)
  25. local cell = {isAlive=true}
  26. if(not alive)then cell.isAlive = false end
  27. return cell
  28. end
  29. --- Fill universe with dead cells ---
  30. verse:fill(newCell(false))
  31. -------------------------------------
  32. function drawCell(x,y)
  33. if(x <= w and y <= h and y >= 2)then
  34. term.setCursorPos(x,y)
  35. if(verse:get(x,y).isAlive==true)then
  36. term.setBackgroundColor(colors.yellow)
  37. term.setTextColor(colors.black)
  38. write(' ')
  39. else
  40. term.setBackgroundColor(colors.black)
  41. term.setTextColor(colors.lightGray)
  42. write('L')
  43. end
  44. end
  45. end
  46. function life()
  47. for x = 1, verse:getHeight() do
  48. for y = 1, verse:getWidth() do
  49. local friends = 0
  50. if(verse:get(y-1,x-1).isAlive == true)then friends = friends + 1 end
  51. if(verse:get(y,x-1).isAlive == true)then friends = friends + 1 end
  52. if(verse:get(y-1,x).isAlive == true)then friends = friends + 1 end
  53. if(verse:get(y,x+1).isAlive == true)then friends = friends + 1 end
  54. if(verse:get(y+1,x).isAlive == true)then friends = friends + 1 end
  55. if(verse:get(y+1,x+1).isAlive == true)then friends = friends + 1 end
  56. if(verse:get(y+1,x-1).isAlive == true)then friends = friends + 1 end
  57. if(verse:get(y-1,x+1).isAlive == true)then friends = friends + 1 end
  58. -- Die conditions
  59. if(verse:get(y,x).isAlive and friends < 2)then
  60. verse:get(y,x).isAlive = false
  61. elseif(friends > 3 and verse:get(x,y).isAlive)then
  62. verse:get(y,x).isAlive = false
  63. end
  64. -- Live conditions
  65. if(verse:get(y,x).isAlive and (friends == 2 or friends == 3))then
  66. verse:get(y,x).isAlive = true
  67. end
  68. if(not verse:get(y,x).isAlive and (friends == 3))then
  69. verse:get(y,x).isAlive = true
  70. end
  71. end
  72. end
  73. end
  74. function infoBar()
  75. term.setCursorPos(1,1)
  76. term.setBackgroundColor(colors.gray)
  77. term.setTextColor(colors.white)
  78. term.clearLine()
  79. write(" Paused: " .. tostring(isPaused) .. "   Generation: " ..  generation .. "  Speed: " .. speed)
  80. end
  81. verse:loop(drawCell)
  82. while running do
  83. term.current().setVisible(false)
  84. infoBar()
  85. ev = {os.pullEvent()}
  86. if(ev[1] == 'key' and ev[2] == keys.space)then
  87. isPaused = not isPaused
  88. end
  89. if(ev[1] == 'timer' and ev[2] == genTimer)then
  90. if(not isPaused)then life() verse:loop(drawCell) generation = generation + 1 end
  91. genTimer = os.startTimer(speed)
  92. elseif(isPaused)then
  93. if(ev[1] == "mouse_click")then
  94. if(ev[2] == 1)then verse:set(ev[3],ev[4],newCell(true)) end
  95. if(ev[2] == 2)then verse:set(ev[3],ev[4],newCell(false)) end
  96. drawCell(ev[3],ev[4])
  97. end
  98. end
  99. term.current().setVisible(true)
  100. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement