refactor GameField constructor for mapped seed Cells

This commit is contained in:
Sorrel Bri 2020-05-23 23:37:39 -07:00
parent f68cd3a62a
commit ea42beed13
2 changed files with 32 additions and 13 deletions

View file

@ -1,18 +1,22 @@
import Cell from "./Cell";
export default class GameField {
constructor(seed) {
constructor({ fieldArray = [], fieldMap = {} }) {
// seed = [ [] ]
this.map = {};
seed.forEach((subArray, majorIndex) =>
fieldArray.forEach((subArray, majorIndex) =>
subArray.forEach((value, minorIndex) => {
if (value > 0) {
this.map[`${majorIndex}-${minorIndex}`] = new Cell(true);
}
})
);
for (let key in fieldMap) {
this.map[key] = new Cell(true);
}
// instead of implementing multiple GameFields, clear irrelevant keys and expand Game Field as needed
// discrete Field expansion should only happen in View (to keep view fields centered)
}
}
// as a stream -> fieldStream => Stream(GameField, () => Stream(GameField.computeNeighbors(), () => Stream(GameField.setLiving()))
// as a stream -> fieldStream => Stream(GameField, () => Stream(fieldStream.computeNeighbors(), () => Stream(fieldStream.setLiving()))

View file

@ -6,19 +6,34 @@ describe("Game Field", () => {
});
});
describe("Game Field seeds living Cells", () => {
const gameField = new GameField([
describe("Game Field seeds living Cells with array", () => {
const gameArraySeed = new GameField({
fieldArray: [
[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);
],
});
const gameMapSeed = new GameField({
fieldMap: {
"0-0": true,
"0-2": true,
"1-1": true,
},
});
["0-1", "1-0", "1-2"].forEach((key) => {
test(`Array seed: ${key} should equal living Cell`, () => {
expect(gameArraySeed.map[key].living).toEqual(true);
});
test(`Map seed: ${key} should equal undefined`, () => {
expect(gameMapSeed.map[key]).toEqual(undefined);
});
});
["0-0", "0-2", "1-1"].forEach((key) => {
test(`${key} should equal undefined`, () => {
expect(gameField.map[key]).toEqual(undefined);
test(`Array seed: ${key} should equal undefined`, () => {
expect(gameArraySeed.map[key]).toEqual(undefined);
});
test(`Map seed: ${key} should equal living Cell`, () => {
expect(gameMapSeed.map[key].living).toEqual(true);
});
});
});