Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- function simulate()
- local m = #cell
- local cell2 = {}
- for i = 1, m do
- cell2[i] = {}
- for j = 1, m do
- cell2[i][j] = cell[i][j]
- end
- end
- for i = 1, m do
- for j = 1, m do
- local count
- if cell2[i][j] == 0 then count = 0 else count = -1 end
- for x = -1, 1 do
- for y = -1, 1 do
- 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
- end
- end
- if count < 2 or count > 3 then cell[i][j] = 0 end
- if count == 3 then cell[i][j] = 1 end
- end
- end
- end
- function init()
- speed=0.25
- local null,x=term.getSize()
- print("Recommended size is "..x-1)
- write("Size:")
- size=tonumber(read())
- cell={}
- for i=1,size do
- cell[i]={}
- for ia=1,size do
- cell[i][ia]=math.random(0,1)
- end
- end
- end
- function draw()
- term.clear()
- term.setCursorPos(1,1)
- for k,v in ipairs(cell) do
- for key,cs in ipairs(v) do
- if cs==0 then
- write(" ")
- else
- write("#")
- end
- end
- print("")
- end
- if not sim then
- write("Paused")
- else
- write("Speed:"..speed)
- end
- term.setCursorPos(xsel,ysel)
- if cell[ysel][xsel]==0 then
- write("+")
- else
- write("@")
- end
- end
- init()
- sim=true
- xsel=1
- ysel=1
- while true do
- if sim then
- simulate()
- end
- os.startTimer(speed)
- e,k=os.pullEvent()
- if e=="key" or e=="char" then
- os.pullEvent("timer")
- if k==keys["up"] then
- if ysel~=1 then
- ysel=ysel-1
- end
- elseif k==keys["down"] then
- if ysel~=size then
- ysel=ysel+1
- end
- elseif k==keys["left"] then
- if xsel~=1 then
- xsel=xsel-1
- end
- elseif k==keys["right"] then
- if xsel~=size then
- xsel = xsel + 1
- end
- elseif k==keys["enter"] then
- if cell[ysel][xsel]==0 then
- cell[ysel][xsel]=1
- else
- cell[ysel][xsel]=0
- end
- elseif k==keys["p"] then
- sim=not sim
- elseif k==13 then
- speed=speed+0.05
- elseif k==12 then
- if speed>0 then
- speed=speed-0.05
- end
- end
- end
- draw()
- end
Advertisement
Add Comment
Please, Sign In to add comment