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.


(subscriptrange):
Conway: procedure options (main);        /* 20 November 2013 */
   /* A grid of (1:100, 1:100) is desired; the array GRID is defined as (0:101, 0:101), */
   /* to satisfy the requirement that elements off-grid are zero.                       */
   declare n fixed binary; /* grid size) */
 
   put ('What grid size do you want?');
   get (n);
   put skip list ('Generating a grid of size ' || trim(n) );
 
begin;
   declare grid (0:n+1,0:n+1) bit(1) initial ((*) '0'b);
   declare new  (0:n+1,0:n+1) bit(1);
   declare cell(3,3) defined grid(1sub-2+i, 2sub-2+j) bit (1);
   declare (i, j, k) fixed binary;
 
   /* Initialize some cells. */
   grid(2,2) = '1'b; grid(2,3) = '1'b; grid(2,4) = '1'b;
 
   /* Print the initial state. */
   put list ('Initial pattern:');
   do i = 1 to n;
      put skip;
      do j = 1 to n;
         put edit (grid(i,j)) (b(1));
      end;
   end;
 
   do k = 1 to 4;
      /* Do one generation of life */
      new = '0'b;
      /* For each C, the center of a 3 x 3 cell matrix. */
      do i = 1 to n;
         do j = 1 to n;
            if grid(i,j) then
               select (sum(cell)-1);
                  when (0,1)       new(i,j) = '0'b;
                  when (4,5,6,7,8) new(i,j) = '0'b;
                  when (2,3)       new(i,j) = '1'b;
               end;
            else
               select (sum(cell));
                  when (3)         new(i,j) = '1'b;
                  otherwise        new(i,j) = '0'b;
               end;
         end;
      end;
      grid = new; /* Update GRID with the new generation. */
 
      /* Print the generation. */
      put skip(2) list ('Generation ' || trim(k));
      do i = 1 to n;
         put skip;
         do j = 1 to n;
            put edit (grid(i,j)) (b(1));
         end;
      end;
   end;
end;	
end Conway;