-
Notifications
You must be signed in to change notification settings - Fork 0
/
spec_game_of_life.rb
199 lines (170 loc) · 6.78 KB
/
spec_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
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
# Spec file
require 'rspec'
require_relative 'game_of_life.rb'
describe 'Game of life' do
let!(:world) { World.new }
let!(:cell) { Cell.new(1, 1) }
context 'World' do
subject { World.new }
it 'should create a new world object' do
expect(subject.is_a?(World)).to be true
end
it 'should respond to proper methods' do
expect(subject).to respond_to(:rows)
expect(subject).to respond_to(:cols)
expect(subject).to respond_to(:cell_grid)
expect(subject).to respond_to(:live_neighbours_around_cell)
expect(subject).to respond_to(:cells)
expect(subject).to respond_to(:randomly_populate)
expect(subject).to respond_to(:live_cells)
end
it 'should create proper cell grid on initialization' do
expect(subject.cell_grid.is_a?(Array)).to be true
subject.cell_grid.each do |row|
expect(row.is_a?(Array)).to be true
row.each do |col|
expect(col.is_a?(Cell)).to be true
end
end
end
it 'should add all cells to cells array' do
expect(subject.cells.count).to eq(9)
end
it 'should detect a neighbour to the north' do
subject.cell_grid[cell.y - 1][cell.x].alive = true
expect(subject.live_neighbours_around_cell(cell).count).to eq(1)
end
it 'should detect a neighbour to the north east' do
subject.cell_grid[cell.y - 1][cell.x + 1].alive = true
expect(subject.live_neighbours_around_cell(cell).count).to eq(1)
end
it 'should detect a neighbour to the east' do
subject.cell_grid[cell.y][cell.x + 1].alive = true
expect(subject.live_neighbours_around_cell(cell).count).to eq(1)
end
it 'should detect a neighbour to the south east' do
subject.cell_grid[cell.y + 1][cell.x + 1].alive = true
expect(subject.live_neighbours_around_cell(cell).count).to eq(1)
end
it 'should detect a neighbour to the south' do
subject.cell_grid[cell.y + 1][cell.x].alive = true
expect(subject.live_neighbours_around_cell(cell).count).to eq(1)
end
it 'should detect a neighbour to the south west' do
subject.cell_grid[cell.y + 1][cell.x - 1].alive = true
expect(subject.live_neighbours_around_cell(cell).count).to eq(1)
end
it 'should detect a neighbour to the west' do
subject.cell_grid[cell.y][cell.x - 1].alive = true
expect(subject.live_neighbours_around_cell(cell).count).to eq(1)
end
it 'should detect a neighbour to the north west' do
subject.cell_grid[cell.y - 1][cell.x - 1].alive = true
expect(subject.live_neighbours_around_cell(cell).count).to eq(1)
end
it 'should randomly populate the world' do
expect(subject.live_cells.count).to eq(0)
subject.randomly_populate
expect(subject.live_cells.count).not_to eq(0)
end
end
context 'Cell' do
subject { Cell.new }
it 'should create a new cell object' do
expect(subject.is_a?(Cell)).to be true
end
it 'should respond to proper methods' do
expect(subject).to respond_to(:alive)
expect(subject).to respond_to(:x)
expect(subject).to respond_to(:y)
expect(subject).to respond_to(:alive?)
expect(subject).to respond_to(:die!)
end
it 'should initzialize properly' do
expect(subject.alive).to be false
expect(subject.x).to be(0)
expect(subject.y).to be(0)
end
end
context 'Game' do
subject { Game.new }
it 'should create new Game object' do
expect(subject.is_a?(Game)).to be true
end
it 'should respond to proper methods' do
expect(subject).to respond_to(:world)
expect(subject).to respond_to(:seeds)
end
it 'should initzialize properly' do
expect(subject.world.is_a?(World)).to be true
expect(subject.seeds.is_a?(Array)).to be true
end
it 'should plant seeds properly' do
game = Game.new(world, [[1, 0], [2, 0]])
expect(world.cell_grid[1][0]).to be_alive
expect(world.cell_grid[2][0]).to be_alive
end
end
context 'Rules' do
let!(:game) { Game.new }
context 'Rule 1: Any live cell with fewer than two live neighbours dies, as if caused by under-population.' do
it 'should kill a alive cell with no neighbours' do
game.world.cell_grid[1][1].alive = true
expect(game.world.cell_grid[1][1]).to be_alive
game.tick!
expect(game.world.cell_grid[1][1]).to be_dead
end
it 'should kill a alive cell with one alive neighbour' do
game = Game.new(world, [[1, 0], [2, 0]])
game.tick!
expect(world.cell_grid[1][0]).to be_dead
expect(world.cell_grid[2][0]).to be_dead
end
it 'should kill a alive cell with two alive neighbour' do
game = Game.new(world, [[0, 1], [2, 1], [1, 1]])
game.tick!
expect(world.cell_grid[1][1]).to be_alive
end
end
context 'Rule 2: Any live cell with two or three live neighbours lives on to the next generation.' do
it 'should keep cell alive that has two alive neighbours' do
game = Game.new(world, [[0, 1], [2, 1], [1, 1]])
expect(world.live_neighbours_around_cell(world.cell_grid[1][1]).count).to eq(2)
game.tick!
expect(world.cell_grid[0][1]).to be_dead
expect(world.cell_grid[1][1]).to be_alive
expect(world.cell_grid[2][1]).to be_dead
end
it 'should keep cell alive that has three alive neighbours' do
game = Game.new(world, [[0, 1], [2, 1], [1, 1], [2, 2]])
expect(world.live_neighbours_around_cell(world.cell_grid[1][1]).count).to eq(3)
game.tick!
expect(world.cell_grid[0][1]).to be_dead
expect(world.cell_grid[1][1]).to be_alive
expect(world.cell_grid[2][1]).to be_alive
expect(world.cell_grid[2][2]).to be_alive
end
end
context 'Rule 3: Any live cell with more than three live neighbours dies, as if by overcrowding.' do
it 'should kill a alive cell with four alive neighbours' do
game = Game.new(world, [[0, 1], [2, 1], [1, 1], [2, 2], [1, 2]])
expect(world.live_neighbours_around_cell(world.cell_grid[1][1]).count).to eq(4)
game.tick!
expect(world.cell_grid[0][1]).to be_alive
expect(world.cell_grid[1][1]).to be_dead
expect(world.cell_grid[2][1]).to be_alive
expect(world.cell_grid[2][2]).to be_alive
expect(world.cell_grid[1][2]).to be_dead
end
end
context 'Rule 4: Any dead cell with excactly three live neighbours becomes a live cell, as if by rerpoduction.' do
it 'revives dead cells with three alive neighbours' do
game = Game.new(world, [[0, 1], [2, 1], [1, 1]])
expect(world.live_neighbours_around_cell(world.cell_grid[1][0]).count).to eq(3)
game.tick!
expect(world.cell_grid[1][0]).to be_alive
expect(world.cell_grid[1][2]).to be_alive
end
end
end
end