-
Notifications
You must be signed in to change notification settings - Fork 0
/
game_spec.rb
212 lines (164 loc) · 6.02 KB
/
game_spec.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
# frozen_string_literal: true
require 'rspec'
require_relative './game'
RSpec.describe Cell do
context 'when a cell is new' do
it 'is dead' do
expect(subject.alive?).to eq(false)
end
end
context 'when the cell has 4 neighbrours' do
let(:neighbours) { Array.new(4) { Cell.new } }
it 'knows when all neighbours are dead' do
subject.add_neighbours(neighbours)
expect(subject.living_neighbour_count).to eq(0)
end
it 'knows how many neighbours are alive' do
# let's rise two neighbours from the dead
neighbours[0].alive = true
neighbours[1].alive = true
subject.add_neighbours(neighbours)
expect(subject.living_neighbour_count).to eq(2)
end
end
context 'when time takes a step' do
let(:neighbours) { Array.new(4) { Cell.new } }
context 'the cell is alive' do
subject { Cell.new(alive: true) }
it 'will die if it has less than 2 live neighbours' do
subject.add_neighbours(neighbours)
subject.tick
expect(subject.alive?).to eq(false)
end
it 'will die if it has more than 3 live neighbours' do
0.upto(3) { |idx| neighbours[idx].alive = true }
subject.add_neighbours(neighbours)
subject.tick
expect(subject.alive?).to eq(false)
end
it 'will stay alive if it has 2 neighbours' do
0.upto(1) { |idx| neighbours[idx].alive = true }
subject.add_neighbours(neighbours)
subject.tick
expect(subject.alive?).to eq(true)
end
it 'will stay alive if it has 3 neighbours' do
0.upto(2) { |idx| neighbours[idx].alive = true }
subject.add_neighbours(neighbours)
subject.tick
expect(subject.alive?).to eq(true)
end
it 'will stay alive if the last 3 added neighbours are alive' do
1.upto(3) { |idx| neighbours[idx].alive = true }
subject.add_neighbours(neighbours)
subject.tick
expect(subject.alive?).to eq(true)
end
end
context 'the cell is dead' do
subject { Cell.new(alive: false) }
it 'stays dead if there are less than 3 neighbours' do
1.upto(2) { |idx| neighbours[idx].alive = true }
subject.add_neighbours(neighbours)
subject.tick
expect(subject.alive?).to eq(false)
end
it 'should become alive if has 3 live neighbours' do
1.upto(3) { |idx| neighbours[idx].alive = true }
subject.add_neighbours(neighbours)
subject.tick
expect(subject.alive?).to eq(true)
end
it 'stays dead if there are more than 3 neighbours' do
0.upto(3) { |idx| neighbours[idx].alive = true }
subject.add_neighbours(neighbours)
subject.tick
expect(subject.alive?).to eq(false)
end
end
end
end
RSpec.describe Game do
let(:board_1x1) { Game.new([[Cell.new(alive: false)]]) }
let(:board_3x3) do
Game.new(
[
[Cell.new(alive: false), Cell.new(alive: false), Cell.new(alive: false)],
[Cell.new(alive: false), Cell.new(alive: false), Cell.new(alive: false)],
[Cell.new(alive: false), Cell.new(alive: false), Cell.new(alive: false)]
]
)
end
context 'when initialising the board' do
subject { board_1x1 }
it 'can tell if the cell at coordinates x:0, y:0 is alive' do
expect(subject.cell_alive?(x: 0, y: 0)).to be(false)
end
context 'when cells get acquinted with their neigbours' do
subject { board_3x3 }
it 'tells that the 0:0 cell has neighbours 1:0, 0:1 and 1:1' do
expect(subject.cell_at(x: 0, y: 0).neighbours).to include(
subject.cell_at(x: 1, y: 0),
subject.cell_at(x: 0, y: 1),
subject.cell_at(x: 1, y: 1)
)
end
it 'tells that the 0:2 cell has neighbours 1:2, 0:1 and 1:1' do
expect(subject.cell_at(x: 0, y: 2).neighbours).to include(
subject.cell_at(x: 0, y: 1),
subject.cell_at(x: 1, y: 1),
subject.cell_at(x: 1, y: 2)
)
end
it 'tells that the 2:0 cell has neighbours 2:1, 1:0 and 1:1' do
expect(subject.cell_at(x: 2, y: 0).neighbours).to include(
subject.cell_at(x: 1, y: 0),
subject.cell_at(x: 1, y: 1),
subject.cell_at(x: 2, y: 1)
)
end
it 'tells that the 2:2 cell has neighbours 2:1, 1:2 and 1:1' do
expect(subject.cell_at(x: 2, y: 2).neighbours).to include(
subject.cell_at(x: 1, y: 2),
subject.cell_at(x: 1, y: 1),
subject.cell_at(x: 2, y: 1)
)
end
it 'tells that the 1:1 cell has all 8 neighbours linked' do
expect(subject.cell_at(x: 1, y: 1).neighbours).to include(
subject.cell_at(x: 0, y: 0),
subject.cell_at(x: 1, y: 0),
subject.cell_at(x: 2, y: 0),
subject.cell_at(x: 0, y: 1),
subject.cell_at(x: 2, y: 1),
subject.cell_at(x: 0, y: 2),
subject.cell_at(x: 1, y: 2),
subject.cell_at(x: 2, y: 2)
)
end
end
end
context 'when time takes a step (some integration tests)' do
subject { board_3x3 }
it 'if there is a single live cell on the baord it dies' do
subject.cell_at(x: 1, y: 1).alive = true
subject.tick
expect(subject.cell_alive?(x: 1, y: 1)).to be(false)
end
it 'if there is a single live cell on the baord it dies' do
subject.cell_at(x: 0, y: 0).alive = true
subject.cell_at(x: 1, y: 0).alive = true
subject.cell_at(x: 2, y: 0).alive = true
subject.tick
expect(subject.cell_alive?(x: 0, y: 0)).to be(false)
expect(subject.cell_alive?(x: 1, y: 0)).to be(true)
expect(subject.cell_alive?(x: 2, y: 0)).to be(false)
expect(subject.cell_alive?(x: 0, y: 1)).to be(false)
expect(subject.cell_alive?(x: 1, y: 1)).to be(true)
expect(subject.cell_alive?(x: 2, y: 1)).to be(false)
expect(subject.cell_alive?(x: 0, y: 2)).to be(false)
expect(subject.cell_alive?(x: 1, y: 2)).to be(false)
expect(subject.cell_alive?(x: 2, y: 2)).to be(false)
end
end
end