Class: Cell

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-life.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status = nil, seed_probability = 0.5) ⇒ Cell

Returns a new instance of Cell.



9
10
11
12
13
14
15
16
17
18
# File 'lib/ruby-life.rb', line 9

def initialize(status = nil, seed_probability = 0.5)
  case status
  when 'alive'
    @alive = true
  when 'dead'
    @alive = false
  else
    @alive = seed_probability > rand
  end
end

Instance Attribute Details

#aliveObject Also known as: alive?

Returns the value of attribute alive.



6
7
8
# File 'lib/ruby-life.rb', line 6

def alive
  @alive
end

#neighborsObject

Returns the value of attribute neighbors.



6
7
8
# File 'lib/ruby-life.rb', line 6

def neighbors
  @neighbors
end

Instance Method Details

#to_iObject



28
29
30
# File 'lib/ruby-life.rb', line 28

def to_i
  alive? ? 1 : 0
end

#to_sObject



32
33
34
# File 'lib/ruby-life.rb', line 32

def to_s
  alive? ? 'O' : '-'
end

#to_symObject



36
37
38
# File 'lib/ruby-life.rb', line 36

def to_sym
  alive? ? :alive : :dead
end

#updateObject



20
21
22
23
24
25
26
# File 'lib/ruby-life.rb', line 20

def update
  if alive?
    self.alive = false if neighbors < 2 || neighbors > 3
  else
    self.alive = true if neighbors == 3
  end
end