Class: Conwaymp::Cell
- Inherits:
-
Object
- Object
- Conwaymp::Cell
- Defined in:
- lib/conwaymp/cell.rb
Instance Attribute Summary collapse
-
#living_neighbours ⇒ Object
Returns the value of attribute living_neighbours.
-
#state ⇒ Object
Returns the value of attribute state.
Instance Method Summary collapse
- #alive? ⇒ Boolean
- #dead? ⇒ Boolean
- #die! ⇒ Object
-
#initialize(state = nil) ⇒ Cell
constructor
A new instance of Cell.
- #overpopulated? ⇒ Boolean
- #print ⇒ Object
- #rebirth! ⇒ Object
- #repopulate? ⇒ Boolean
- #survives? ⇒ Boolean
- #tick ⇒ Object
- #underpopulated? ⇒ Boolean
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_neighbours ⇒ Object
Returns the value of attribute living_neighbours.
5 6 7 |
# File 'lib/conwaymp/cell.rb', line 5 def living_neighbours @living_neighbours end |
#state ⇒ Object
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
42 43 44 |
# File 'lib/conwaymp/cell.rb', line 42 def alive? @state == :alive end |
#dead? ⇒ 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
30 31 32 |
# File 'lib/conwaymp/cell.rb', line 30 def overpopulated? living_neighbours > 3 && alive? end |
#print ⇒ Object
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
34 35 36 |
# File 'lib/conwaymp/cell.rb', line 34 def repopulate? living_neighbours == 3 && dead? end |
#survives? ⇒ Boolean
38 39 40 |
# File 'lib/conwaymp/cell.rb', line 38 def survives? ((2..3).cover? living_neighbours) && alive? end |
#tick ⇒ Object
21 22 23 24 |
# File 'lib/conwaymp/cell.rb', line 21 def tick die! if underpopulated? || overpopulated? rebirth! if repopulate? || survives? end |
#underpopulated? ⇒ Boolean
26 27 28 |
# File 'lib/conwaymp/cell.rb', line 26 def underpopulated? living_neighbours < 2 && alive? end |