-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathlife.rb
150 lines (123 loc) · 3.1 KB
/
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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
require 'rubygems'
require 'spec'
class Life
attr_reader :live_cells
def initialize(live_cells)
@live_cells = live_cells
end
def to_a
@live_cells
end
def next_generation
next_gen = []
alive_neighbors.each do |cell,count|
next_gen << cell if alive_next(count, live_cells.include?(cell))
end
self.class.new(next_gen)
end
def alive_neighbors
h = Hash.new(0)
live_cells.each do |x,y|
((x-1)..(x+1)).each do |u|
((y-1)..(y+1)).each do|v|
h[[u,v]] += 1 if ([u,v] != [x,y])
end
end
end
h
end
def alive_next(n, alive_now=true)
if alive_now
(n >= 2 && n <= 3)
else
(n == 3)
end
end
end
describe Life do
before do
@live_cells = [[1, 1], [2, 2]]
@subject = Life.new(@live_cells)
end
it "should exist" do
@subject.should_not be_nil
end
it "should have a serialization" do
@subject.to_a.should == @live_cells
end
it "should advance generations" do
x = @subject.next_generation
x.should be_kind_of Life
end
it "should kill a cell with 0 neigboors" do
x = Life.new([[1,1]])
y = x.next_generation
y.to_a.should_not include([1,1])
end
context "oscillator" do
it "should return the next generation" do
first = Life.new([[1,2], [2,2], [3,2]])
gen = first.next_generation
[[2,1],[2,2], [2,3]].each do |cell|
gen.live_cells.should include cell
end
end
end
context "creating live neighbor hash" do
it "should return a hash for a single coordinate" do
Life.new([[1,1]]).alive_neighbors.should == {
[0,0] => 1,
[0,1] => 1,
[0,2] => 1,
[1,0] => 1,
[1,2] => 1,
[2,0] => 1,
[2,1] => 1,
[2,2] => 1
}
end
it "should return a hash for multiple coordinates" do
Life.new([[1,1],[1,2]]).alive_neighbors.should == {
[0,0] => 1,
[0,1] => 2,
[0,2] => 2,
[0,3] => 1,
[1,0] => 1,
[1,1] => 1,
[1,2] => 1,
[1,3] => 1,
[2,0] => 1,
[2,1] => 2,
[2,2] => 2,
[2,3] => 1
}
end
end
context "the rules" do
context "a live cell on the next generation" do
it "should be dead with 0 live neighbors" do
@subject.alive_next(0).should be_false
end
it "should be dead with 1 live neighbors" do
@subject.alive_next(1).should be_false
end
it "should be alive with 2 live neighbors" do
@subject.alive_next(2).should be_true
end
it "should be alive with 3 live neighbors" do
@subject.alive_next(3).should be_true
end
it "should be dead with 4 live neighbors" do
@subject.alive_next(4).should be_false
end
end
context "a dead cell on the next generation" do
it "should be alive with 3 live neighbors" do
@subject.alive_next(3, false).should be_true
end
it "should be dead with 2 live neighbors" do
@subject.alive_next(2, false).should be_false
end
end
end
end