-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspec_game_of_life.rb
209 lines (172 loc) · 6.75 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
200
201
202
203
204
205
206
207
208
209
#spec file
require 'rspec'
require_relative 'game_of_life'
# Let: When you have to assign a variable instead of using a before block to create an instance variable.
# This is advanced memory saving stuff!
describe 'Game of Life' do
let!(:cell) { Cell.new(1,1) }
let!(:world) { World.new }
let!(:game) { Game.new(world, [[1, 1]]) }
let!(:window) { GameOfLifeWindow.new }
context 'Ga me' do
subject { game }
it 'Initializes a new game' do
subject.world.is_a?(World).should be_true
subject.world.cell_board[1][1].alive.should be_true
end
end
context 'World' do
subject { World.new }
it 'Responds to the appropriate methods' do
subject.should respond_to(:rows)
subject.should respond_to(:cols)
subject.should respond_to(:cell_board)
subject.should respond_to(:cells)
subject.should respond_to(:randomly_populate)
subject.should respond_to(:live_neighbours_around_cell)
subject.should respond_to(:live_cells)
end
it 'Initializes the cell board properly' do
subject.cell_board.each do |row|
row.is_a?(Array).should be_true
row.each do |element|
element.is_a?(Cell).should be_true
element.alive.should be_false
end
end
it 'Can count cells' do
subject.cells.count.should == subject.rows * subject.cols
end
it 'Can count live cells' do
subject.live_cells.count.should == 0
subject.cell_board[1][1].alive = true
subject.live_cells.count.should == 1
end
it 'Detects live neighbour to the north' do
subject.cell_board[cell.y - 1][cell.x].alive = true
subject.live_neighbours_around_cell(cell).count.should == 1
end
it 'Detects live neighbour to the north-east' do
subject.cell_board[cell.y - 1][cell.x + 1].alive = true
subject.live_neighbours_around_cell(cell).count.should == 1
end
it 'Detects live neighbour to the east' do
subject.cell_board[cell.y][cell.x + 1].alive = true
subject.live_neighbours_around_cell(cell).count.should == 1
end
it 'Detects live neighbour to the south-east' do
subject.cell_board[cell.y + 1][cell.x + 1].alive = true
subject.live_neighbours_around_cell(cell).count.should == 1
end
it 'Detects live neighbour to the south' do
subject.cell_board[cell.y + 1][cell.x].alive = true
subject.live_neighbours_around_cell(cell).count.should == 1
end
it 'Detects live neighbour to the south-west' do
subject.cell_board[cell.y + 1][cell.x - 1].alive = true
subject.live_neighbours_around_cell(cell).count.should == 1
end
it 'Detects live neighbour to the west' do
subject.cell_board[cell.y][cell.x - 1].alive = true
subject.live_neighbours_around_cell(cell).count.should == 1
end
it 'Detects live neighbour to the north-west' do
subject.cell_board[cell.y - 1][cell.x - 1].alive = true
subject.live_neighbours_around_cell(cell).count.should == 1
end
it 'Detects no live neighbours' do
subject.live_neighbours_around_cell(subject.cell_board[1][1]).should == []
end
it 'Counts live and dead cells' do
subject.randomly_populate
(subject.live_cells.count + subject.dead_cells.count).should == subject.cells.count
end
it 'Should randomly populates the world' do
subject.live_cells.should == []
subject.randomly_populate
subject.live_cells.should_not == []
end
it 'Should count live and dead cells' do
subject.randomly_populate
(subject.live_cells.count + subject.dead_cells.count).should == subject.cells.count
end
end
context 'Cell' do
subject { Cell.new }
it 'Initializes a new cell object' do
subject.alive.should be_false
end
it 'Responds to the set methods' do
subject.should respond_to(:x)
subject.should respond_to(:y)
subject.should respond_to(:alive)
end
end
context 'Rules' do
context 'Rule #1: Any live cell with fewer than two live neighbours dies, as if caused by under-population.' do
it 'Kills live cell with no neighbours' do
game.world.cell_board[1][1].should be_alive
game.tick!
game.world.cell_board[1][1].should be_dead
end
it 'Kills live cell with 1 live neighbour' do
game = Game.new(world, [[0, 1], [1, 1]])
game.tick!
game.world.cell_board[0][1].should be_dead
game.world.cell_board[1][1].should be_dead
end
it 'Doesnt kill live cell with 2 neighbours' do
game = Game.new(world, [[0, 1], [1, 1], [2, 1]])
game.tick!
world.cell_board[1][1].should 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 alive cell with 2 neighbours to next generation' do
game = Game.new(world, [[0, 1], [1, 1], [2, 1]])
world.live_neighbours_around_cell(world.cell_board[1][1]).count.should == 2
game.tick!
world.cell_board[0][1].should be_dead
world.cell_board[1][1].should be_alive
world.cell_board[2][1].should be_dead
end
it 'Should keep alive cell with 3 neighbours to next generation' do
game = Game.new(world, [[0, 1], [1, 1], [2, 1], [2, 2]])
world.live_neighbours_around_cell(world.cell_board[1][1]).count.should == 3
game.tick!
world.cell_board[0][1].should be_dead
world.cell_board[1][1].should be_alive
world.cell_board[2][1].should be_alive
world.cell_board[2][2].should 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 live cell with more than 3 live neighbours' do
game = Game.new(world, [[0, 1], [1, 1], [2, 1], [2, 2], [1, 2]])
world.live_neighbours_around_cell(world.cell_board[1][1]).count.should == 4
game.tick!
world.cell_board[0][1].should be_alive
world.cell_board[1][1].should be_dead
world.cell_board[2][1].should be_alive
world.cell_board[2][2].should be_alive
world.cell_board[1][2].should be_dead
end
end
context 'Rule #4: Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.' do
it 'Revives dead cell with 3 neighbours' do
game = Game.new(world, [[0, 1], [1, 1], [2, 1]])
world.live_neighbours_around_cell(world.cell_board[1][0]).count.should == 3
game.tick!
world.cell_board[1][0].should be_alive
world.cell_board[1][2].should be_alive
end
end
end
context 'Game of life window' do
subject { window }
it 'Responds to proper methods' do
subject.should respond_to(:height)
subject.should respond_to(:width)
end
end
end