Class: GameOfLife::Board::Cell

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(initial_state = :dead) ⇒ Cell

Creates a GameOfLife::Board::Cell. A cell starts out as dead unless explicitly set alive.

Raises:

  • (ArgumentError)


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_generationObject

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

#stateObject

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

Returns:

  • (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

Returns:

  • (Boolean)


26
27
28
# File 'lib/game_of_life/cell.rb', line 26

def dead?
  ! alive?
end

#to_sObject



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