Module: GameOfLife::Rules

Defined in:
lib/game_of_life/rules.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.should_cell_live?(board, cell, x, y) ⇒ Boolean

The rules followed are:

  1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.

  2. Any live cell with two or three live neighbours lives on to the next generation.

  3. Any live cell with more than three live neighbours dies, as if by overcrowding.

  4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

Returns:

  • (Boolean)


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:

  1. Any live cell with fewer than two live neighbours dies, as if caused by under-population.

  2. Any live cell with two or three live neighbours lives on to the next generation.

  3. Any live cell with more than three live neighbours dies, as if by overcrowding.

  4. Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.

Returns:

  • (Boolean)


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