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.


identification division.
program-id. game-of-life-program.
data division.
working-storage section.
01  grid.
    05 cell-table.
        10 row occurs 5 times.
            15 cell pic x value space occurs 5 times.
    05 next-gen-cell-table.
        10 next-gen-row occurs 5 times.
            15 next-gen-cell pic x occurs 5 times.
01  counters.
    05 generation pic 9.
    05 current-row pic 9.
    05 current-cell pic 9.
    05 living-neighbours pic 9.
    05 neighbour-row pic 9.
    05 neighbour-cell pic 9.
    05 check-row pic s9.
    05 check-cell pic s9.
procedure division.
control-paragraph.
    perform blinker-paragraph varying current-cell from 2 by 1
    until current-cell is greater than 4.
    perform show-grid-paragraph through life-paragraph
    varying generation from 0 by 1
    until generation is greater than 2.
    stop run.
blinker-paragraph.
    move '#' to cell(3,current-cell).
show-grid-paragraph.
    display 'GENERATION ' generation ':'.
    display '   +---+'.
    perform show-row-paragraph varying current-row from 2 by 1
    until current-row is greater than 4.
    display '   +---+'.
    display ''.
life-paragraph.
    perform update-row-paragraph varying current-row from 2 by 1
    until current-row is greater than 4.
    move next-gen-cell-table to cell-table.
show-row-paragraph.
    display '   |' with no advancing.
    perform show-cell-paragraph varying current-cell from 2 by 1
    until current-cell is greater than 4.
    display '|'.
show-cell-paragraph.
    display cell(current-row,current-cell) with no advancing.
update-row-paragraph.
    perform update-cell-paragraph varying current-cell from 2 by 1
    until current-cell is greater than 4.
update-cell-paragraph.
    move 0 to living-neighbours.
    perform check-row-paragraph varying check-row from -1 by 1
    until check-row is greater than 1.
    evaluate living-neighbours,
        when 2 move cell(current-row,current-cell) to next-gen-cell(current-row,current-cell),
        when 3 move '#' to next-gen-cell(current-row,current-cell),
        when other move space to next-gen-cell(current-row,current-cell),
    end-evaluate.
check-row-paragraph.
    add check-row to current-row giving neighbour-row.
    perform check-cell-paragraph varying check-cell from -1 by 1
    until check-cell is greater than 1.
check-cell-paragraph.
    add check-cell to current-cell giving neighbour-cell.
    if cell(neighbour-row,neighbour-cell) is equal to '#',
    and check-cell is not equal to zero or check-row is not equal to zero,
    then add 1 to living-neighbours.