Legebatterie

apps--GameOfLife

Mar 16th, 2019
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.77 KB | None | 0 0
  1. reset=function()
  2. term.setBackgroundColor(colors.black)
  3. term.clear()
  4. Newcells={}
  5. Ocells={}
  6. counter=0
  7. end
  8.  
  9.  
  10.  
  11. randomField=function()
  12. for i=1,969 do
  13. xc= math.fmod(i,51)
  14. if xc==0 then xc=51 end
  15. yc=math.ceil(i/51)
  16. if math.random(0,99)<chance then
  17. paintutils.drawPixel(xc,yc,colors.green)
  18. Ocells[xc+((yc-1)*51)]=1
  19. else
  20. paintutils.drawPixel(xc,yc,colors.black)
  21. Ocells[xc+((yc-1)*51)]=nil
  22. end
  23. end
  24. end
  25.  
  26.  
  27.  
  28. loop=function()
  29. parallel.waitForAny(input,evolute)
  30.  
  31. if ki==57 then drawMode()
  32. elseif ki==16 then error()
  33. elseif ki==35 then openHelp()
  34. elseif ki==46 then reset() drawMode()
  35. elseif ki==2 then sleepTime=(ki-1)*0.1 loop()
  36. elseif ki==3 then sleepTime=(ki-1)*0.1 loop()
  37. elseif ki==4 then sleepTime=(ki-1)*0.1 loop()
  38. elseif ki==5 then sleepTime=(ki-1)*1.5 loop()
  39. elseif ki==6 then sleepTime=(ki-1)*1.5 loop()
  40. elseif ki==7 then sleepTime=(ki-1)*1.5 loop()
  41. elseif ki==8 then sleepTime=(ki-1)*0.2 loop()
  42. elseif ki==9 then sleepTime=(ki-1)*0.2 loop()
  43. elseif ki==10 then sleepTime=(ki-1)*0.2 loop()
  44. elseif ki==11 then sleepTime=(ki-1)*0.2 loop()
  45. end
  46. end
  47.  
  48. drawMode=function()
  49. var1=true
  50. while var1 do
  51. event,n,x,y=os.pullEvent()
  52. if event=="mouse_click" or "mouse_drag" then
  53. if n==1 then
  54. paintutils.drawPixel(x,y,colors.green)
  55. Ocells[x+((y-1)*51)]=1
  56. elseif n==2 then
  57. paintutils.drawPixel(x,y,colors.black)
  58. Ocells[x+((y-1)*51)]=nil
  59. end
  60. end
  61. if event=="key" then
  62. if n==57 then --space
  63. loop()
  64. elseif n==35 then --h
  65. openHelp()
  66. elseif n==16 then --q
  67. error()
  68. elseif n==46 then --c
  69. reset()
  70. elseif n==31 then --s
  71. openSettings()
  72. elseif n==19 then --r
  73. randomField()
  74. end
  75. end
  76. end
  77. end
  78.  
  79.  
  80.  
  81. evolute=function()
  82. while true do
  83. sleep(sleepTime)
  84. counter=counter+1
  85. term.setCursorPos(1,19)
  86. term.setTextColor(colors.blue)
  87. write(counter)
  88. for i=1,969 do
  89. xc= math.fmod(i,51)
  90. if xc==0 then xc=51 end
  91. yc=math.ceil(i/51)
  92. neighbours=0
  93. if not(Ocells[i-52]==nil) then neighbours=neighbours+1 end
  94. if not(Ocells[i-51]==nil) then neighbours=neighbours+1 end
  95. if not(Ocells[i-50]==nil) then neighbours=neighbours+1 end
  96. if not(Ocells[i- 1]==nil) then neighbours=neighbours+1 end
  97. if not(Ocells[i+ 1]==nil) then neighbours=neighbours+1 end
  98. if not(Ocells[i+52]==nil) then neighbours=neighbours+1 end
  99. if not(Ocells[i+51]==nil) then neighbours=neighbours+1 end
  100. if not(Ocells[i+50]==nil) then neighbours=neighbours+1 end
  101. if Ocells[i]==nil then
  102. if neighbours==3 then
  103. Newcells[i]=1
  104. paintutils.drawPixel(xc,yc,colors.green)
  105. else
  106. if math.random(0,999)<birth then
  107. Newcells[i]=1
  108. paintutils.drawPixel(xc,yc,colors.green)
  109. else
  110. Newcells[i]=nil
  111. end
  112. end
  113. end
  114. if not(Ocells[i]==nil) then
  115. if neighbours>3 or neighbours<2 then
  116. Newcells[i]=nil
  117. paintutils.drawPixel(xc,yc,colors.black)
  118. else
  119. Newcells[i]=1
  120. end
  121. end
  122. end
  123. for i=1,969 do
  124. Ocells[i]=Newcells[i]
  125. end
  126. end
  127. end
  128.  
  129.  
  130.  
  131. input=function()
  132. event,ki=os.pullEvent("key")
  133. return ki
  134. end
  135.  
  136.  
  137.  
  138. openHelp=function()
  139. term.clear()
  140. term.setCursorPos(1,1)
  141. term.setBackgroundColor(colors.black)
  142. term.setTextColor(colors.white)
  143. print("RULES:")
  144. print("cell with more than 3 neighbours dies")
  145. print("cell with less then 2 neighbours dies")
  146. print("dead cell with 3 neighbours will be reborn")
  147. print("")
  148. print("press h to open this Help-Menue")
  149. print("press s to open Settings-Menue")
  150. print("press q to leave the program")
  151. print("press c to kill all cells")
  152. print("press r to create a random pattern")
  153. print("press space to start/stop animation")
  154. print("press a number to change evolution speed")
  155. print("")
  156. print("press any key to continue")
  157. event,n=os.pullEvent("key")
  158. reset()
  159. drawMode()
  160. end
  161.  
  162.  
  163. openSettings=function()
  164. term.clear()
  165. term.setCursorPos(1,1)
  166. term.setBackgroundColor(colors.black)
  167. term.setTextColor(colors.white)
  168. print("SETTINGS:")
  169. print("")
  170. print("random: chance for living cell in %:")
  171. print("")
  172. print("random birth chance in proMill:")
  173. print("")
  174. print("senility after x generations. x=")
  175. print("")
  176. print("pres any key to contniue")
  177.  
  178. var2=true
  179. while var2 do
  180. e,n,x,y=os.pullEvent()
  181. if e=="key" then
  182. var2=false
  183. end
  184. if e=="mouse_click" then
  185. if y==3 then
  186. term.setCursorPos(37,y)
  187. chance=tonumber(read())
  188. var2=false
  189. elseif y==5 then
  190. term.setCursorPos(33,y)
  191. birth=tonumber(read())
  192. var2=false
  193. elseif y==7 then
  194. term.setCursorPos(35,y)
  195. senility=tonumber(read())
  196. var2=false
  197.  
  198. end
  199. end
  200. end
  201. reset()
  202. drawMode()
  203. end
  204.  
  205.  
  206. Ocells={}
  207. Newcells={}
  208. sleepTime=0.5
  209. chance=33
  210. birth=0
  211. senility=0
  212. counter=0
  213. openHelp()
Add Comment
Please, Sign In to add comment