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.


COMMENT A PORT OF AN R6RS SCHEME IMPLEMENTATION OF CONWAY'S GAME OF LIFE TO SIMULA --- ASSUMES ;
COMMENT ALL CELLS OUTSIDE THE DEFINED GRID ARE DEAD ;
 
BEGIN
 
 
COMMENT FIRST WE NEED CONS, CAR, CDR, LENGTH AND MAP TO SIMULATE SCHEME ;
 
CLASS ATOM;;
 
ATOM CLASS CELL(ALIVE); INTEGER ALIVE;;
 
ATOM CLASS LIST(A1, A2); REF(ATOM) A1, A2;;
 
REF(LIST) PROCEDURE CONS(A1, A2); REF(ATOM) A1, A2;
    CONS :- NEW LIST(A1, A2);
 
REF(ATOM) PROCEDURE CAR(LST); REF(LIST) LST;
    CAR :- LST.A1;
 
REF(ATOM) PROCEDURE CDR(LST); REF(LIST) LST;
    CDR :- LST.A2;
 
INTEGER PROCEDURE LENGTH(LST); REF(LIST) LST;
    LENGTH := IF LST == NONE THEN 0 ELSE 1 + LENGTH(CDR(LST));
 
REF(LIST) PROCEDURE MAP(F, LST); PROCEDURE F IS REF(ATOM) PROCEDURE F(ARG); REF(ATOM) ARG;; REF(LIST) LST;
    MAP :- IF LST == NONE THEN NONE ELSE CONS(F(CAR(LST)), MAP(F, CDR(LST)));
 
 
 
COMMENT NOW FOLLOWS THE PROBLEM SPECIFIC PART ;
 
 
COMMENT IF N IS OUTSIDE BOUNDS OF LIST, RETURN 0 ELSE VALUE AT N ;
REF(ATOM) PROCEDURE NTH(N, LST); INTEGER N; REF(LIST) LST;
    NTH :- IF N > LENGTH(LST) THEN NEW CELL(0) ELSE
           IF N < 1 THEN NEW CELL(0) ELSE
           IF N = 1 THEN CAR(LST) ELSE NTH(N - 1, CDR(LST));
 
 
COMMENT RETURN THE NEXT STATE OF THE SUPPLIED UNIVERSE ;
REF(LIST) PROCEDURE NEXTUNIVERSE(UNIVERSE); REF(LIST) UNIVERSE;
BEGIN
 
    COMMENT VALUE AT (X, Y) ;
    INTEGER PROCEDURE VALUEAT(X, Y); INTEGER X, Y;
    BEGIN
        REF(ATOM) A;
        A :- NTH(Y, UNIVERSE);
        INSPECT A
            WHEN LIST DO
                VALUEAT := NTH(X, THIS LIST) QUA CELL.ALIVE
            OTHERWISE
                VALUEAT := 0;
    END VALUEAT;
 
    COMMENT SUM OF THE VALUES OF THE CELLS SURROUNDING (X, Y) ;
    INTEGER PROCEDURE NEIGHBORSUM(X, Y); INTEGER X, Y;
        NEIGHBORSUM :=
            VALUEAT(X - 1, Y - 1)
          + VALUEAT(X - 1, Y    )
          + VALUEAT(X - 1, Y + 1)
          + VALUEAT(X,     Y - 1)
          + VALUEAT(X,     Y + 1)
          + VALUEAT(X + 1, Y - 1)
          + VALUEAT(X + 1, Y    )
          + VALUEAT(X + 1, Y + 1);
 
    COMMENT NEXT STATE OF THE CELL AT (X, Y) ;
    INTEGER PROCEDURE NEXTCELL(X, Y); INTEGER X, Y;
    BEGIN
        INTEGER CUR, NS;
        CUR := VALUEAT(X, Y);
        NS := NEIGHBORSUM(X, Y);
        NEXTCELL := IF CUR = 1 AND (NS < 2 OR NS > 3) THEN 0 ELSE
                    IF CUR = 0 AND NS = 3 THEN 1 ELSE CUR;
    END NEXTCELL;
 
    COMMENT NEXT STATE OF ROW N ;
    REF(LIST) PROCEDURE ROW(N, OUT); INTEGER N; REF(LIST) OUT;
    BEGIN
        INTEGER W, O;
        W := LENGTH(CAR(UNIVERSE));
        O := LENGTH(OUT);
        ROW :- IF W = O THEN OUT ELSE ROW(N, CONS(NEW CELL(NEXTCELL(W - O, N)), OUT));
    END ROW;
 
 
    COMMENT A RANGE OF INTS FROM BOT TO TOP ;
    REF(LIST) PROCEDURE INTRANGE(BOT, TOP); INTEGER BOT, TOP;
        INTRANGE :- IF BOT > TOP THEN NONE ELSE CONS(NEW CELL(BOT), INTRANGE(BOT + 1, TOP));
 
    REF(ATOM) PROCEDURE LAMBDA(N); REF(ATOM) N;
        LAMBDA :- ROW(N QUA CELL.ALIVE, NONE);
 
    NEXTUNIVERSE :- MAP(LAMBDA, INTRANGE(1, LENGTH(UNIVERSE)));
 
