Class: World

Inherits:
Object
  • Object
show all
Defined in:
lib/life/world.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(x, y, seed = []) ⇒ World

Returns a new instance of World.



5
6
7
8
# File 'lib/life/world.rb', line 5

def initialize(x, y, seed = [])
  @current = Grid.new(x, y, seed)
  @next = @current.deep_copy
end

Instance Attribute Details

#currentObject (readonly)

Returns the value of attribute current.



3
4
5
# File 'lib/life/world.rb', line 3

def current
  @current
end

Instance Method Details

#tickObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/life/world.rb', line 10

def tick
  current.each do |row|
    x = current.index(row)
    row.each do |cell|
      y = row.index(cell)
      if cell.live?
        case @current.live_neighbor_count_for(x,y)
        when 0..1 then @next[x][y].kill
        when 2..3 then @next[x][y].live
        when 4..8 then @next[x][y].kill
        end
      elsif cell.dead? && @current.live_neighbor_count_for(x,y) == 3
        @next[x][y].live
      end
    end
  end
  @current = @next.deep_copy
end

#to_s(live, dead) ⇒ Object



29
30
31
# File 'lib/life/world.rb', line 29

def to_s(live, dead)
  @current.to_s(live, dead)
end