Class: Golr::Game

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(columns, rows, living_cells = []) ⇒ Game

Returns a new instance of Game.



6
7
8
9
10
11
# File 'lib/golr/game.rb', line 6

def initialize(columns, rows, living_cells = [])
  @columns = columns
  @rows = rows
  @grid = init_grid(living_cells)
  @rules = Rules.new
end

Instance Attribute Details

#columnsObject (readonly)

Returns the value of attribute columns.



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

def columns
  @columns
end

#gridObject (readonly)

Returns the value of attribute grid.



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

def grid
  @grid
end

#rowsObject (readonly)

Returns the value of attribute rows.



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

def rows
  @rows
end

Instance Method Details

#alive?(key) ⇒ Boolean

Returns:

  • (Boolean)


33
34
35
# File 'lib/golr/game.rb', line 33

def alive?(key)
  @grid[key] == true
end

#evolveObject



24
25
26
27
28
29
30
31
# File 'lib/golr/game.rb', line 24

def evolve
  next_grid = init_grid
  @grid.each_key do |key|
    next_grid[key] = @rules.evaluate(living_neighbors(key), alive?(key))
  end
  @grid = next_grid
  self
end