Class: Cell
- Inherits:
-
Object
- Object
- Cell
- Defined in:
- lib/cell.rb
Instance Attribute Summary collapse
-
#cell_state ⇒ Object
Returns the value of attribute cell_state.
-
#queued_state ⇒ Object
writeonly
Sets the attribute queued_state.
Instance Method Summary collapse
-
#add_neighbor(cell) ⇒ Object
adds a neighbor to this cell.
-
#initialize ⇒ Cell
constructor
creates a new Cell with a state of “dead”.
-
#number_of_live_neighbors ⇒ Object
returns the number of live neighbors this cell has.
-
#number_of_neighbors ⇒ Object
return the number of neighbors that this cell has.
-
#transition_state ⇒ Object
Transitions this cell to its next state of evolution.
Constructor Details
#initialize ⇒ Cell
creates a new Cell with a state of “dead”
8 9 10 11 12 |
# File 'lib/cell.rb', line 8 def initialize @neighbors = [] @cell_state = :dead @queued_state = nil end |
Instance Attribute Details
#cell_state ⇒ Object
Returns the value of attribute cell_state.
2 3 4 |
# File 'lib/cell.rb', line 2 def cell_state @cell_state end |
#queued_state=(value) ⇒ Object (writeonly)
Sets the attribute queued_state
3 4 5 |
# File 'lib/cell.rb', line 3 def queued_state=(value) @queued_state = value end |
Instance Method Details
#add_neighbor(cell) ⇒ Object
adds a neighbor to this cell. this method takes care of hooking up the bi-directional relationship so the 2 cells do not need to be added to each other as neighbors, only 1 of them needs to be notified of the relationship
19 20 21 22 23 24 |
# File 'lib/cell.rb', line 19 def add_neighbor(cell) unless @neighbors.include? cell @neighbors << cell cell.neighbors << self end end |
#number_of_live_neighbors ⇒ Object
returns the number of live neighbors this cell has
32 33 34 35 |
# File 'lib/cell.rb', line 32 def number_of_live_neighbors live_neighbors = @neighbors.find_all {|cell| cell.cell_state == :live} live_neighbors.size end |
#number_of_neighbors ⇒ Object
return the number of neighbors that this cell has
27 28 29 |
# File 'lib/cell.rb', line 27 def number_of_neighbors @neighbors.size end |
#transition_state ⇒ Object
Transitions this cell to its next state of evolution
38 39 40 41 42 43 |
# File 'lib/cell.rb', line 38 def transition_state if @queued_state != nil @cell_state = @queued_state @queued_state = nil end end |