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.


(defun print-row (row)
   (if (endp row)
       nil
       (prog2$ (if (first row)
                   (cw "[]")
                   (cw "  "))
               (print-row (rest row)))))
 
(defun print-grid-r (grid)
   (if (endp grid)
       nil
       (progn$ (cw "|")
               (print-row (first grid))
               (cw "|~%")
               (print-grid-r (rest grid)))))
 
(defun print-line (l)
   (if (zp l)
       nil
       (prog2$ (cw "-")
               (print-line (1- l)))))
 
(defun print-grid (grid)
   (progn$ (cw "+")
           (print-line (* 2 (len (first grid))))
           (cw "+~%")
           (print-grid-r grid)
           (cw "+")
           (print-line (* 2 (len (first grid))))
           (cw "+~%")))
 
(defun neighbors-row-r (row)
   (if (endp (rest (rest row)))
       (list (if (first row) 1 0))
       (cons (+ (if (first row) 1 0)
                (if (third row) 1 0))
             (neighbors-row-r (rest row)))))
 
(defun neighbors-row (row)
   (cons (if (second row) 1 0)
         (neighbors-row-r row)))
 
(defun zip+ (xs ys)
   (if (or (endp xs) (endp ys))
       (append xs ys)
       (cons (+ (first xs) (first ys))
             (zip+ (rest xs) (rest ys)))))
 
(defun counts-row (row)
   (if (endp row)
       nil
       (cons (if (first row) 1 0)
             (counts-row (rest row)))))
 
(defun neighbors-r (grid prev-counts curr-counts next-counts
                         prev-neighbors curr-neighbors
                         next-neighbors)
   (if (endp (rest grid))
       (list (zip+ (zip+ prev-counts
                         prev-neighbors)
                   (neighbors-row (first grid))))
       (cons (zip+ (zip+ (zip+ prev-counts next-counts)
                         (zip+ prev-neighbors next-neighbors))
                   curr-neighbors)
             (neighbors-r (rest grid)
                          curr-counts
                          next-counts
                          (counts-row (third grid))
                          curr-neighbors
                          next-neighbors
                          (neighbors-row (third grid))))))
 
(defun neighbors (grid)
   (neighbors-r grid
                nil
                (counts-row (first grid))
                (counts-row (second grid))
                nil
                (neighbors-row (first grid))
                (neighbors-row (second grid))))
 
(defun life-rules-row (life neighbors)
   (if (or (endp life) (endp neighbors))
       nil
       (cons (or (and (first life)
                      (or (= (first neighbors) 2)
                          (= (first neighbors) 3)))
                 (and (not (first life))
                      (= (first neighbors) 3)))
             (life-rules-row (rest life) (rest neighbors)))))
 
(defun life-rules-r (grid neighbors)
   (if (or (endp grid) (endp neighbors))
       nil
       (cons (life-rules-row (first grid) (first neighbors))
             (life-rules-r (rest grid) (rest neighbors)))))
 
(defun conway-step (grid)
   (life-rules-r grid (neighbors grid)))
 
(defun conway (grid steps)
   (if (zp steps)
       nil
       (progn$ (print-grid grid)
               (conway (conway-step grid) (1- steps)))))