Game of life
Posted: Sun Dec 31, 2017 8:23 pm
The ten thousand's version of this game, this time in Smart Basic.
Three parameters can be set, controlling the size (n), speed (dt) and initial population (po).
Once more SB proves its incredible speed for an interpreter (setting dt to zero)
For use on iPhones, decrease n sufficiently.
Three parameters can be set, controlling the size (n), speed (dt) and initial population (po).
Once more SB proves its incredible speed for an interpreter (setting dt to zero)
For use on iPhones, decrease n sufficiently.
Code: Select all
' game of live by John Conway
' triggered by the Vintage Basic site
'
n=30 ' board size = n x n cells
po=0.2 ' probability of living cells at initialization
dt=.5 ' pause between frames
ds=floor(748/n)
dim a(n+1,n+1),b(n+1,n+1)
graphics ! graphics clear ! draw color 0,0,0
init()
do turn(a,b) ! turn(b,a) ! until forever
end
def init()
for i=0 to .n ! for j=0 to .n
if rnd(1)<.po then .a(i,j)=1 else .a(i,j)=0
next j ! next i
return
end def
def board(c(,))
graphics clear ! draw size 3
adr(0,.n+1)
draw line 7,7 to .xp,7 ! draw line 7,.xp to .xp,.xp
draw line 7,7 to 7,.xp ! draw line .xp,7 to .xp,.xp
for i=0 to .n ! for j=0 to .n
if c(i,j) then ! adr(i,j) ! draw text "x" at .xp,.yp ! end if
next j ! next i
end def
def turn(a(,),b(,))
board(a) ! pause .dt
for i=0 to .n
if i=0 then imin=.n else imin=i-1
if i=.n then iplus=0 else iplus=i+1
for j=0 to .n
if j=0 then jmin=.n else jmin=j-1
if j=.n then jplus=0 else jplus=j+1
tot=0 ! b(i,j)=a(i,j)
tot+=a(imin,jmin)+a(imin,j)+a(imin,jplus)
tot+=a(i,jmin)+a(i,jplus)
tot+=a(iplus,jmin)+a(iplus,j)+a(iplus,jplus)
if tot<2 or tot>3 then b(i,j)=0
if tot=3 then b(i,j)=1
next j
next i
return
end def
def adr(i,j) ! .xp=15+.ds*j ! .yp=10+.ds*i ! end def