Class: SlackGame::Game::Lifegame::Field

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(size) ⇒ Field

Returns a new instance of Field.



31
32
33
34
# File 'lib/slack_game/game/lifegame.rb', line 31

def initialize(size)
  @size = size
  @field = Array.new(size) { Array.new(size) { rand < 0.3 ? Cell.new(true) : Cell.new(false) } }
end

Instance Attribute Details

#fieldObject (readonly)

Returns the value of attribute field.



30
31
32
# File 'lib/slack_game/game/lifegame.rb', line 30

def field
  @field
end

Instance Method Details

#nextObject



36
37
38
39
40
41
42
43
44
45
# File 'lib/slack_game/game/lifegame.rb', line 36

def next
  next_field = Array.new(@size) { Array.new(@size) }
  @field.each_with_index do |line, outer_index|
    line.each_with_index do |cell, inner_index|
      next_field[outer_index][inner_index] = cell.next_gen(arround_cells(outer_index, inner_index))
    end
  end
  @field = next_field
  self
end

#to_displayObject



47
48
49
50
51
52
53
# File 'lib/slack_game/game/lifegame.rb', line 47

def to_display
  @field.map do |l|
    l.map do |d|
      d.alive? ? Canvas::ARIVE : Canvas::DEAD
    end
  end
end