-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcycle_handler.rb
More file actions
59 lines (48 loc) · 1 KB
/
Copy pathcycle_handler.rb
File metadata and controls
59 lines (48 loc) · 1 KB
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
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
class CycleHandler
def initialize(cells, height, width)
@cells = cells
@height = height
@width = width
end
def cycle_cells
set_next_states
step_cells
@cells
end
private
def set_next_states
for_each_cell do |cell, x, y|
cell.set_next_state(number_living_neighbors(x, y))
end
end
def number_living_neighbors(cell_x, cell_y)
num_alive = 0
((cell_x - 1)..(cell_x+1)).each do |x|
next if x < 0 || x >= @height
(cell_y-1..cell_y+1).each do |y|
next if y < 0 || y >= @width || (x==cell_x && y==cell_y)
num_alive += 1 if @cells[x][y].alive?
end
end
num_alive
end
def step_cells
for_each_cell do |cell|
cell.step
end
end
def for_each_cell(&block)
@cells.each_with_index do |row, x|
row.each_with_index do |cell, y|
case block.arity
when 0
yield
when 1
yield cell
when 3
yield cell, x, y
end
end
end
end
end