Class: Grid
- Inherits:
-
Array
- Object
- Array
- Grid
- Defined in:
- lib/life/grid.rb
Instance Method Summary collapse
- #deep_copy ⇒ Object
-
#initialize(x, y, seed = []) ⇒ Grid
constructor
A new instance of Grid.
- #live_neighbor_count_for(x, y) ⇒ Object
- #to_s(live, dead) ⇒ Object
Constructor Details
Instance Method Details
#deep_copy ⇒ Object
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 |