Class: Rsb::Gol::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/rsb/gol/game.rb

Constant Summary collapse

CLEAR_SCREEN_CHAR =
"\e[H\e[2J"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Game

Returns a new instance of Game.



9
10
11
12
13
14
15
16
17
18
19
20
21
# 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

Instance Attribute Details

#seedObject

Returns the value of attribute seed.



7
8
9
# File 'lib/rsb/gol/game.rb', line 7

def seed
  @seed
end

#stop_runningObject (readonly)

Returns the value of attribute stop_running.



6
7
8
# File 'lib/rsb/gol/game.rb', line 6

def stop_running
  @stop_running
end

#universeObject (readonly)

Returns the value of attribute universe.



6
7
8
# File 'lib/rsb/gol/game.rb', line 6

def universe
  @universe
end

Instance Method Details

#clear_screenObject



23
24
25
# File 'lib/rsb/gol/game.rb', line 23

def clear_screen
  puts CLEAR_SCREEN_CHAR
end

#start(iterations = 1000, alive = '0', dead = ' ', step = 3) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
# 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) ⇒ Object



27
28
29
30
# File 'lib/rsb/gol/game.rb', line 27

def status_line(data)
  "@@@ generation clock: #{data[:clock]} " +
    "deaths: #{data[:deaths]} births: #{data[:births]}"
end