Class: GameOfLife::Board::Cell
- Inherits:
-
Object
- Object
- GameOfLife::Board::Cell
- Defined in:
- lib/game_of_life/cell.rb
Overview
An indivisual cell in the Game GameOfLife::Board
Constant Summary collapse
- STATES =
[:live, :dead]
Instance Attribute Summary collapse
-
#should_live_in_next_generation ⇒ Object
Returns the value of attribute should_live_in_next_generation.
-
#state ⇒ Object
Returns the value of attribute state.
Instance Method Summary collapse
- #alive? ⇒ Boolean
- #change_state_if_needed! ⇒ Object
- #dead? ⇒ Boolean
-
#initialize(initial_state = :dead) ⇒ Cell
constructor
Creates a Cell.
- #to_s ⇒ Object
Constructor Details
#initialize(initial_state = :dead) ⇒ Cell
Creates a GameOfLife::Board::Cell. A cell starts out as dead unless explicitly set alive.
9 10 11 12 |
# File 'lib/game_of_life/cell.rb', line 9 def initialize(initial_state=:dead) raise ArgumentError, "Unknown state #{initial_state}" unless STATES.include? initial_state @state = initial_state end |
Instance Attribute Details
#should_live_in_next_generation ⇒ Object
Returns the value of attribute should_live_in_next_generation.
6 7 8 |
# File 'lib/game_of_life/cell.rb', line 6 def should_live_in_next_generation @should_live_in_next_generation end |
#state ⇒ Object
Returns the value of attribute state.
6 7 8 |
# File 'lib/game_of_life/cell.rb', line 6 def state @state end |
Instance Method Details
#alive? ⇒ Boolean
22 23 24 |
# File 'lib/game_of_life/cell.rb', line 22 def alive? state == :live end |
#change_state_if_needed! ⇒ Object
14 15 16 17 18 19 20 |
# File 'lib/game_of_life/cell.rb', line 14 def change_state_if_needed! if self.should_live_in_next_generation self.state = :live else self.state = :dead end end |
#dead? ⇒ Boolean
26 27 28 |
# File 'lib/game_of_life/cell.rb', line 26 def dead? ! alive? end |
#to_s ⇒ Object
30 31 32 33 34 |
# File 'lib/game_of_life/cell.rb', line 30 def to_s fields = %w{state should_live_in_next_generation} important_details = fields.map {|attr| "#{attr}:#{self.send(attr)}"} "#{super} <#{important_details.join(' ')}>" end |