diff --git a/src/components/GameField.js b/src/components/GameField.js index 2978b36..ce43565 100644 --- a/src/components/GameField.js +++ b/src/components/GameField.js @@ -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())) diff --git a/src/test/GameField.test.js b/src/test/GameField.test.js index 6f620b2..71605be 100644 --- a/src/test/GameField.test.js +++ b/src/test/GameField.test.js @@ -6,19 +6,34 @@ describe("Game Field", () => { }); }); -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); +describe("Game Field seeds living Cells with array", () => { + const gameArraySeed = new GameField({ + fieldArray: [ + [0, 1, 0], + [1, 0, 1], + ], + }); + 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); }); }); });