class LifeGrid

Public Class Methods

new(window) click to toggle source

Manages the underlying array of cells and their life process

# File lib/NanoLife.rb, line 71
def initialize(window)
  @num_cols = WIN_WIDTH / CELL_SIZE
  @num_rows = WIN_HEIGHT / CELL_SIZE
  @grid = create_grid
  @window = window
end

Public Instance Methods

check_neighbors(x, y) click to toggle source

Given an x and y, calculate how many neighbors cell has

# File lib/NanoLife.rb, line 111
def check_neighbors(x, y)
  num_neighbors = 0

  # Previous row
  if y > 0 and x > 0
    num_neighbors += @grid[x-1][y-1]
  end
  if y > 0
    num_neighbors += @grid[x][y-1]
  end
  if x < @num_cols - 1 and y > 0
    num_neighbors += @grid[x+1][y-1]
  end

  # Current row
  if x > 0
    num_neighbors += @grid[x-1][y]
  end
  if x < @num_cols - 1
    num_neighbors += @grid[x+1][y]
  end

  # Lower row
  if x > 0 and y < @num_rows - 1
    num_neighbors += @grid[x-1][y+1]
  end
  if y < @num_rows - 1
    num_neighbors += @grid[x][y+1]
  end
  if x < @num_cols - 1 and y < @num_rows - 1
    num_neighbors += @grid[x+1][y+1]
  end

  return num_neighbors
end
clear() click to toggle source

Utility function to randomize grid

# File lib/NanoLife.rb, line 184
def clear
  (0...@num_cols).each do |x|
    (0...@num_rows).each do |y|
      @grid[x][y] = 0
    end
  end
end
create_grid() click to toggle source

Create an empty grid array

# File lib/NanoLife.rb, line 79
def create_grid
  return Array.new(@num_cols) {Array.new(@num_rows, 0)}
end
draw() click to toggle source

Draw the cell grid to the window

# File lib/NanoLife.rb, line 148
def draw
  # draw grid
  (0...@num_cols).each do |x|
    (0...@num_rows).each do |y|
      if @grid[x][y] == 0
        color = EMPTY_COLOR
      else
        color = CELL_COLOR
      end
      realx = x * CELL_SIZE
      realy = y * CELL_SIZE
      @window.draw_quad(
        realx, realy, color,
        realx + CELL_SIZE, realy, color,
        realx, realy + CELL_SIZE, color,
        realx + CELL_SIZE, realy + CELL_SIZE, color,
      )
    end
  end
end
invert_cell(x, y) click to toggle source

Flip the state of a cell. Useful for manual click manipulation

# File lib/NanoLife.rb, line 170
def invert_cell(x, y)
  @grid[x][y] = (@grid[x][y] == 0 ? 1 : 0)
end
randomize() click to toggle source

Utility function to randomize grid

# File lib/NanoLife.rb, line 175
def randomize
  (0...@num_cols).each do |x|
    (0...@num_rows).each do |y|
      @grid[x][y] = rand(2)
    end
  end
end
update() click to toggle source

Ipdate state of all cells based on Conway's Game of Life rules

# File lib/NanoLife.rb, line 84
def update
  # For all cells check neighbors and kill or birth
  tmp_grid = create_grid
  (0...@num_cols).each do |x|
    (0...@num_rows).each do |y|
      num_neighbors = check_neighbors(x, y)

      if num_neighbors < 2 and @grid[x][y] == 1
        tmp_grid[x][y] = 0
      end
      if num_neighbors == 2 or num_neighbors == 3
        if @grid[x][y] == 1
          tmp_grid[x][y] = 1
        end
      end
      if num_neighbors == 3 and @grid[x][y] == 0
        tmp_grid[x][y] = 1
      end
      if num_neighbors > 3 and @grid[x][y] == 1
        tmp_grid[x][y] = 0
      end
    end
  end
  @grid = tmp_grid
end