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.


set starting_condition to ((0, 0), (1, 0), (2, 0), (-1, -1), (0, -1), (1, -1))
 
RunGameOfLife starting_condition
 
to printColony colonies
    set xCoords to the first item of each item of colonies
    set yCoords to the second item of each item of colonies
    set min_x to the min of xCoords
    set max_x to the max of xCoords
    set min_y to the min of yCoords
    set max_y to the max of yCoords
    repeat for y in min_y..max_y
        set row to ()
        repeat for x in min_x..max_x
            if (x, y) is in colonies
                push "#" into row
            else
                push "-" into row
            end if
        end repeat
        join row using ""
        put row
    end repeat
end printColony
 
to neighboursOf coordinate
    return ( \
            coordinate + (-1, 1), \
            coordinate + (0, 1), \
            coordinate + (1, 1), \
            coordinate + (-1, 0), \
            coordinate + (1, 0), \
            coordinate + (-1, -1), \
            coordinate + (0, -1), \
            coordinate + (1, -1), \
            )
end neighboursOf
 
to WillNextGenHaveCell colony, coordinate
    set neighbour_count to the number of items in (each item of neighboursOf(coordinate) where each is in colony)
    if coordinate is in colony
        return neighbour_count is in (2, 3)
    else
        return neighbour_count equals 3
    end if
end WillNextGenHaveCell
 
to RunGameOfLife colony
    printColony colony
    set the listInsertionMode to "nested"
    repeat 10 times
        set new_colony to ()
 
        set xCoords to the first item of each item of colony
        set yCoords to the second item of each item of colony
 
        set min_x to (the min of xCoords) - 1
        set max_x to (the max of xCoords) + 1
        set min_y to (the min of yCoords) - 1
        set max_y to (the max of yCoords) + 1
 
        repeat for y in min_y..max_y
            repeat for x in min_x..max_x
                if WillNextGenHaveCell(colony, (x, y))
                    insert (x, y) into new_colony
                end if
            end repeat
        end repeat
        set colony to new_colony
        printColony colony
        wait 1 second
    end repeat
end RunGameOfLife