Maybe you have to use the Sleep command.
If you brake down the rules of Conway's game of life it becomes:
if neighbours=3 or (Cell=1 and neighbours=2) then newCell=1 else newCell=0
Or with other words a cell with 3 neighbours wins every game cycle, independent the cell was died before or not. ;-)
By the way it's a good example that an short/optimized term must not be the fastes !
(For the code/CPU it's faster to ask if the cell is alive=1 or died=0 before.)
Joshy
file: game_of_life.bas
Code: Select all
sub GameOfLife(oldGenerationImage as any ptr, _
newGenerationImage as any ptr)
dim as integer iW,iH,iPitch
dim as ubyte ptr pI,pO
imageinfo oldGenerationImage,iw,ih,,iPitch,pI
imageinfo newGenerationImage,iw,ih,, ,pO
dim as ubyte ptr ri0=pI+(iH-1)*iPitch ' input row[last]
dim as ubyte ptr ri1=pI ' input row[0]
dim as ubyte ptr ri2=ri1+iPitch ' input row[1]
dim as ubyte ptr ro1=pO ' output row[0]
dim as ubyte cell
dim as integer neighbours,iLeft,iMidle,iRight
#macro one_cell
neighbours = ri0[iLeft]: neighbours += ri0[iMidle]: neighbours += ri0[iRight]
neighbours += ri1[iLeft]: cell = ri1[iMidle]: neighbours += ri1[iRight]
neighbours += ri2[iLeft]: neighbours += ri2[iMidle]: neighbours += ri2[iRight]
if cell=1 then
if (neighbours<2 orelse neighbours>3) then ro1[iMidle]=0 else ro1[iMidle]=1
elseif neighbours=3 then
ro1[iMidle]=1
else
ro1[iMidle]=0
endif
#endmacro
#macro one_row
iLeft=iW-1:iMidle=0:iRight=1
for x as integer = 1 to iW-1
one_cell
iLeft=iMidle:iMidle=iRight:iRight+=1
next
iRight=0 : one_cell
#endmacro
for y as integer = 1 to iH-1
one_row
ri0=ri1 : ri1=ri2 : ri2+=iPitch : ro1+=iPitch
next
ri2=pI ' last row becomes row[0]
one_row
end sub
'
' main
'
dim as integer iW,iH
screeninfo iW,iH
randomize timer()
' 1/2 size
iW=(iW\2) : iH=(ih\2)
' 3/4 size
'iW=(iW\4)*3 : iH=(ih\4)*3
screenres iW,iH,8
palette 1,255,255,255
var oldPopulation = ImageCreate(iW,iH,0)
var newPopulation = ImageCreate(iW,iH,0)
for i as integer = 1 to 100000
pset oldPopulation,(100+rnd*(iW-200),100+rnd*(iH-200)),1
next
dim as integer fps,frames
dim as double tStart = timer()
while inkey()=""
GameOfLife(oldPopulation,newPopulation)
put (0,0),newPopulation,PSET
draw string (0,0),"fps: " & fps
swap oldPopulation,newPopulation
frames+=1
if (frames mod 60)=0 then
dim as double tNow=timer()
fps=60/(tNow-tStart)
tStart=tNow
end if
'sleep 1
wend