The Game of Life is a   cellular automaton   devised by the British mathematician   John Horton Conway   in 1970.   It is the best-known example of a cellular automaton.

Conway's game of life is described   here:

A cell   C   is represented by a   1   when alive,   or   0   when dead,   in an   m-by-m   (or m×m)   square array of cells.

We calculate   N   - the sum of live cells in C's   eight-location neighbourhood,   then cell   C   is alive or dead in the next generation based on the following table:

   C   N                 new C
   1   0,1             ->  0  # Lonely
   1   4,5,6,7,8       ->  0  # Overcrowded
   1   2,3             ->  1  # Lives
   0   3               ->  1  # It takes three to give birth!
   0   0,1,2,4,5,6,7,8 ->  0  # Barren

Assume cells beyond the boundary are always dead.

The "game" is actually a zero-player game, meaning that its evolution is determined by its initial state, needing no input from human players.   One interacts with the Game of Life by creating an initial configuration and observing how it evolves.



Task

Although you should test your implementation on more complex examples such as the   glider   in a larger universe,   show the action of the blinker   (three adjoining cells in a row all alive),   over three generations, in a 3 by 3 grid.



References

   Its creator John Conway, explains   the game of life.   Video from numberphile on youtube.
   John Conway   Inventing Game of Life   - Numberphile video.


Related task

   Langton's ant   - another well known cellular automaton.


 
PROGRAM LIFE
 
!$INTEGER
 
!$KEY
!for C-64 compatibility
 
CONST Xmax=38,Ymax=20
 
DIM x,y,N
DIM WORLD[39,21],NextWORLD[39,21]
 
BEGIN
 
! Glider test
!------------------------------------------
WORLD[1,1]=1 WORLD[1,2]=0 WORLD[1,3]=0
WORLD[2,1]=0 WORLD[2,2]=1 WORLD[2,3]=1
WORLD[3,1]=1 WORLD[3,2]=1 WORLD[3,3]=0
!------------------------------------------
 
PRINT(CHR$(12);"Press any key to interrupt")
LOOP
  PRINT(CHR$(11);) PRINT
  PRINT(STRING$(Xmax+2,"-"))
  !---------- endless world ---------
  FOR y=1 TO Ymax DO
    WORLD[0,y]=WORLD[Xmax,y]
    WORLD[Xmax+1,y]=WORLD[1,y]
  END FOR
  FOR x=1 TO Xmax DO
    WORLD[x,0]=WORLD[x,Ymax]
    WORLD[x,Ymax+1]=WORLD[x,1]
  END FOR
  WORLD[0,0]=WORLD[Xmax,Ymax]
  WORLD[Xmax+1,Ymax+1]=WORLD[1,1]
  WORLD[Xmax+1,0]=WORLD[1,Ymax]
  WORLD[0,Ymax+1]=WORLD[Xmax,1]
  !---------- endless world ---------
  FOR y=1 TO Ymax DO
    PRINT("|";)
    FOR x=1 TO Xmax DO
      PRINT(CHR$(32+WORLD[x,y]*3);)
      N=WORLD[x-1,y-1]+WORLD[x-1,y]+WORLD[x-1,y+1]+WORLD[x,y-1]
      N=N+WORLD[x,y+1]+WORLD[x+1,y-1]+WORLD[x+1,y]+WORLD[x+1,y+1]
      IF (WORLD[x,y]<>0 AND (N=2 OR N=3)) OR (WORLD[x,y]=0 AND N=3) THEN
        NextWORLD[x,y]=1
      ELSE
        NextWORLD[x,y]=0
      END IF
    END FOR
    PRINT("|")
  END FOR
  PRINT(STRING$(Xmax+2,"-"))
  PAUSE(0.1)
 
  FOR x=0 TO Xmax+1 DO
    FOR y=0 TO Ymax+1 DO
      WORLD[x,y]=NextWORLD[x,y]
      NextWORLD[x,y]=0
    END FOR
  END FOR
  REPEAT
      GET(A$)
  UNTIL A$<>""
  EXIT IF A$=CHR$(27)
END LOOP
 
PRINT("Press any key to exit")
REPEAT
UNTIL GETKEY$<>""
END PROGRAM
 
 