Class: Conwaymp::Cell

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(state = nil) ⇒ Cell

Returns a new instance of Cell.



7
8
9
10
11
12
13
14
15
# File 'lib/conwaymp/cell.rb', line 7

def initialize(state = nil)
  @state = state ||  (0.15 > rand ? :alive : :dead)
  @living_neighbours = 0

  @icons = {
    alive: ' # '.colorize(:green),
    dead: ' . '.colorize(:red)
  }
end

Instance Attribute Details

#living_neighboursObject

Returns the value of attribute living_neighbours.



5
6
7
# File 'lib/conwaymp/cell.rb', line 5

def living_neighbours
  @living_neighbours
end

#stateObject

Returns the value of attribute state.



5
6
7
# File 'lib/conwaymp/cell.rb', line 5

def state
  @state
end

Instance Method Details

#alive?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/conwaymp/cell.rb', line 42

def alive?
  @state == :alive
end

#dead?Boolean

Returns:

  • (Boolean)


46
47
48
# File 'lib/conwaymp/cell.rb', line 46

def dead?
  @state == :dead
end

#die!Object



54
55
56
# File 'lib/conwaymp/cell.rb', line 54

def die!
  @state = :dead
end

#overpopulated?Boolean

Returns:

  • (Boolean)


30
31
32
# File 'lib/conwaymp/cell.rb', line 30

def overpopulated?
  living_neighbours > 3 && alive?
end


17
18
19
# File 'lib/conwaymp/cell.rb', line 17

def print
  @icons[@state]
end

#rebirth!Object



50
51
52
# File 'lib/conwaymp/cell.rb', line 50

def rebirth!
  @state = :alive
end

#repopulate?Boolean

Returns:

  • (Boolean)


34
35
36
# File 'lib/conwaymp/cell.rb', line 34

def repopulate?
  living_neighbours == 3 && dead?
end

#survives?Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/conwaymp/cell.rb', line 38

def survives?
  ((2..3).cover? living_neighbours) && alive?
end

#tickObject



21
22
23
24
# File 'lib/conwaymp/cell.rb', line 21

def tick
  die! if underpopulated? || overpopulated?
  rebirth! if repopulate? || survives?
end

#underpopulated?Boolean

Returns:

  • (Boolean)


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

def underpopulated?
  living_neighbours < 2 && alive?
end