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.


/*REXX program runs and displays the Conway's game of life, it stops after  N  repeats. */
signal on halt                                   /*handle a  cell growth  interruptus.  */
parse arg peeps  '(' rows  cols  empty  life!  clearScreen  repeats  generations .
       rows =        p(rows               3)     /*the maximum number of cell  rows.    */
       cols =        p(cols               3)     /* "     "       "    "   "  columns.  */
        emp = pickChar(empty        'blank')     /*an empty cell character  (glyph).    */
   clearScr =        p(clearScreen        0)     /* "1"   indicates to clear the screen.*/
      life! = pickChar(life!            '☼')     /*the gylph kinda looks like an amoeba.*/
       reps =        p(repeats            2)     /*stop pgm  if there are  two  repeats.*/
generations =        p(generations      100)     /*the number of  generations  allowed. */
sw= max(linesize() - 1,  cols)                   /*usable screen width for the display. */
#reps= 0;        $.= emp                         /*the universe is new,  ··· and barren.*/
gens=abs(generations)                            /*used for a  programming  convenience.*/
x= space(peeps); upper x                         /*elide superfluous spaces; uppercase. */
if x==''         then x= "BLINKER"               /*if nothing specified,  use  BLINKER. */
if x=='BLINKER'  then x= "2,1 2,2 2,3"
if x=='OCTAGON'  then x= "1,5 1,6 2,4 2,7 3,3 3,8 4,2 4,9 5,2 5,9 6,3 6,8 7,4 7,7 8,5 8,6"
call assign.                                     /*assign the initial state of all cells*/
call showCells                                   /*show the  initial state of the cells.*/
     do life=1  for gens;      call assign@      /*construct  next  generation of cells.*/
     if generations>0 | life==gens  then call showCells    /*should cells be displayed? */
     end   /*life*/                              /* [↑]  cell colony grows, lives, dies.*/
exit                                             /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
showCells: if clearScr  then 'CLS'               /*  ◄───  change 'command' for your OS.*/
           call showRows                         /*show the rows in the proper order.   */
           say right(copies('▒', sw)  life, sw)  /*show a fence between the generations.*/
           if _==''  then exit                   /*if there's no life, then stop the run*/
           if !._    then #reps= #reps + 1       /*we detected a repeated cell pattern. */
           !._= 1                                /*existence  state and compare  later. */
           if reps\==0 & #reps<=reps then return /*so far, so good,   regarding repeats.*/
           say
           say center('"Life" repeated itself' reps "times, simulation has ended.",sw,'▒')
           exit                                  /*stick a fork in it,  we're all done. */
/*───────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────*/
$:         parse arg _row,_col;                    return $._row._col==life!
assign$:   do r=1  for rows;   do c=1  for cols;   $.r.c= @.r.c;                                    end;    end;       return
assign.:   do while x\==''; parse var x r "," c x; $.r.c=life!; rows=max(rows,r); cols=max(cols,c); end; life=0; !.=0; return
assign?:   ?=$.r.c; n=neighbors(); if ?==emp then do;if n==3 then ?=life!; end; else if n<2 | n>3 then ?=emp; @.r.c=?; return
assign@:   @.=emp;      do r=1  for rows;  do c=1  for cols;  call assign?;  end;  end;            call assign$;       return
halt:      say;         say "REXX program  (Conway's Life)  halted.";    say;      exit 0
neighbors: return $(r-1,c-1)  +  $(r-1,c)  +  $(r-1,c+1)  +  $(r,c-1)  +  $(r,c+1)  +  $(r+1,c-1)  +  $(r+1,c)  +  $(r+1,c+1)
p:         return word(arg(1), 1)
pickChar:  _=p(arg(1)); arg u .; if u=='BLANK' then _=" "; L=length(_); if L==3 then _=d2c(_); if L==2 then _=x2c(_);  return _
showRows:  _=; do r=rows by -1 for rows; z=; do c=1 for cols; z=z||$.r.c; end; z=strip(z,'T',emp); say z; _=_||z; end; return