Init Conway with random
This commit is contained in:
parent
333e8eb317
commit
02d0cad4dd
24
conway.lua
24
conway.lua
|
@ -12,9 +12,10 @@ for i=0,width*height-1 do
|
|||
buffer[i]=0
|
||||
end
|
||||
-- initial state
|
||||
buffer[11+10*width]=1
|
||||
buffer[12+10*width]=1
|
||||
buffer[13+10*width]=1
|
||||
math.randomseed(1234)
|
||||
for i=0,2500 do
|
||||
buffer[math.random(0, width*height-1)]=1
|
||||
end
|
||||
|
||||
function display()
|
||||
for y=0,height-1 do
|
||||
|
@ -29,18 +30,16 @@ function display()
|
|||
end
|
||||
|
||||
function compute_ib()
|
||||
for i=0,width*height-1 do
|
||||
ib[i]=buffer[i]
|
||||
end
|
||||
ib[0]=buffer[0]
|
||||
for x=1,width-1 do
|
||||
ib[x]=ib[x]+ib[x-1]
|
||||
ib[x]=buffer[x]+ib[x-1]
|
||||
end
|
||||
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
|
||||
for y=1,height-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
|
||||
|
@ -57,7 +56,7 @@ function update()
|
|||
buffer[i]=0
|
||||
elseif n == 3 then
|
||||
buffer[i]=1
|
||||
else
|
||||
elseif n > 3 then
|
||||
buffer[i]=0
|
||||
end
|
||||
end
|
||||
|
@ -66,10 +65,7 @@ end
|
|||
|
||||
function TIC()
|
||||
display()
|
||||
if btn(0) then update() end
|
||||
if btn(1) then update() end
|
||||
if btn(2) then update() end
|
||||
if btn(3) then update() end
|
||||
if t%30==0 then update() end
|
||||
t=t+1
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue