class Rsb::Gol::Game

Constants

CLEAR_SCREEN_CHAR

Attributes

seed[RW]
stop_running[R]
universe[R]

Public Class Methods

new(options = {}) click to toggle source
# File lib/rsb/gol/game.rb, line 9
def initialize(options = {})
  random_seed   = options[:random_seed] == false ? false : true
  rows          = options[:rows] || 40
  cols          = options[:cols] || 80
  grid          = Rsb::Gol::Grid.new(rows, cols, random_seed)
  @universe     = options[:universe] || Rsb::Gol::Universe.new(grid: grid)
  @stop_running = false

  trap('INT') do
    puts "\ngame of life will now stop running\n"
    @stop_running = true
  end
end

Public Instance Methods

clear_screen() click to toggle source
# File lib/rsb/gol/game.rb, line 23
def clear_screen
  puts CLEAR_SCREEN_CHAR
end
start(iterations = 1000, alive='0', dead=' ', step=3) click to toggle source
# File lib/rsb/gol/game.rb, line 32
def start(iterations = 1000, alive='0', dead=' ', step=3)
  clear_screen
  iterations.times do
    exit if stop_running
    data   = universe.tick!
    abort('empty universe') if data[:empty_universe]
    status = status_line(data)
    output = universe.visualize(alive, dead)

    clear_screen
    puts "\n\n#{output}\n\n #{status}\n"
    sleep(1.0/ step)
  end
end
status_line(data) click to toggle source
# File lib/rsb/gol/game.rb, line 27
def status_line(data)
  "@@@ generation clock: #{data[:clock]} " +
    "deaths: #{data[:deaths]} births: #{data[:births]}"
end