-
Notifications
You must be signed in to change notification settings - Fork 1
/
game.of.life-spec.js
271 lines (219 loc) · 6.58 KB
/
game.of.life-spec.js
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
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
/* global querySelector */
describe("the game of life", function () {
describe("the grid", function () {
it("should be defined", function () {
var grid = new Game()
expect(grid).toBeDefined()
})
it("should have a width and height", function () {
var w = 10
, h = 20
, grid = new Game(w, h)
expect(grid.width).toEqual(w)
expect(grid.height).toEqual(h)
})
it("should allow positions on the grid to be spawned", function () {
expect(new Game().spawn).toBeDefined()
})
it("should be able to getClone", function() {
expect(new Game().getClone).toBeDefined()
})
it("should get a clone of the grid with correct width and height", function() {
var grid = new Game(2,2)
, board = grid.getClone()
expect(board.length).toBe(grid.width)
expect(board[0].length).toBe(grid.height)
})
it("should get a clone with the cells spawned", function() {
var grid = new Game(3,3)
, board
grid.spawn(1,1)
grid.spawn(0,1)
board = new Game()
board.setBoard(grid.getClone())
expect(grid.isAlive(1,1)).toBe(true)
expect(board.isAlive(0,1)).toBe(true)
grid.kill(1,1)
expect(board.isAlive(1,1)).toBe(true)
})
it("should spawn at a location", function () {
var grid = new Game(10, 20)
grid.spawn(1, 2) // y, x
expect(grid.isAlive(1, 2)).toBe(true)
})
it("should spawn only at valid locations", function () {
var grid = new Game(2, 2)
grid.spawn(1, 1)
expect(grid.isAlive(1, 1)).toBe(true)
expect(function () { grid.spawn(5, 4) }).toThrow(new Error("Invalid grid position"))
})
it("should get the number of neighbours", function () {
var grid = new Game(3, 3)
grid.spawn(1, 1)
expect(grid.aliveNeighbours(1, 1)).toBe(0)
grid.spawn(0, 0)
expect(grid.aliveNeighbours(1, 1)).toBe(1)
grid.spawn(0, 1)
expect(grid.aliveNeighbours(1, 1)).toBe(2)
grid.spawn(0, 2)
expect(grid.aliveNeighbours(1, 1)).toBe(3)
grid.spawn(1, 0)
expect(grid.aliveNeighbours(1, 1)).toBe(4)
})
it("should handle out of bounds inputs", function () {
var grid = new Game(2, 2)
expect(grid.aliveNeighbours(-1, 0)).toBe(0)
})
it("should be able to calculate the next round", function() {
var grid = new Game(2, 2)
expect(grid.next).toBeDefined()
})
it("should be able to kill", function() {
var grid = new Game(2, 2)
expect(grid.kill).toBeDefined();
})
it("should be able to kill a cell", function() {
var grid = new Game(2, 2)
grid.spawn(1, 1)
grid.kill(1, 1)
expect(grid.isAlive(1,1)).toBe(false);
})
it("should kill a cell with less than 2 neighbours", function() {
var grid = new Game(2, 2)
grid.spawn(1, 1)
grid.next()
expect(grid.isAlive(1,1)).toBe(false)
})
it("should continue a cell with two neighbours", function() {
var grid = new Game(2, 2)
grid.spawn(1, 1)
grid.spawn(0, 1)
grid.spawn(1, 0)
grid.next()
expect(grid.isAlive(1,1)).toBe(true)
})
it("should continue a cell with three neighbours", function() {
var grid = new Game(2, 2)
grid.spawn(1, 1)
grid.spawn(0, 1)
grid.spawn(1, 0)
grid.spawn(0, 0)
grid.next()
expect(grid.isAlive(1,1)).toBe(true)
})
it("should kill cells with four neighbours", function() {
var grid = new Game(3, 3)
grid.spawn(1, 1)
// neighbours
grid.spawn(0, 0)
grid.spawn(0, 1)
grid.spawn(0, 2)
grid.spawn(1, 0)
grid.next()
expect(grid.isAlive(0,1)).toBe(false)
})
it("should spawn any dead cell with 3 live neighbours", function () {
var grid = new Game(3, 3)
grid.spawn(0, 1)
grid.spawn(1, 0)
grid.spawn(0, 0)
grid.next()
expect(grid.isAlive(1,1)).toBe(true)
})
it("should be able to draw", function () {
var grid = new Game(2,2)
expect(grid.draw).toBeDefined()
})
it("should be able to draw the grid", function () {
var grid = new Game(2,2)
, gridHtml = grid.draw().html
expect(gridHtml.id).toBe('grid')
})
it("should draw the grid with correct number of rows", function () {
var grid = new Game(2,2)
, gridHtml = grid.draw().html
expect(gridHtml.querySelectorAll('.row').length).toBe(2)
})
it("should draw the grid with correct number of cells", function () {
var grid = new Game(2,2)
, gridHtml = grid.draw().html
expect(gridHtml.querySelectorAll('.cell').length).toBe(4)
})
it("should draw the grid with live cells checked", function () {
var grid = new Game(2,2)
grid.spawn(1,1)
grid.spawn(0,1)
gridHtml = grid.draw().html
expect(gridHtml.querySelectorAll('[type="checkbox"]:checked').length).toBe(2)
})
it("should draw the cells at [2, 1] coordinates checked", function () {
var grid = new Game(3,3)
, positionX = 2
, positionY = 1
, gridHtml, x, y, input
grid.spawn(positionX, positionY)
gridHtml = grid.draw().html
input = gridHtml.querySelector('[type="checkbox"]:checked')
x = input.getAttribute('data-x') * 1
y = input.getAttribute('data-y') * 1
expect(x).toBe(positionX)
expect(y).toBe(positionY)
})
it("should check every cell", function() {
var grid = new Game(5,5)
, self = this
grid.search({ each: function(x, y) {
grid.spawn(x, y)
}})
grid.search({each: function(x, y) {
self.expect(grid.isAlive(x, y)).toBe(true)
}})
})
it("should emulate block still life", function () {
var grid = new Game(4,4)
, cells = [
[1,1]
,[2,1]
,[1,2]
,[2,2]
]
for (var index = 0; index < cells.length; index++) {
grid.spawn(cells[index][0], cells[index][1])
}
grid.next()
for (var index = 0; index < cells.length; index++) {
expect(grid.isAlive(cells[index][0], cells[index][1])).toBe(true)
}
})
it("should emulate boat still life", function () {
var grid = new Game(5,5)
, cells = [
[2,1]
,[1,2]
,[3,2]
,[1,3]
,[2,3]
]
for (var index = 0; index < cells.length; index++) {
grid.spawn(cells[index][0], cells[index][1])
}
grid.next()
for (var index = 0; index < cells.length; index++) {
expect(grid.isAlive(cells[index][0], cells[index][1])).toBe(true)
}
})
it("should create looper oscillator", function () {
var grid = new Game(5,5)
grid.spawn(2, 3)
grid.spawn(2, 2)
grid.spawn(2, 1)
expect(grid.aliveNeighbours(2,1)).toBe(1)
expect(grid.aliveNeighbours(2,2)).toBe(2)
expect(grid.aliveNeighbours(1,2)).toBe(3)
grid.next()
expect(grid.isAlive(1,2)).toBe(true)
expect(grid.isAlive(2,2)).toBe(true)
expect(grid.isAlive(3,2)).toBe(true)
})
})
})