Class: Conwy::World::CellGrid

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

Defined Under Namespace

Classes: Coordinate

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.from_s(string) ⇒ Object



20
21
22
23
24
25
26
# File 'lib/conwy/world.rb', line 20

def self.from_s(string)
  new(string.split("\n").map do |row|
    row.each_char.map do |char|
      Cell.new(alive: char == '1')
    end
  end)
end

Instance Method Details

#nextObject



36
37
38
39
40
41
42
# File 'lib/conwy/world.rb', line 36

def next
  self.class.new(map.with_index do |row, x|
    row.map.with_index do |cell, y|
      cell.next(alive_neighbors: alive_neighbors(Coordinate.new(x, y)))
    end
  end)
end

#to_sObject



28
29
30
31
32
33
34
# File 'lib/conwy/world.rb', line 28

def to_s
  map do |row|
    row.map do |cell|
      cell.alive? ? 1 : 0
    end.join
  end.join("\n")
end