ConwaysLife.frink

Download or view ConwaysLife.frink in plain text format

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
   for instruction = charList[instructionStr]
   {
      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.
// the keys are [x,y] coordinates and the values are 0 and 1
toggle = new dict
multifor point = [new range[1,10],new range[1,10]]
   toggle@point = 0
 
// Animate the game of life
a = new Animation[3/s]
 
// 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 tval = [new range[1,10],new range[1,10]]
   {
      [x1,y1] = tval
      // 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.
      pointsum = 0
      status = 0
      // Process each surrounded point
      multifor point = [xmin to xmax, ymin to ymax]
      {
         [x2,y2] = point
         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.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 [key, val] = toggle
      display@key = val
}
 
// Write the animation file
a.write["ConwaysLife.gif",400,400]
 
end = now[]
println["Program run time: " + ((end - start)*1.0 -> "seconds")]


Download or view ConwaysLife.frink in plain text format


This is a program written in the programming language Frink.
For more information, view the Frink Documentation or see More Sample Frink Programs.

Alan Eliasen was born 19945 days, 5 hours, 33 minutes ago.