Skip to content

Instantly share code, notes, and snippets.

@yesil
Created February 1, 2018 14:36
Show Gist options
  • Save yesil/379da13c02f8188a3ff7e2f0a0956412 to your computer and use it in GitHub Desktop.
Save yesil/379da13c02f8188a3ff7e2f0a0956412 to your computer and use it in GitHub Desktop.
// work in progress
// white
const dead = 0;
// black
const alive = 1;
const asIs = 2;
// Get all alive neighbours
const aliveNeighbours = (neighbours) => neighbours.filter(neighbour => neighbour === alive);
// A living cell with less than 2 live neighbors dies
const rule1 = (neighbours) => neighbours[4] === alive && aliveNeighbours(neighbours).length - 1 < 2 ? dead : asIs;
// A living cell with 2 or 3 live neighbors lives
const rule2 = (neighbours) => {
if(neighbours[4] === alive){
const count = aliveNeighbours(neighbours).length - 1;
return count === 2 || count === 3 ? alive : dead;
}
return asIs;
};
describe('In Conways Game of Life', function() {
it('A living cell with less than 2 live neighbors dies', function() {
const neighbours = [dead, dead, dead, dead, alive, dead, dead, dead, alive];
assert.equal(rule1(neighbours), dead);
});
it('A living cell with 2 or 3 live neighbors lives', function() {
let neighbours = [dead, dead, dead, alive, dead, dead, alive, alive];
assert.equal(rule2(neighbours), dead);
neighbours = [dead, alive, dead, alive, dead, alive, alive, alive];
// assert.equal(rule2(neighbours), alive);
});
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment