Class: GameOfLife::Game

Inherits:
Object
  • Object
show all
Defined in:
lib/game_of_life/game.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#boardObject (readonly)

the Game Board



4
5
6
# File 'lib/game_of_life/game.rb', line 4

def board
  @board
end

Instance Method Details

#seed(formatted_input) ⇒ Object

Takes the formatted input data and seeds the Board with it

Examples:

[
  [:dead, :live, :live, :live],
  [:live, :live, nil,   :dead]
]

Parameters:

  • formatted_input (string)


13
14
15
# File 'lib/game_of_life/game.rb', line 13

def seed(formatted_input)
  @board = Board.new(formatted_input)
end

#tickObject

The event by which the board transitions from the current to the next generation/state. There are three stages in the process:

  • reformat the board: Each tick can make the neighboring dead cells (which are not technically in the board now) cells alive. So reformat the board to add a new row at the top and bottom an a column at the left and right (all dead cells)

  • mark and sweep for the next generation

  • shed the dead weight: In stage 1, we added a layer of dead cells around the current board. If the whole layer is still dead, just remove it as we don’t want to keep adding layers of dead weight on every tick.



27
28
29
30
31
# File 'lib/game_of_life/game.rb', line 27

def tick
  @board.reformat_for_next_generation!
  @board.mark_and_sweep_for_next_generation!
  @board.shed_dead_weight!
end