Page 1 of 1

Game of life

Posted: Sun Dec 31, 2017 8:23 pm
by Henko
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.

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

Re: Game of life

Posted: Mon Jan 01, 2018 12:16 pm
by Henko
Here is a somewhat better and nicer version.
The speed can be modified at runtime. Rendering with a pause of zero is unstable, but the content maintains integrity.
The size of the board (n=21) is set for the iPhone 5. On iPad a size of n=53 is the maximum. For other devices: try different values for n.

Code: Select all

' game of life by John Conway
' triggered by the Vintage Basic site
' version january 1st, 2018 by Henko
' 
n=21     '  board size = n x n cells
po=0.2   '  probability of living cell at initialization
dt=.5    '  pause between frames (can be modified at runtime)
dim a(n+1,n+1),b(n+1,n+1)
init()
do turn(a,b) ! turn(b,a) ! until forever
end

def init()
graphics ! graphics clear ! draw color 0,0,0 ! draw font size 10
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
.ds=14 ! w=.n*.ds ! xb=w/2-70 ! yb=w+60 ! .t$=chr$(58412)
button "minus" text "-" at xb,yb size 50,50
button "plus" text "+" at xb+120,yb size 50,50
return
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 board(x(,))
graphics clear ! draw size 3
adr(0,.n+1) ! xb=.xp+2
draw line 5,5 to xb,5 ! draw line 5,xb to xb,xb
draw line 5,5 to 5,xb ! draw line xb,5 to xb,xb
for i=0 to .n ! for j=0 to .n
  if x(i,j) then ! adr(i,j) ! draw text .t$ at .xp,.yp ! end if
  next j ! next i
if button_pressed("plus") then tmut(0.1)
if button_pressed("minus") then tmut(-0.1)
end def

def tmut(delta)
.dt=max(0,.dt+delta)
draw font size 20
draw text .dt at init.xb+70,init.yb+15
draw font size 10
end def

def adr(i,j) ! .xp=8+.ds*j ! .yp=10+.ds*i ! end def