Class: GameOfLife::Game
- Inherits:
-
Object
- Object
- GameOfLife::Game
- Defined in:
- lib/gameoflife/game.rb
Instance Method Summary collapse
- #display_world ⇒ Object
- #generate_random_array ⇒ Object
-
#initialize ⇒ Game
constructor
A new instance of Game.
- #load_map(array) ⇒ Object
- #run ⇒ Object
- #single_step ⇒ Object
Constructor Details
#initialize ⇒ Game
Returns a new instance of Game.
11 12 13 14 15 16 17 |
# File 'lib/gameoflife/game.rb', line 11 def initialize if block_given? yield self end @core = load_map generate_random_array end |
Instance Method Details
#display_world ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 |
# 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 ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
# 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) ⇒ Object
34 35 36 |
# File 'lib/gameoflife/game.rb', line 34 def load_map array @core = Core.new(array) end |
#run ⇒ Object
56 57 58 59 60 61 |
# File 'lib/gameoflife/game.rb', line 56 def run while true single_step sleep 1 end end |
#single_step ⇒ Object
51 52 53 54 |
# File 'lib/gameoflife/game.rb', line 51 def single_step @core.next_world display_world end |