class GameOfLife::Earth

Attributes

cells[RW]

GameOfLife::Cell is a grid element where GameOfLife::Earth is a grid This class doesn't hold any logic related to cell mutation rules

height[R]
width[R]

Public Class Methods

new(width, height, seed_probability = nil) click to toggle source
# File lib/game_of_life/earth.rb, line 10
def initialize(width, height, seed_probability = nil)
  @generation = 1
  seed_probability = rand if seed_probability.nil?

  @width  = width
  @height = height

  @cells = Array.new(width) do
    Array.new(height) do
      Cell.new(seed_probability)
    end
  end
end

Public Instance Methods

[](x) click to toggle source

@return row of @cells

# File lib/game_of_life/earth.rb, line 25
def [](x)
  @cells[x]
end
alive_neighbours(x, y) click to toggle source

@param [row] x @param [col] y

# File lib/game_of_life/earth.rb, line 43
def alive_neighbours(x, y)
  [
      [-1, -1], [0, -1], [1, -1],  # over
      [-1,  0],          [1,  0],  # sides
      [-1,  1], [0,  1], [1,  1]   # under
  ].inject(0) do |sum, pos|
    _x = x + pos[0]
    _y = y + pos[1]

    # if we are in the Earth sizes
    if _x >= 0 && _y >= 0 && _x < @width && _y < @height
      sum += self[_x][_y].bool_to_num
    end

    sum
  end
end
log() click to toggle source
# File lib/game_of_life/earth.rb, line 61
def log
  grid = "Generation #{@generation}"
  @cells.transpose.each do |row|
    grid += "\n"
    grid += row.map(&:bool_to_num).join(' | ')
  end
  grid += "\n"
  grid
end
next!() click to toggle source
# File lib/game_of_life/earth.rb, line 29
def next!
  @generation += 1
  @cells.each_with_index do |col, x|
    col.each_with_index do |cell, y|
      cell.neighbours = alive_neighbours(x, y)
    end
  end

  @cells.each { |row| row.each(&:next!) }
  nil
end