Class: BrowserConwayGameOfLife::Cell
- Inherits:
-
Object
- Object
- BrowserConwayGameOfLife::Cell
- Defined in:
- lib/browser_conway_game_of_life/cell.rb
Constant Summary collapse
- NEIGHBOURS =
[ [-1, -1], [-1, 0], [-1, 1], [0, -1], [0, 1], [1, -1], [1, 0], [1, 1] ]
Instance Attribute Summary collapse
-
#value ⇒ Object
Returns the value of attribute value.
Class Method Summary collapse
Instance Method Summary collapse
- #change! ⇒ Object
- #dead? ⇒ Boolean
-
#initialize(value = rand(0..1)) ⇒ Cell
constructor
A new instance of Cell.
- #live? ⇒ Boolean
- #to_s ⇒ Object
Constructor Details
#initialize(value = rand(0..1)) ⇒ Cell
Returns a new instance of Cell.
11 12 13 |
# File 'lib/browser_conway_game_of_life/cell.rb', line 11 def initialize(value = rand(0..1)) (0..1).include?(value) ? @value = value : @value = rand(0..1) end |
Instance Attribute Details
#value ⇒ Object
Returns the value of attribute value.
9 10 11 |
# File 'lib/browser_conway_game_of_life/cell.rb', line 9 def value @value end |
Class Method Details
.live_neighbours(universe, i, j) ⇒ Object
31 32 33 34 35 |
# File 'lib/browser_conway_game_of_life/cell.rb', line 31 def self.live_neighbours(universe, i, j) NEIGHBOURS.inject(0) do |sum, pos| sum + universe[(i + pos[0]) % universe.size][(j + pos[1]) % universe[0].size].value end end |
Instance Method Details
#change! ⇒ Object
23 24 25 |
# File 'lib/browser_conway_game_of_life/cell.rb', line 23 def change! self.value = self.value.send(:+, -1).abs end |
#dead? ⇒ Boolean
19 20 21 |
# File 'lib/browser_conway_game_of_life/cell.rb', line 19 def dead? !live? end |
#live? ⇒ Boolean
15 16 17 |
# File 'lib/browser_conway_game_of_life/cell.rb', line 15 def live? @value == 1 end |
#to_s ⇒ Object
27 28 29 |
# File 'lib/browser_conway_game_of_life/cell.rb', line 27 def to_s live? ? 'O' : ' ' end |