X-Git-Url: https://git.xandkar.net/?a=blobdiff_plain;f=life%2F006%2Flife.ts;h=7b6ad3b140764c0ff7bc92975f278ab99766ed7e;hb=537249fc4bd177f195cc2bf52a0ebaa0bcf234ee;hp=e99d3fd28f3c36e000ee693b10eea6560deb7c76;hpb=54042df4de905f34081846cd36e6b56473971b37;p=cellular-automata.git diff --git a/life/006/life.ts b/life/006/life.ts index e99d3fd..7b6ad3b 100644 --- a/life/006/life.ts +++ b/life/006/life.ts @@ -4,7 +4,7 @@ type GridLocation = {r: number, k: number}; interface GridInterface { get : (location: GridLocation) => A; - map : (f : (location: GridLocation) => A) => Grid; + map : (f : (location: GridLocation) => A) => void; moore_neighbors: (origin : GridLocation) => Array; print : (toString : (A: A) => string) => void; }; @@ -53,17 +53,8 @@ class Grid implements GridInterface { }; map(f : (location: GridLocation) => A) { - const cells = []; - for (let r = 0; r < this.rows; r++) { - cells[r] = []; - for (let k = 0; k < this.columns; k++) { - const location = {r: r, k: k}; - cells[r][k] = f(location); - } - }; - const init = ({r, k}) => cells[r][k]; - const grid = new Grid({rows: this.rows, columns: this.columns, init: init}); - return grid + this.cells = + this.cells.map((row, r) => row.map((_, k) => f({r: r, k: k}))); }; private print_border(): void { @@ -160,7 +151,7 @@ namespace Life { }; next() : void { - const grid = this.grid.map( + this.grid.map( (location) => { const neighbor_locations = this.grid.moore_neighbors(location); const neighbor_states = neighbor_locations.map((l) => this.grid.get(l)); @@ -170,7 +161,6 @@ namespace Life { return state_1 } ); - this.grid = grid }; print() : void {