stub fieldStream

This commit is contained in:
Sorrel Bri 2020-05-24 13:23:26 -07:00
parent 530acc0aeb
commit bf239fe656
5 changed files with 84 additions and 33 deletions

View file

@ -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);

View file

@ -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,
};

View file

@ -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

View file

@ -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);
});
});
});

18
src/utils/index.js Normal file
View file

@ -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,
};