-
Notifications
You must be signed in to change notification settings - Fork 3
/
game_of_life.rb
64 lines (55 loc) · 1.17 KB
/
game_of_life.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
57
58
59
60
61
62
63
64
class Game
LIVE = [2,3]
BORN = [3]
attr_accessor :grid, :cols, :rows
def initialize(cols, rows)
@cols, @rows = cols, rows
@grid = build_grid
end
def load(*cells)
cells.each {|y,x| grid[y][x] = 1}
end
def to_s
grid.inject('') do |s, rows|
rows.each {|cell| s << cell.to_s}
s << "\n"
end
end
def live_neighbors_count(y,x)
neighbors(y,x).select {|cell| cell == 1}.size
end
def tick
new_grid = build_grid
grid.each_with_index do |row, y|
row.each_with_index do |cell, x|
count = live_neighbors_count(y,x)
new_grid[y][x] = begin
if cell.zero?
BORN.include?(count) ? 1 : 0
else
LIVE.include?(count) ? 1 : 0
end
end
end
end
@grid = new_grid
end
private
def build_grid
Array.new(rows) { Array.new(cols, 0) }
end
def neighbors(y, x)
(-1..1).inject [] do |values, py|
(-1..1).each do |px|
unless py == 0 and px == 0
i = y + py
j = x + px
i = 0 unless i < rows
j = 0 unless j < cols
values << grid[i][j]
end
end
values
end
end
end