SciQuest_csp

GameOfLife

Mar 26th, 2012
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Lua 4.76 KB | None | 0 0
  1. -- Conway's Game of Life
  2.  
  3. -- setup() is called once at the start.
  4. function setup()
  5.      -- this changes the size of the simulation. larger numbers will lag the ipad. keep it less then 100.
  6.      size = 50
  7.      -- this creates an empty 'list'. lists are how Lua handles collections of things.
  8.      -- they are like arrays, vectors, sets, or tuples.
  9.      -- lists have a collection of unique "keys" (indexes) which start at 1 and increment.
  10.      -- each key corresponds to a (non-unique) value.
  11.      cells = {}
  12.      -- this nested loop constructs a list of lists. this creates a 2 dimensional array.
  13.      -- because the inner and outer loops both run size-many times, the array will be square.
  14.      -- this notation means i will start with a value of 1 and increment to a value of "size".
  15.      for i = 1,size do
  16.          -- we are creating a NEW, empty list here each time the outer loops runs.
  17.          local temp = {}
  18.          -- the inner loop populates the empty "temp" list with true/false (boolean) values
  19.          for j = 1,size do
  20.              -- this corresponds to a 20% chance for the value to be true.
  21.              -- true will correspond to alive, false to dead.
  22.              if math.random(5) == 1 then
  23.                  table.insert(temp, true)
  24.              else
  25.                  table.insert(temp, false)
  26.              end
  27.          end
  28.          -- the temp list is added to the cells list. this is still in the outer loop,
  29.          -- so it happens multiple times.
  30.          table.insert(cells, temp)
  31.      end
  32.  end
  33. -- end of the setup() function
  34.  
  35. -- draw() is called once every frame (up to 30 hertz)
  36.  function draw()    
  37.      -- clears the screen
  38.      background(40, 40, 50)
  39.      -- feel free to increase this if the cells look 'blocky'
  40.      strokeWidth(5)
  41.  
  42.      -- partitions the width and height of the screen into size-many chunks, each dx and dy size    
  43.      dx = WIDTH / size
  44.      dy = HEIGHT / size
  45.      
  46.      -- draws the current contents of the cells list (which is a list of lists of booleans)
  47.      -- this notation will loop once for everything in the cells list (for each list it contains)
  48.      -- that list (value) will be called x, and its index (key) will be called i.
  49.      for i,x in pairs(cells) do
  50.          -- does the same for each list of booleans, "x".
  51.          for j,y in pairs(x) do
  52.              -- cells stores boolean values and can thus be used as a conditional
  53.              if cells[i][j] then
  54.                  -- four corners of the rectangle to draw
  55.                  rect(dx*(i-1), dy*(j-1), dx, dy)
  56.              end
  57.          end
  58.      end
  59.  
  60.      -- this creates a NEW 2D list (each time it is called) called "next".
  61.      -- all values will be false.      
  62.      next = {}
  63.      for i=1,size do
  64.          local temp = {}
  65.          for j=1,size do
  66.              table.insert(temp, false)
  67.          end
  68.          table.insert(next, temp)
  69.      end
  70.      
  71.      local adj = 0
  72.      for i=1,size do
  73.          for j=1,size do
  74.              -- this is a reduced version of the game of life rules.
  75.              -- we dont need to include rules for cells dying, since "next" is all false values.
  76.              -- numAdj(i,j) is short for numberOfLivingCellsAdjacentTo(i,j)
  77.              -- it is defined after the draw() function
  78.              adj = numAdj(i, j)
  79.              if adj == 3 then
  80.                  next[i][j] = true
  81.              end
  82.              if cells[i][j] and adj == 2 then
  83.                  next[i][j] = true
  84.              end        
  85.          end
  86.      end
  87.      
  88.      -- replace cells with our new array.
  89.      cells = next
  90.  end
  91.  
  92. -- Counts the number of adjacent cells which are alive.
  93. -- x and y are the inputs. they represent the position in the grid of cells.
  94.  function numAdj(x, y)
  95.      -- we start counting at 0
  96.      local count = 0
  97.      -- count from (one to the left), to (one to the right)
  98.      for i = x-1,x+1 do
  99.          -- count from (one below), to (one above)
  100.          for j = y-1,y+1 do
  101.              -- this is a compound if statement. COULD be broken up into several 'if' statements
  102.              -- but this implementation is more efficient and avoids being tediously verbose
  103.              -- the first four conditionals (0<i through j<=size) ensure that you are counting
  104.              -- only cells which actually exist.
  105.              -- the last (i~=x or j~=y) ensures you do not count yourself
  106.              if 0<i and i<=size and 0<j and j<=size and (i~=x or j~=y) then
  107.                  -- cells is an array of true/false boolean values, therefor you can use
  108.                  -- its value as a conditional
  109.                  if cells[i][j] then
  110.                      count = count + 1
  111.                  end
  112.              end
  113.          end
  114.      end
  115.      -- this is what this function outputs
  116.      return count
  117.  end
Advertisement
Add Comment
Please, Sign In to add comment