Class: Grid

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

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of Grid.



3
4
5
6
7
8
# File 'lib/life/grid.rb', line 3

def initialize(x, y, seed=[])
  @width = x
  @height = y
  super(x) { Array.new(y) { Cell.new(false) } }
  seed(seed)
end

Instance Method Details

#deep_copyObject



10
11
12
# File 'lib/life/grid.rb', line 10

def deep_copy
  Marshal.load(Marshal.dump(self))
end

#live_neighbor_count_for(x, y) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/life/grid.rb', line 14

def live_neighbor_count_for(x, y)
  count = 0
  x_range(x).each do |curr_x|
    y_range(y).each do |curr_y|
      unless (curr_x == x && curr_y == y)
        count += 1 if self[curr_x][curr_y].live?
      end
    end
  end
  count
end

#to_s(live, dead) ⇒ Object



26
27
28
29
30
31
32
# File 'lib/life/grid.rb', line 26

def to_s(live, dead)
  array_of_rows = self.transpose
  rows = array_of_rows.map do |row|
    row.map { |cell| cell.live? ? live : dead }.join(" ")
  end
  rows.join("\n")
end