Sxw1212

Life

Sep 29th, 2012
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. function simulate()
  2. local m = #cell
  3. local cell2 = {}
  4. for i = 1, m do
  5. cell2[i] = {}
  6. for j = 1, m do
  7. cell2[i][j] = cell[i][j]
  8. end
  9. end
  10.  
  11. for i = 1, m do
  12. for j = 1, m do
  13. local count
  14. if cell2[i][j] == 0 then count = 0 else count = -1 end
  15. for x = -1, 1 do
  16. for y = -1, 1 do
  17. if i+x >= 1 and i+x <= m and j+y >= 1 and j+y <= m and cell2[i+x][j+y] == 1 then count = count + 1 end
  18. end
  19. end
  20. if count < 2 or count > 3 then cell[i][j] = 0 end
  21. if count == 3 then cell[i][j] = 1 end
  22. end
  23. end
  24. end
  25.  
  26. function init()
  27. speed=0.25
  28. local null,x=term.getSize()
  29. print("Recommended size is "..x-1)
  30. write("Size:")
  31. size=tonumber(read())
  32. cell={}
  33. for i=1,size do
  34. cell[i]={}
  35. for ia=1,size do
  36. cell[i][ia]=math.random(0,1)
  37. end
  38. end
  39. end
  40.  
  41. function draw()
  42. term.clear()
  43. term.setCursorPos(1,1)
  44. for k,v in ipairs(cell) do
  45. for key,cs in ipairs(v) do
  46. if cs==0 then
  47. write(" ")
  48. else
  49. write("#")
  50. end
  51. end
  52. print("")
  53. end
  54. if not sim then
  55. write("Paused")
  56. else
  57. write("Speed:"..speed)
  58. end
  59. term.setCursorPos(xsel,ysel)
  60. if cell[ysel][xsel]==0 then
  61. write("+")
  62. else
  63. write("@")
  64. end
  65. end
  66.  
  67. init()
  68. sim=true
  69. xsel=1
  70. ysel=1
  71. while true do
  72. if sim then
  73. simulate()
  74. end
  75. os.startTimer(speed)
  76. e,k=os.pullEvent()
  77. if e=="key" or e=="char" then
  78. os.pullEvent("timer")
  79. if k==keys["up"] then
  80. if ysel~=1 then
  81. ysel=ysel-1
  82. end
  83. elseif k==keys["down"] then
  84. if ysel~=size then
  85. ysel=ysel+1
  86. end
  87. elseif k==keys["left"] then
  88. if xsel~=1 then
  89. xsel=xsel-1
  90. end
  91. elseif k==keys["right"] then
  92. if xsel~=size then
  93. xsel = xsel + 1
  94. end
  95. elseif k==keys["enter"] then
  96. if cell[ysel][xsel]==0 then
  97. cell[ysel][xsel]=1
  98. else
  99. cell[ysel][xsel]=0
  100. end
  101. elseif k==keys["p"] then
  102. sim=not sim
  103. elseif k==13 then
  104. speed=speed+0.05
  105. elseif k==12 then
  106. if speed>0 then
  107. speed=speed-0.05
  108. end
  109. end
  110. end
  111. draw()
  112. end
Advertisement
Add Comment
Please, Sign In to add comment