Alright, this time we are going to finish off test-driving the game logic. Let's jump right in with the next test.
Test-Driving the Game Module
Now, we want to introduce the ability to set a cell in the grid. The following test will make sure that we can set a single cell in the grid:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('Game of Life', function () { | |
var game; | |
beforeEach(function () { | |
game = Game(30); | |
}); | |
//...other tests | |
it('should set cell', function () { | |
game.setCell(1,1); | |
expect(game.grid()[1][1]).toBe(1); | |
}); | |
}); |
Now run the following command:
$ karma start
You should now see 2 tests running green!
Conway's Game of Life has 4 rules:
- Any live cell with fewer than two live neighbours dies, as if caused by under-population.
- Any live cell with two or three live neighbours lives on to the next generation.
- Any live cell with more than three live neighbours dies, as if by overcrowding.
- Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.
Here are the tests for the first rule:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('Any live cell with fewer than two live neighbours dies, as if caused by under-population.', function () { | |
it('should kill cell with no live neighbours', function () { | |
game.setCell(3,3); | |
game.nextFrame(); | |
expect(game.grid()[3][3]).toBe(0); | |
}); | |
it('should kill cell with one live neighbour', function () { | |
game.setCell(3,2); | |
game.setCell(3,3); | |
game.nextFrame(); | |
expect(game.grid()[3][3]).toBe(0); | |
}); | |
it('should kill cell with one live neighbour in the previous row', function () { | |
game.setCell(2,2); | |
game.setCell(3,3); | |
game.nextFrame(); | |
expect(game.grid()[3][3]).toBe(0); | |
}); | |
it('should kill cell with one live neighbour in the next row', function () { | |
game.setCell(4,4); | |
game.setCell(3,3); | |
game.nextFrame(); | |
expect(game.grid()[3][3]).toBe(0); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
var nextFrame = function () { | |
function countRow(row, prevColumn, nextColumn, isNotCurrent) { | |
var result = 0; | |
if (row) { | |
if (prevColumn) { | |
result += row[prevColumn]; | |
} | |
if (nextColumn) { | |
result += row[nextColumn]; | |
} | |
if (isNotCurrent) { | |
result += row[prevColumn + 1] | |
} | |
} | |
return result; | |
} | |
var gridClone = _grid.map(function (arr) { | |
return arr.slice(); | |
}); | |
for (var x = gridClone.length - 1; x >= 0; x--) { | |
var currentRow = gridClone[x], | |
prevRow = null, | |
nextRow = null; | |
if (x > 0) { | |
prevRow = gridClone[x - 1]; | |
} | |
if (x < (gridClone.length - 1)) { | |
nextRow = gridClone[x + 1]; | |
} | |
for (var y = currentRow.length - 1; y >= 0; y--) { | |
var count = 0, | |
prevColumn = null, | |
nextColumn = null; | |
if (y > 0) { | |
prevColumn = y - 1; | |
} | |
if (y < (currentRow.length - 1)) { | |
nextColumn = y + 1; | |
} | |
count += countRow(prevRow, prevColumn, nextColumn, true); | |
count += countRow(nextRow, prevColumn, nextColumn, true); | |
count += countRow(currentRow, prevColumn, nextColumn); | |
if (count < 2) { | |
_grid[x][y] = 0; | |
} | |
}; | |
}; | |
}; | |
return function (size) { | |
init(size); | |
return { | |
grid: grid, | |
setCell: setCell, | |
nextFrame: nextFrame | |
}; | |
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (count < 2) { | |
_grid[x][y] = 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('Any live cell with two or three live neighbours lives on to the next generation.', function () { | |
it('should save cell with two live neighbours', function () { | |
game.setCell(3,2); | |
game.setCell(3,4); | |
game.setCell(3,3); | |
game.nextFrame(); | |
expect(game.grid()[3][3]).toBe(1); | |
}); | |
it('should save cell with three live neighbours', function () { | |
game.setCell(3,2); | |
game.setCell(3,4); | |
game.setCell(4,4); | |
game.setCell(3,3); | |
game.nextFrame(); | |
expect(game.grid()[3][3]).toBe(1); | |
}); | |
it('should save cell with three live neighbours on different rows', function () { | |
game.setCell(2,2); | |
game.setCell(3,4); | |
game.setCell(4,4); | |
game.setCell(3,3); | |
game.nextFrame(); | |
expect(game.grid()[3][3]).toBe(1); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('Any live cell with more than three live neighbours dies, as if by overcrowding.', function () { | |
it('should kill cell with four live neighbours on different rows', function () { | |
game.setCell(2,2); | |
game.setCell(3,4); | |
game.setCell(4,4); | |
game.setCell(4,3); | |
game.setCell(3,3); | |
game.nextFrame(); | |
expect(game.grid()[3][3]).toBe(0); | |
}); | |
it('should kill cell with five live neighbours on different rows', function () { | |
game.setCell(2,2); | |
game.setCell(3,4); | |
game.setCell(4,4); | |
game.setCell(4,3); | |
game.setCell(4,2); | |
game.setCell(3,3); | |
game.nextFrame(); | |
expect(game.grid()[3][3]).toBe(0); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (count < 2 || count > 3) { | |
_grid[x][y] = 0; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
describe('Any dead cell with exactly three live neighbours becomes a live cell, as if by reproduction.', function () { | |
it('should set cell with three live neighbours on different rows', function () { | |
game.setCell(2,2); | |
game.setCell(3,4); | |
game.setCell(4,4); | |
game.nextFrame(); | |
expect(game.grid()[3][3]).toBe(1); | |
}); | |
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (count < 2 || count > 3) { | |
_grid[x][y] = 0; | |
} | |
if (count === 3) { | |
_grid[x][y] = 1; | |
} |
Ok, now we are in the position to add the environment for React and start to build up our web components. This will be topic of the next instalment.
No comments:
Post a Comment