Class: Rubylife::Game
- Inherits:
-
Object
- Object
- Rubylife::Game
- Defined in:
- lib/rubylife.rb
Instance Attribute Summary collapse
-
#board ⇒ Object
readonly
Returns the value of attribute board.
Instance Method Summary collapse
-
#initialize(board = Board.new, seed = []) ⇒ Game
constructor
A new instance of Game.
- #tick! ⇒ Object
Constructor Details
#initialize(board = Board.new, seed = []) ⇒ Game
Returns a new instance of Game.
10 11 12 13 14 15 16 17 |
# File 'lib/rubylife.rb', line 10 def initialize(board=Board.new, seed=[]) @board = board seed.each do |cell| board.grid[cell[0]][cell[1]].alive=true end end |
Instance Attribute Details
#board ⇒ Object (readonly)
Returns the value of attribute board.
8 9 10 |
# File 'lib/rubylife.rb', line 8 def board @board end |
Instance Method Details
#tick! ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 |
# File 'lib/rubylife.rb', line 19 def tick! live_cells = [] dead_cells = [] @board.cells.each do |cell| # Rule #1: # Any live cell with fewer than two live neighbours dies, as if caused by underpopulation. if (cell.alive? && @board.live_neighbours(cell).size < 2) dead_cells << cell end # Rule #2: # Any live cell with two or three live neighbours lives on to the next generation. if (cell.alive? && (@board.live_neighbours(cell).size == 2 || @board.live_neighbours(cell) == 3)) live_cells << cell end # Rule #3: # Any live cell with more than three live neighbours dies, as if by overcrowding.. if (cell.alive? && @board.live_neighbours(cell).size > 3) dead_cells << cell end # Rule #4: # Any dead cell with exactly three live neighbours becomes a live cell,as if by reproduction. if (cell.dead? && @board.live_neighbours(cell).size == 3) live_cells << cell end end live_cells.each { |cell| cell.alive = true} dead_cells.each { |cell| cell.alive = false} end |