-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgame.rb
More file actions
42 lines (35 loc) · 837 Bytes
/
game.rb
File metadata and controls
42 lines (35 loc) · 837 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
class Game
attr_accessor :world
def initialize(world)
@world = world
end
def next_step!
next_step_live_cells = []
next_step_dead_cells = []
@world.cells.each do |cell|
neighbour_count = self.world.live_neighbours(cell).count
#1
if cell.alive? && neighbour_count < 2
next_step_dead_cells << cell
end
#2
if cell.alive? && neighbour_count > 3
next_step_dead_cells << cell
end
#3
if cell.alive? && (neighbour_count == 2 || neighbour_count == 3)
next_step_live_cells << cell
end
#4
if cell.dead? && neighbour_count == 3
next_step_live_cells << cell
end
end
next_step_live_cells.each do |cell|
cell.rise!
end
next_step_dead_cells.each do |cell|
cell.die!
end
end
end