Class: BrowserConwayGameOfLife::Cell

Inherits:
Object
  • Object
show all
Defined in:
lib/browser_conway_game_of_life/cell.rb

Constant Summary collapse

NEIGHBOURS =
[
  [-1, -1], [-1, 0], [-1, 1],
  [0, -1], [0, 1],
  [1, -1], [1, 0], [1, 1]
]

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value = rand(0..1)) ⇒ Cell

Returns a new instance of Cell.



11
12
13
# File 'lib/browser_conway_game_of_life/cell.rb', line 11

def initialize(value = rand(0..1))
  (0..1).include?(value) ? @value = value : @value = rand(0..1)
end

Instance Attribute Details

#valueObject

Returns the value of attribute value.



9
10
11
# File 'lib/browser_conway_game_of_life/cell.rb', line 9

def value
  @value
end

Class Method Details

.live_neighbours(universe, i, j) ⇒ Object



31
32
33
34
35
# File 'lib/browser_conway_game_of_life/cell.rb', line 31

def self.live_neighbours(universe, i, j)
  NEIGHBOURS.inject(0) do |sum, pos|
    sum + universe[(i + pos[0]) % universe.size][(j + pos[1]) % universe[0].size].value
  end
end

Instance Method Details

#change!Object



23
24
25
# File 'lib/browser_conway_game_of_life/cell.rb', line 23

def change!
  self.value = self.value.send(:+, -1).abs
end

#dead?Boolean

Returns:

  • (Boolean)


19
20
21
# File 'lib/browser_conway_game_of_life/cell.rb', line 19

def dead?
  !live?
end

#live?Boolean

Returns:

  • (Boolean)


15
16
17
# File 'lib/browser_conway_game_of_life/cell.rb', line 15

def live?
  @value == 1
end

#to_sObject



27
28
29
# File 'lib/browser_conway_game_of_life/cell.rb', line 27

def to_s
  live? ? 'O' : ' '
end