Module: GameOfLife::Rules
- Defined in:
- lib/game_of_life/rules.rb
Class Method Summary collapse
-
.should_cell_live?(board, cell, x, y) ⇒ Boolean
The rules followed are: 1.
Instance Method Summary collapse
-
#should_cell_live?(board, cell, x, y) ⇒ Boolean
private
The rules followed are: 1.
Class Method Details
.should_cell_live?(board, cell, x, y) ⇒ Boolean
The rules followed are:
-
Any live cell with fewer than two live neighbours dies, as if caused by under-population.
-
Any live cell with two or three live neighbours lives on to the next generation.
-
Any live cell with more than three live neighbours dies, as if by overcrowding.
-
Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
9 10 11 12 13 14 15 16 17 |
# File 'lib/game_of_life/rules.rb', line 9 def should_cell_live?(board, cell, x, y) live_neighbors_count = board.neighbors_of_cell_at(x, y).select { |n| n.alive? }.size case cell.state when :live (2..3).include? live_neighbors_count when :dead live_neighbors_count == 3 end end |
Instance Method Details
#should_cell_live?(board, cell, x, y) ⇒ Boolean (private)
The rules followed are:
-
Any live cell with fewer than two live neighbours dies, as if caused by under-population.
-
Any live cell with two or three live neighbours lives on to the next generation.
-
Any live cell with more than three live neighbours dies, as if by overcrowding.
-
Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
9 10 11 12 13 14 15 16 17 |
# File 'lib/game_of_life/rules.rb', line 9 def should_cell_live?(board, cell, x, y) live_neighbors_count = board.neighbors_of_cell_at(x, y).select { |n| n.alive? }.size case cell.state when :live (2..3).include? live_neighbors_count when :dead live_neighbors_count == 3 end end |