Class: GameOfLife::Game

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

Instance Method Summary collapse

Constructor Details

#initializeGame

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_worldObject



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_arrayObject



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

#runObject



56
57
58
59
60
61
# File 'lib/gameoflife/game.rb', line 56

def run
  while true
    single_step
    sleep 1
  end
end

#single_stepObject



51
52
53
54
# File 'lib/gameoflife/game.rb', line 51

def single_step
  @core.next_world
  display_world
end