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.


 
start = now[]
// Generate a random 10x10 grid with "1" being on and "0" being off
instructions = ["1000100110","0001100010","1000111101","1001111110","0000110011","1111000001","0100001110","1011101001","1001011000","1101110111"]
 
// Create dictionary of starting positions.
rowCounter = 0
display = new dict
for instructionStr = instructions
{
   rowCounter = rowCounter + 1
   columnCounter = 0
   instructionArr = charList[instructionStr]
   for instruction = instructionArr
   {
      columnCounter = columnCounter + 1
      arr = [rowCounter,columnCounter]
      if instruction == "1"
         display@arr = 1
      else
         display@arr = 0
   }
}
 
// Create toggle dictionary to track changes. It starts off with everything off.
toggle = new dict
multifor[x,y] = [new range[1,10],new range[1,10]]
{
   arr = [x,y]
   toggle@arr = 0
}
 
// Animate the game of life
a = new Animation[3/s]
win = undef
 
// Loop through 10 changes to the grid. The starting points will tick down to two stable unchanging shapes in 10 steps.
for i = 1 to 12 // 12 steps so animation will pause on final state.
{
   // Graphics item for this frame of the animation.
   g = new graphics
   g.backgroundColor[1,1,1]
   // Add in a transparent shape to prevent the image from jiggle to automatic scaling.
   g.color[0,0,0,0] // Transparent black
   g.fillRectSides[-1, -1, 12, 12] // Set minimum size
   g.clipRectSides[-1, -1, 12, 12] // Set maximum size
   g.color[0,0,0] // Color back to default black
   multifor[x1,y1] = [new range[1,10],new range[1,10]]
   {
      tval = [x1,y1]
      // This is programmed with a hard edge. Points beyond the border aren't considered.
      xmax = min[x1+1,10]
      xmin = max[x1-1,1]
      ymax = min[y1+1,10]
      ymin = max[y1-1,1]
      // Range will be 8 surrounding cells or cells up to border.
      pointx = new range[xmin,xmax]
      pointy = new range[ymin,ymax]
      pointsum = 0
      status = 0
      // Process each surrounded point
      multifor[x2,y2] = [pointx,pointy]
      {
         // Assign the array to a variable so it can be used as a dictionary reference.
         point = [x2,y2]
         if x2 == x1 && y2 == y1
         {
            status = display@point
         } else // Calculate the total of surrounding points
         {
            pointsum = pointsum + display@point
         }
      }
      // Animate if the point is on.
      if status == 1
      {
         g.color[0,0,0]
         g.fillEllipseCenter[x1,y1,1,1]
      }
      toggle@tval = status // This will be overwritten if needed by neighbor check conditions below.
	  // Check if off point has 3 on point neighbors
      if status == 0 && pointsum == 3
      {
         toggle@tval = 1
      }
	  // Check if on point has between 2 and 3 on point neighbors
      if status == 1 && (pointsum < 2 || pointsum > 3)
      {
         toggle@tval = 0
      }
   }
   // Add the current frame to the animation
   a.add[g]
   // Replace the current display with the toggle values.
   for toggleKeys = keys[toggle]
   {
      val = toggle@toggleKeys
      display@toggleKeys = val
   }
}
 
// Write the animation file
a.write["FrinkAnimationGoL.gif",400,400]
 
end = now[]
println["Program run time: " + ((end - start)*1.0 -> "seconds")]
 