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.


;{Conway's Game of Life in Fermat}
;{square grid with wrap-around boundaries}
 
size:=50;                         {how big a grid do you want? This fits my screen OK, change this for your own screen}
 
Array w1[size,size], w2[size,size];      {set up an active world and a 'scratchpad' world}
act:=1;
buf:=2;
%[1]:=[w1];                      {Fermat doesn't have 3D arrays in the normal sense--}
%[2]:=[w2];                      {we need to use the somewhat odd "array of arrays" functionality}
 
Func Cls = for i = 1 to size do !!; od.;     {"clear screen" by printing a bunch of newlines}
 
Func Draw =                     {draw the active screen}
    for i = 1 to size do
        for j = 1 to size do
            if %[act][i, j] = 1 then !('# ') else !('. ') fi;
        od;
        !;
    od;
.;
 
Func Rnd =                                   {randomize the grid with a density of 40% live cells}
    for i = 1 to size do
        for j = 1 to size do
            if Rand|5<2 then %[act][i, j] := 1 else %[act][i, j] := 0 fi;
        od;
    od;
    Cls;
    Draw;
.;
 
Func Blinker =                               {clears the screen except for a blinker in the top left corner}
    for i = 1 to size do
        for j = 1 to size do
            %[act][i, j] := 0;
        od;
    od;
    %[act][1,2] := 1;
    %[act][2,2] := 1;
    %[act][3,2] := 1;
    Cls;
    Draw;
.;
 
Func Iter =                                 {do one iteration}
    for i = 1 to size do
        if i = 1 then im := size else im := i - 1 fi;        {handle wrap around}
        if i = size then ip := 1 else ip := i + 1 fi;
        for j = 1 to size do
            if j = 1 then jm := size else jm := j - 1 fi;
            if j = size then jp := 1 else jp := j + 1 fi;
            neigh :=  %[act][im, jm];                        {count neigbours}
            neigh :+ (%[act][im, j ]);
            neigh :+ (%[act][im, jp]);
            neigh :+ (%[act][i , jm]);
            neigh :+ (%[act][i , jp]);
            neigh :+ (%[act][ip, jm]);
            neigh :+ (%[act][ip, j ]);
            neigh :+ (%[act][ip, jp]);
            if neigh < 2 or neigh > 3 then %[buf][i, j] := 0 fi;      {alive and dead rules}
            if neigh = 2 then %[buf][i, j] := %[act][i, j] fi;
            if neigh = 3 then %[buf][i, j] := 1 fi;                              
        od;
    od;
    Swap(act, buf);     {rather than copying the scratch over into the active, just exchange their identities}
    Cls;
    Draw;
.;
 
choice := 9;
while choice <> 0 do              {really rough menu, not the point of this exercise}
    ?choice;
    if choice=4 then Cls fi;
    if choice=3 then Blinker fi;
    if choice=2 then Rnd fi;
    if choice=1 then Iter fi;
od;
 
!!'John Horton Conway (26 December 1937 – 11 April 2020)';
 