diff --git a/src/components/Cell.js b/src/components/Cell.js index dcb3fb9..918f971 100644 --- a/src/components/Cell.js +++ b/src/components/Cell.js @@ -1,3 +1,5 @@ +const { Stream } = require("../utils"); + class Cell { constructor(living = false, liveNeighbors = 0) { this.living = living; @@ -22,21 +24,6 @@ class Cell { } } -class Stream { - constructor(head, next) { - this.head = head; - this.tail = next; - this.memo = false; - } - get next() { - if (!this.memo) { - this.tail = this.tail(); - this.memo = true; - } - return this.tail; - } -} - class CellStream extends Stream { constructor(head, next) { super(head, next); diff --git a/src/components/GameField.js b/src/components/GameField.js index 7e7e0f3..204e54c 100644 --- a/src/components/GameField.js +++ b/src/components/GameField.js @@ -1,6 +1,7 @@ -import { cellStream } from "./Cell"; +const { cellStream } = require("./Cell"); +const { Stream } = require("../utils"); -export default class GameField { +class GameField { constructor({ fieldArray = [], fieldMap = {} }) { // seed = [ [] ] this.map = {}; @@ -19,8 +20,32 @@ export default class GameField { } } +class FieldStream extends Stream { + constructor(head, next) { + super(head, next); + } + get map() { + return this.head.map; + } +} + +const fieldStream = ({ fieldArray, fieldMap }) => { + return new FieldStream(new GameField({ fieldArray, fieldMap }), function () { + // calculate liveNeighbors for all cells on first next call + + new FieldStream({}, function () { + // call .next on all Cells on second next call + }); + }); +}; + // as a stream -> fieldStream => Stream(GameField, () => Stream(fieldStream.computeNeighbors(), () => Stream(fieldStream.setLiving())) // instantiate table (orientation of major and minor axis dependent on viewport) // const gameFields = new Array(1).fill(new GameField({})); // const container = document.getElementById("game-field"); + +module.exports = { + GameField, + fieldStream, +}; diff --git a/src/index.js b/src/index.js index 3f7d44f..8f21f88 100644 --- a/src/index.js +++ b/src/index.js @@ -1,9 +1,8 @@ import reset from "./styles/reset.css"; import css from "./styles/style.css"; // import Controls from './components/Controls'; -import GameField from "./components/GameField"; +const { GameField } = require("./components/GameField"); (() => console.log("hello world!"))(); - // controls // -- state=idle ? // ---- rewind runs through gameHistory to current state - 1 step diff --git a/src/test/GameField.test.js b/src/test/GameField.test.js index 54db8e7..c9e040b 100644 --- a/src/test/GameField.test.js +++ b/src/test/GameField.test.js @@ -1,33 +1,55 @@ -import GameField from "../components/GameField"; +import { GameField, fieldStream } from "../components/GameField"; + +const fieldArray = [ + [0, 1, 0], + [1, 0, 1], +]; + +const fieldMap = { + "0-0": true, + "0-2": true, + "1-1": 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, - }, - }); + const gameArraySeed = new GameField({ fieldArray }); + const gameMapSeed = new GameField({ fieldMap }); + const streamArraySeed = fieldStream({ fieldArray }); + const streamMapSeed = fieldStream({ fieldMap }); + ["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); }); + + test(`Stream array seed: ${key} should equal living Cell`, () => { + expect(streamArraySeed.map[key].living).toEqual(true); + }); + + test(`Stream map seed: ${key} should equal undefined`, () => { + expect(streamMapSeed.map[key]).toEqual(undefined); + }); }); + ["0-0", "0-2", "1-1"].forEach((key) => { 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); }); + + test(`Stream array seed: ${key} should equal undefined`, () => { + expect(streamArraySeed.map[key]).toEqual(undefined); + }); + + test(`Stream map seed: ${key} should equal living Cell`, () => { + expect(streamMapSeed.map[key].living).toEqual(true); + }); }); }); diff --git a/src/utils/index.js b/src/utils/index.js new file mode 100644 index 0000000..ea07440 --- /dev/null +++ b/src/utils/index.js @@ -0,0 +1,18 @@ +class Stream { + constructor(head, next) { + this.head = head; + this.tail = next; + this.memo = false; + } + get next() { + if (!this.memo) { + this.tail = this.tail(); + this.memo = true; + } + return this.tail; + } +} + +module.exports = { + Stream, +};