Class: Rsb::Gol::Game
- Inherits:
-
Object
- Object
- Rsb::Gol::Game
- Defined in:
- lib/rsb/gol/game.rb
Constant Summary collapse
- CLEAR_SCREEN_CHAR =
"\e[H\e[2J"
Instance Attribute Summary collapse
-
#seed ⇒ Object
Returns the value of attribute seed.
-
#stop_running ⇒ Object
readonly
Returns the value of attribute stop_running.
-
#universe ⇒ Object
readonly
Returns the value of attribute universe.
Instance Method Summary collapse
- #clear_screen ⇒ Object
-
#initialize(options = {}) ⇒ Game
constructor
A new instance of Game.
- #start(iterations = 1000, alive = '0', dead = ' ', step = 3) ⇒ Object
- #status_line(data) ⇒ Object
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( = {}) random_seed = [:random_seed] == false ? false : true rows = [:rows] || 40 cols = [:cols] || 80 grid = Rsb::Gol::Grid.new(rows, cols, random_seed) @universe = [: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
#seed ⇒ Object
Returns the value of attribute seed.
7 8 9 |
# File 'lib/rsb/gol/game.rb', line 7 def seed @seed end |
#stop_running ⇒ Object (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 |
#universe ⇒ Object (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_screen ⇒ Object
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 |