Class: Cell

Inherits:
Object
  • Object
show all
Defined in:
lib/cell.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeCell

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_stateObject

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

Parameters:

  • value

    the value to set the attribute queued_state to.



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_neighborsObject

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_neighborsObject

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_stateObject

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