class GameOfLife::Cell
GameOfLife::Cell
is a grid element where GameOfLife::Earth
is a grid This @object will mutate based on following Rules (see next!
) Examples based on center element
-
Any live cell with fewer than 2 neighbors dies, as if caused by under population.
Example:
cell = 1 cell.next! = 0 if cell.neighbors < 2 1 = alive 0 = dead
| 0 | 0 | 0 | | 0 | 0 | 0 | | 1 | 1 | 0 | => | 0 | 0 | 0 | | 0 | 0 | 0 | | 0 | 0 | 0 |
-
Any live cell with more than 3 live neighbors dies, as if by overcrowding.
Example:
cell = 1 cell.next! = 0 if cell.neighbors > 3 1 = alive 0 = dead
| 0 | 1 | 0 | | 0 | 1 | 0 | | 1 | 1 | 0 | => | 1 | 0 | 0 | | 0 | 1 | 1 | | 0 | 1 | 0 |
-
Any live cell with 2 or 3 live neighbors lives on to next! generation,
Example:
cell = 1 cell.next! = 1 if cell.neighbors == 3 or cell.neighbors == 2 1 = alive 0 = dead
| 0 | 1 | 0 | | 0 | 1 | 0 | | 1 | 1 | 0 | => | 1 | 1 | 0 | | 0 | 1 | 0 | | 0 | 1 | 0 |
-
Any dead cell with more than 3 live neighbors lives on to next! generation,
Example:
cell = 0 cell.next! = 1 if cell.neighbors > 3 1 = alive 0 = dead
| 0 | 1 | 0 | | 0 | 0 | 0 | | 1 | 0 | 0 | => | 0 | 1 | 0 | | 0 | 1 | 1 | | 0 | 0 | 0 |
Attributes
Public Class Methods
@param [Numeric or Boolean] alive_state
# File lib/game_of_life/cell.rb, line 52 def initialize(alive_state) unless alive_state.is_a?(Numeric) || alive_state.is_a?(NilClass) || alive_state.is_a?(TrueClass) || alive_state.is_a?(FalseClass) raise 'Invalid state' end @alive = if alive_state.is_a?(Numeric) alive_state > rand else alive_state end end
Public Instance Methods
true stands for cell is alive false stands for cell is died @return true or false
# File lib/game_of_life/cell.rb, line 80 def alive? alive end
@return 1 or 0
# File lib/game_of_life/cell.rb, line 85 def bool_to_num alive ? 1 : 0 end
change @alive => false
# File lib/game_of_life/cell.rb, line 90 def die! @alive = false end
change @alive state based on rules
# File lib/game_of_life/cell.rb, line 68 def next! if alive? die! if (neighbours < 2) || (neighbours > 3) relive! if (neighbours == 2) || (neighbours == 3) else relive! if neighbours > 3 end end
change @alive => true
# File lib/game_of_life/cell.rb, line 95 def relive! @alive = true end