add Game Field seed to constructor

This commit is contained in:
Sorrel Bri 2020-05-23 23:23:52 -07:00
parent c73e13927d
commit f68cd3a62a
3 changed files with 43 additions and 2 deletions

View file

@ -0,0 +1,18 @@
import Cell from "./Cell";
export default class GameField {
constructor(seed) {
// seed = [ [] ]
this.map = {};
seed.forEach((subArray, majorIndex) =>
subArray.forEach((value, minorIndex) => {
if (value > 0) {
this.map[`${majorIndex}-${minorIndex}`] = new Cell(true);
}
})
);
// instead of implementing multiple GameFields, clear irrelevant keys and expand Game Field as needed
}
}
// as a stream -> fieldStream => Stream(GameField, () => Stream(GameField.computeNeighbors(), () => Stream(GameField.setLiving()))

View file

@ -1,4 +1,4 @@
const Cell = require("../components/Cell"); import Cell from "../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", () => {
@ -42,5 +42,4 @@ describe("Cell functionality", () => {
expect(cell.living).toEqual(state); expect(cell.living).toEqual(state);
}); });
}); });
test.todo("dispatch setLiving should reset live neighbors");
}); });

View file

@ -0,0 +1,24 @@
import GameField from "../components/GameField";
describe("Game Field", () => {
test("smoke test", () => {
expect(new GameField([])).toEqual({ map: {} });
});
});
describe("Game Field seeds living Cells", () => {
const gameField = new GameField([
[0, 1, 0],
[1, 0, 1],
]);
[("0-1", "1-0", "1-2")].forEach((key) => {
test(`${key} should equal living Cell`, () => {
expect(gameField.map[key].living).toEqual(true);
});
});
["0-0", "0-2", "1-1"].forEach((key) => {
test(`${key} should equal undefined`, () => {
expect(gameField.map[key]).toEqual(undefined);
});
});
});