class GameOfLife::Game

Public Class Methods

new() { |self| ... } click to toggle source
# File lib/gameoflife/game.rb, line 11
def initialize 
  if block_given?
    yield self 
  end

  @core = load_map generate_random_array
end

Public Instance Methods

display_world() click to toggle source
# File lib/gameoflife/game.rb, line 38
def display_world
  (-MAX_HEIGH..MAX_HEIGH).each do |i|
    (-MAX_WIDTH..MAX_WIDTH).each do |j|
      if @core.world[[i,j]].nil?
        print "   "
      else
        print " 1 "
      end
    end
    puts ""
  end
end
generate_random_array() click to toggle source
# File lib/gameoflife/game.rb, line 19
def generate_random_array
  rd = Random.new(Random.new_seed)
  width = rd.rand(RAND_MIN_WIDTH..RAND_MAX_WIDTH)
  heigth = rd.rand(RAND_MIN_HEITG..RAND_MAX_HEITG)
  array = []
  (0..heigth).each do 
    row = []
    (0..width).each do
      row.push rd.rand(0..1) 
    end
    array.push row
  end
  array
end
load_map(array) click to toggle source
# File lib/gameoflife/game.rb, line 34
def load_map array
  @core = Core.new(array)
end
run() click to toggle source
# File lib/gameoflife/game.rb, line 56
def run
  while true
    single_step
    sleep 1
  end
end
single_step() click to toggle source
# File lib/gameoflife/game.rb, line 51
def single_step
  @core.next_world
  display_world
end