add conditional living logic

This commit is contained in:
Sorrel Bri 2020-05-23 22:58:19 -07:00
parent 057a0fdd09
commit c73e13927d
3 changed files with 55 additions and 19 deletions

View file

@ -30,7 +30,6 @@
"transform": {
".*": "<rootDir>/node_modules/babel-jest"
},
"testDirectoryName": "src/test",
"moduleFileExtensions": [
"js"
]

View file

@ -1,10 +1,27 @@
class Cell {
constructor() {
this.living = false;
constructor(living = false) {
this.living = living;
this.liveNeighbors = 0;
}
toggleLiving() {
this.living = !this.living;
}
addLiveNeighbor() {
this.liveNeighbors++;
}
setLiving() {
if (this.living && this.liveNeighbors !== 2 && this.liveNeighbors !== 3) {
return (this.living = false);
}
if (this.liveNeighbors === 3) {
return (this.living = true);
}
}
}
// as a stream -> cellStream = Stream(Cell, () => Cell(cellStream.isLiving()))
// in this case GameField = { [x-y]: cellStream }
// communicating with neighbors = filter for (Boolean(Cell.living)) -> Cell neighbors.addLivingNeighbor
// controlling whether to call or not: filter for (Boolean(Cell.living) || Boolean(cell.liveNeighbors)) -> cellStream.next
module.exports = Cell;

View file

@ -2,25 +2,45 @@ const Cell = require("../components/Cell");
describe("Cell functionality", () => {
test("dispatch toggleLiving state should mark living cell dead", () => {
const cell = new Cell(true);
cell.toggleLiving();
expect(cell.living).toEqual(false);
});
test("dispatch toggleLiving state should mark dead cell living", () => {
const cell = new Cell();
cell.toggleLiving();
expect(cell.living).toEqual(true);
});
test.todo("dispatch toggleLiving state should mark dead cell living");
test.todo(
"dispatch add Live Neighbor should increment live neighbors property"
);
test.todo(
"dispatch setLiving should determine life based on neighbors (for living cells)"
);
const livingFromLivingStates = new Array(8).map((_, i) =>
i === 2 || i === 3 ? true : false
);
test.todo(
"dispatch setLiving should determine life based on neighbors (for dead cells)"
);
const livingFromDeadStates = new Array(8).map((_, i) =>
i === 3 ? true : false
);
test("dispatch add Live Neighbor should increment live neighbors property", () => {
const cell = new Cell();
cell.addLiveNeighbor();
expect(cell.liveNeighbors).toEqual(1);
});
const livingFromLivingStates = new Array(8)
.fill()
.map((_, i) => (i === 2 || i === 3 ? true : false));
livingFromLivingStates.forEach((state, liveNeighbors) => {
test(`dispatch setLiving on live cell with ${liveNeighbors} neighbors should result in living = ${state}`, () => {
const cell = new Cell(true);
new Array(liveNeighbors).fill().forEach((_) => cell.addLiveNeighbor());
cell.setLiving();
expect(cell.living).toEqual(state);
});
});
const livingFromDeadStates = new Array(8)
.fill()
.map((_, i) => (i === 3 ? true : false));
livingFromDeadStates.forEach((state, liveNeighbors) => {
test(`dispatch setLiving on dead cell with ${liveNeighbors} neighbors should result in living = ${state}`, () => {
const cell = new Cell();
new Array(liveNeighbors).fill().forEach((_) => cell.addLiveNeighbor());
cell.setLiving();
expect(cell.living).toEqual(state);
});
});
test.todo("dispatch setLiving should reset live neighbors");
});