-
Notifications
You must be signed in to change notification settings - Fork 1
/
reference_game.rb
56 lines (48 loc) · 1.1 KB
/
reference_game.rb
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
class ReferenceGame
def initialize(fields)
@fields = fields
end
def tick
new_fields = []
fields.each_index do |x|
fields[x].each_index do |y|
new_fields[x] ||= []
new_fields[x][y] = calculate_next_state(x, y)
end
end
@fields = new_fields
end
def calculate_next_state(x, y)
count = count_neighbours(x, y)
if fields[x][y]
return next_state_if_was_alive(count)
else
return next_state_if_was_dead(count)
end
end
def count_neighbours(x, y)
[
field_alive?(x - 1, y - 1),
field_alive?(x - 1, y),
field_alive?(x - 1, y + 1),
field_alive?(x, y - 1),
field_alive?(x, y + 1),
field_alive?(x + 1, y - 1),
field_alive?(x + 1, y),
field_alive?(x + 1, y + 1)
].map { |value| value ? 1 : 0 }.inject(&:+)
end
def field_alive?(xx, yy)
return false if xx < 0 || yy < 0
fields.fetch(xx, []).fetch(yy, false)
end
def next_state_if_was_alive(count)
return count == 2 || count == 3
end
def next_state_if_was_dead(count)
count == 3
end
def fields
@fields
end
end