END NEXTUNIVERSE;
 
COMMENT DISPLAY THE UNIVERSE ;
PROCEDURE DISPLAY(LST); REF(LIST) LST;
BEGIN
    WHILE LST =/= NONE DO
    BEGIN
        REF(LIST) LI;
        LI :- CAR(LST);
        WHILE LI =/= NONE DO
        BEGIN
            REF(CELL) CE;
            CE :- CAR(LI);
            OUTCHAR(IF CE.ALIVE = 1 THEN '#' ELSE '-');
            LI :- CDR(LI);
        END;
        OUTIMAGE;
        LST :- CDR(LST);
    END;
END DISPLAY;
 
COMMENT STARTING WITH SEED, SHOW REPS STATES OF THE UNIVERSE ;
PROCEDURE CONWAY(SEED, REPS); REF(LIST) SEED; INTEGER REPS;
BEGIN
    IF REPS > 0 THEN
    BEGIN
        DISPLAY(SEED);
        OUTIMAGE;
        CONWAY(NEXTUNIVERSE(SEED), REPS - 1);
    END;
END CONWAY;
 
REF(CELL) O, L;
O :- NEW CELL(0);
L :- NEW CELL(1);
 
COMMENT BLINKER IN A 3X3 UNIVERSE ;
! (CONWAY '((0 1 0)
            (0 1 0)
            (0 1 0)) 5) ;
CONWAY(CONS(CONS(O, CONS(L, CONS(O, NONE))),
       CONS(CONS(O, CONS(L, CONS(O, NONE))),
       CONS(CONS(O, CONS(L, CONS(O, NONE))),
       NONE))), 5);
 
COMMENT GLIDER IN AN 8X8 UNIVERSE ;
!(CONWAY '((0 0 1 0 0 0 0 0)
           (0 0 0 1 0 0 0 0)
           (0 1 1 1 0 0 0 0)
           (0 0 0 0 0 0 0 0)
           (0 0 0 0 0 0 0 0)
           (0 0 0 0 0 0 0 0)
           (0 0 0 0 0 0 0 0)
           (0 0 0 0 0 0 0 0)) 30) ;
OUTIMAGE;
 
CONWAY(CONS(CONS(O, CONS(O, CONS(L, CONS(O, CONS(O, CONS(O, CONS(O, CONS(O, NONE)))))))),
       CONS(CONS(O, CONS(O, CONS(O, CONS(L, CONS(O, CONS(O, CONS(O, CONS(O, NONE)))))))),
       CONS(CONS(O, CONS(L, CONS(L, CONS(L, CONS(O, CONS(O, CONS(O, CONS(O, NONE)))))))),
       CONS(CONS(O, CONS(O, CONS(O, CONS(O, CONS(O, CONS(O, CONS(O, CONS(O, NONE)))))))),
       CONS(CONS(O, CONS(O, CONS(O, CONS(O, CONS(O, CONS(O, CONS(O, CONS(O, NONE)))))))),
       CONS(CONS(O, CONS(O, CONS(O, CONS(O, CONS(O, CONS(O, CONS(O, CONS(O, NONE)))))))),
       CONS(CONS(O, CONS(O, CONS(O, CONS(O, CONS(O, CONS(O, CONS(O, CONS(O, NONE)))))))),
       CONS(CONS(O, CONS(O, CONS(O, CONS(O, CONS(O, CONS(O, CONS(O, CONS(O, NONE)))))))),
       NONE)))))))), 30);
 
END.
 