-
Notifications
You must be signed in to change notification settings - Fork 0
/
rule110.rb
68 lines (52 loc) · 958 Bytes
/
rule110.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
65
66
WIDTH = 40
RULE = 110
rule = RULE.to_s(2) # 1101110
rule = "0" + rule # 01101110
LIVE_IMG = "C"
DEAD_IMG = "_"
class Cell
LIVE = 1
DEAD = 0
end
Cell.freeze
class Row
def initialize width, rule
@rule = rule
@width = width
@row = []
init_row
end
def init_row
@width.times do
@row.push Cell::DEAD
end
@row[@width - 1] = Cell::LIVE
end
def render
@row.length.times do |i|
if @row[i] == Cell::LIVE
print LIVE_IMG
else
print DEAD_IMG
end
end
puts
end
def next_gen
next_row = []
next_row.push Cell::DEAD
(@width - 2).times do |i|
index = @row[i].to_s + @row[i+1].to_s + @row[i+2].to_s
index = index.to_i 2
next_row[i+1] = @rule[index].to_i
end
next_row.push Cell::DEAD
@row = next_row
end
end
row = Row.new WIDTH, rule
while true
row.render
row.next_gen
sleep 0.5
end