Init Conway with random

This commit is contained in:
Fabien Freling 2019-03-30 15:01:44 +01:00
parent 333e8eb317
commit 02d0cad4dd

View file

@ -12,9 +12,10 @@ for i=0,width*height-1 do
buffer[i]=0 buffer[i]=0
end end
-- initial state -- initial state
buffer[11+10*width]=1 math.randomseed(1234)
buffer[12+10*width]=1 for i=0,2500 do
buffer[13+10*width]=1 buffer[math.random(0, width*height-1)]=1
end
function display() function display()
for y=0,height-1 do for y=0,height-1 do
@ -29,18 +30,16 @@ function display()
end end
function compute_ib() function compute_ib()
for i=0,width*height-1 do ib[0]=buffer[0]
ib[i]=buffer[i]
end
for x=1,width-1 do for x=1,width-1 do
ib[x]=ib[x]+ib[x-1] ib[x]=buffer[x]+ib[x-1]
end end
for y=1,height-1 do for y=1,height-1 do
ib[y*width]=ib[y*width]+ib[(y-1)*width] ib[y*width]=buffer[y*width]+ib[(y-1)*width]
end end
for y=1,height-1 do for y=1,height-1 do
for x=1,width-1 do for x=1,width-1 do
ib[x+y*width]=ib[x+y*width]+ib[x-1+y*width]+ib[x+(y-1)*width] ib[x+y*width]=buffer[x+y*width]+ib[x-1+y*width]+ib[x+(y-1)*width]-ib[x-1+(y-1)*width]
end end
end end
end end
@ -57,7 +56,7 @@ function update()
buffer[i]=0 buffer[i]=0
elseif n == 3 then elseif n == 3 then
buffer[i]=1 buffer[i]=1
else elseif n > 3 then
buffer[i]=0 buffer[i]=0
end end
end end
@ -66,10 +65,7 @@ end
function TIC() function TIC()
display() display()
if btn(0) then update() end if t%30==0 then update() end
if btn(1) then update() end
if btn(2) then update() end
if btn(3) then update() end
t=t+1 t=t+1
end end