remstart
--------
Functions
   * _border() - draws a border around the grid
--------
Notes
   * Make rules adjustable in editor
   * Make zoom function
   * Enable editing mid-sequence
remend
 
set emulation off
hide mouse
sync on : sync rate 60
 
pmax=200
DIM pixel(pmax+1,pmax+1)
DIM pixelBuff(pmax+1,pmax+1)
 
DIM color(1)
color(1) = rgb(255,255,0)
 
gen=0
 
`Make Cursor
ink 65536*255,0
dot 1,0
dot 0,1
dot 1,2
dot 2,1
get image 1,0,0,3,3
 
`-------------------------------
` EDITOR
`-------------------------------
rem - use the editor to experiment with different start images
Repeat
   gosub _border
   sprite 1,mousex()-1,mousey()-1,1
 
   if mouseclick()>0
      click = mouseclick() : if click>1 then click=0
 
      mx=mousex() : my=mousey()
      if mx>0 and my>0 and mx<pmax+1 and my<pmax+1
         pixel(mx,my)= click
         ink color(pixel(mx,my)),0
         dot mx,my
      endif
   endif
   sync
Until returnkey()=1
 
`-------------------------------
` MAIN LOOP
`-------------------------------
sync rate 0
hide sprite 1
 
DO
ink 30,0 : box 0,0,pmax+150,pmax+2
gosub _border
 
For y = 1 to pmax
   for x = 1 to pmax
      neighbours = 0
      if pixel(x,y)=1 then dec neighbours : `remove selected pixel from equation
      for oy = -1 to 1
         for ox = -1 to 1
            if pixel(x+ox,y+oy)=1 then inc neighbours
         next ox
      next oy
 
      `Apply Rules
      REMSTART
      * Conway's rule set
         For a space that is 'populated':
           Each cell with one or no neighbors dies, as if by loneliness.
           Each cell with four or more neighbors dies, as if by overpopulation.
           Each cell with two or three neighbors survives.
         For a space that is 'empty' or 'unpopulated'
           Each cell with three neighbors becomes populated.
      ---
      * Rule Types
         =0 : cell is now dead
         =1 : cell is now alive
         =pixel(x,y) : status of the cell stays the same
         =1-pixel(x,y) : a dead cell becomes live; a live one becomes dead
      REMEND
      Select neighbours
         case 0 : pixelBuff(x,y)=0 : endcase
         case 1 : pixelBuff(x,y)=0 : endcase
         case 2 : pixelBuff(x,y)=pixel(x,y) : endcase
         case 3 : pixelBuff(x,y)=1 : endcase
         case 4 : pixelBuff(x,y)=0 : endcase
         case 5 : pixelBuff(x,y)=0 : endcase
         case 6 : pixelBuff(x,y)=0 : endcase
         case 7 : pixelBuff(x,y)=0 : endcase
         case 8 : pixelBuff(x,y)=0 : endcase
      Endselect
 
      if pixel(x,y)>0
         ink color(pixel(x,y)),0
         dot x,y
      endif
   next x
next y
 
for y = 1 to pmax
   for x = 1 to pmax
      pixel(x,y) = pixelBuff(x,y)
   next x
next y
 
inc gen,1
ink color(1),0
set cursor pmax+20,pmax-10
print "Generations: "; gen
wait 20
sync
LOOP
 
`-------------------------------
` GOSUBS
`-------------------------------
_border:
   ink 255,0
   l=0 : t=0 : r=pmax+1 : b=pmax+1
   line l,t,r,t : line r,t,r,b : line r,b,l,b : line l,b,l,t
RETURN