Class: BrowserConwayGameOfLife::Universe

Inherits:
Object
  • Object
show all
Defined in:
lib/browser_conway_game_of_life/universe.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(rows, cols) ⇒ Universe

Returns a new instance of Universe.



5
6
7
8
9
# File 'lib/browser_conway_game_of_life/universe.rb', line 5

def initialize(rows, cols)
  srand 42
  @field = Array.new(rows) { Array.new(cols) { Cell.new } }
  @temp_field = @field.clone
end

Instance Attribute Details

#colsObject (readonly)

Returns the value of attribute cols.



3
4
5
# File 'lib/browser_conway_game_of_life/universe.rb', line 3

def cols
  @cols
end

#rowsObject (readonly)

Returns the value of attribute rows.



3
4
5
# File 'lib/browser_conway_game_of_life/universe.rb', line 3

def rows
  @rows
end

Instance Method Details

#nextObject



11
12
13
14
# File 'lib/browser_conway_game_of_life/universe.rb', line 11

def next
  step
  @temp_field = @field.clone
end

#to_sObject



16
17
18
19
20
21
22
23
24
25
# File 'lib/browser_conway_game_of_life/universe.rb', line 16

def to_s
  string = ''
  @field.each do |vector|
    vector.each do |el|
      string << el.to_s
    end
    string << "\n"
  end
  string
end