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": { "transform": {
".*": "<rootDir>/node_modules/babel-jest" ".*": "<rootDir>/node_modules/babel-jest"
}, },
"testDirectoryName": "src/test",
"moduleFileExtensions": [ "moduleFileExtensions": [
"js" "js"
] ]

View file

@ -1,10 +1,27 @@
class Cell { class Cell {
constructor() { constructor(living = false) {
this.living = false; this.living = living;
this.liveNeighbors = 0;
} }
toggleLiving() { toggleLiving() {
this.living = !this.living; 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; module.exports = Cell;

View file

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