Skip to content

Instantly share code, notes, and snippets.

@devill
Created August 2, 2021 19:11
Show Gist options
  • Select an option

  • Save devill/a029a4c8ca7f0843a01c5dcb78e83713 to your computer and use it in GitHub Desktop.

Select an option

Save devill/a029a4c8ca7f0843a01c5dcb78e83713 to your computer and use it in GitHub Desktop.
import expect from 'expect.js'
const DEAD = 0;
const ALIVE = 1;
function evolve(world) {
function countNeighbours(x, y) {
let count = 0;
for(let i = x - 1; i <= x + 1; i++) {
for (let j = y - 1; j <= y + 1; j++) {
if(i in world && j in world[i]) {
count += world[i][j];
}
}
}
return count - world[x][y];
}
function shouldStayAlive(i, j) {
let count = countNeighbours(i, j);
return (count === 2 && world[i][j] === ALIVE) || count === 3;
}
let newWorld = [];
for(let i = 0; i < world.length; i++) {
let line = [];
for(let j = 0; j < world[i].length; j++) {
line.push(shouldStayAlive(i, j) ? ALIVE : DEAD) ;
}
newWorld.push(line);
}
return newWorld;
}
describe("Game of life", () => {
const _ = DEAD;
const X = ALIVE;
specify("that empty world evolves to empty", () => {
const emptyWorld = [
[_,_,_],
[_,_,_],
[_,_,_]
];
expect(evolve(emptyWorld)).to.eql(emptyWorld);
});
specify("that a lonely cell dies", () => {
const world = [
[_,_,_],
[_,X,_],
[_,_,_]
];
const emptyWorld = [
[_,_,_],
[_,_,_],
[_,_,_]
];
expect(evolve(world)).to.eql(emptyWorld);
});
specify("that a cell with a single neighbour dies", () => {
const world = [
[_,_,_],
[X,X,_],
[_,_,_]
];
expect(evolve(world)[1][1]).to.be(DEAD);
});
const examplesOfSurvivingCells = [
[
[_,_,_],
[X,X,X],
[_,_,_]
],[
[_,X,_],
[_,X,_],
[_,X,_]
],[
[_,X,_],
[_,X,X],
[_,X,_]
],
];
for(let i = 0; i < examplesOfSurvivingCells.length; i++) {
specify(`that a cell with two or thee neighbours stays alive case #${i}`, () => {
const emptyWorld = [
[_,_,_],
[_,X,_],
[_,_,_]
];
expect(evolve(examplesOfSurvivingCells[i])[1][1]).to.be(ALIVE);
});
}
specify("that a dead cell with two neighbours does not spawn", () => {
const world = [
[_,_,_],
[X,_,X],
[_,_,_]
];
expect(evolve(world)[1][1]).to.be(DEAD);
});
specify("that a dead cell with three neighbours spawns", () => {
const world = [
[_,X,_],
[X,_,X],
[_,_,_]
];
expect(evolve(world)[1][1]).to.be(ALIVE);
});
specify("that a cell with four neighbours dies", () => {
const world = [
[_,X,_],
[X,X,X],
[_,X,_]
];
expect(evolve(world)[1][1]).to.be(DEAD);
});
specify("that an edge cell is spawns with three neighbours", () => {
const world = [
[_,_,_],
[X,X,X],
[_,_,_]
];
expect(evolve(world)[0][1]).to.be(ALIVE);
});
specify("that an edge cell with three neighbours spawns", () => {
const world = [
[_,_,_],
[_,_,_],
[X,X,X]
];
expect(evolve(world)[2][1]).to.be(ALIVE);
});
specify("that a corner cell with three neighbours spawns", () => {
const world = [
[_,_,_],
[X,X,_],
[_,X,_]
];
expect(evolve(world)[2][0]).to.be(ALIVE);
});
specify("that the height of the new world is the same as the height of the input", () => {
const world = [
[_,_,_],
[_,_,_],
[_,_,_],
[_,_,_]
];
expect(evolve(world).length).to.be(4);
});
specify("that the width of the new world is the same as the width of the input", () => {
const world = [
[_,_,_,_,_],
];
expect(evolve(world)[0].length).to.be(5);
});
specify("that a complex example evolves correctly", () => {
const world = [
[_,_,_,_],
[_,X,X,X],
[_,X,_,_],
[_,X,X,_],
[_,_,_,_],
];
const result = [
[_,_,X,_],
[_,X,X,_],
[X,_,_,X],
[_,X,X,_],
[_,_,_,_],
];
expect(evolve(world)).to.eql(result);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment