From a324d52df5a489ae82d8b21772f2d51581fed7be Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Thu, 30 Aug 2018 12:03:23 +0200 Subject: [PATCH] Fix Simulation Neighbor Propagation --- lib/src/Simulation.dart | 33 +++++++++++++++++---------------- 1 file changed, 17 insertions(+), 16 deletions(-) diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart index 7f221ba..ca7bb71 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -23,11 +23,14 @@ class Simulation { int get w => map.width; int get h => map.height; - Simulation(int w, int h) : this.map = new Grid.fill(w, h, _getGOLCell()) { + Simulation(int w, int h) : this.map = new Grid(w, h) { + for (int i = 0; i < map.length; i++) { + map[i] = _getGOLCell(); + } print("Grid creation finished"); } - static Cell _getGOLCell([bool defaultState = false]) { + Cell _getGOLCell([bool defaultState = false]) { Cell cell = Cell(defaultState); Rule threeTrue = new Rule((int n) { if (n == 3) return true; @@ -37,8 +40,9 @@ class Simulation { if (n == 2) return true; return false; }); - cell.surviveRules..add(twoTrue)..add(threeTrue); - cell.birthRules.add(twoTrue); + cell.surviveRules.add(twoTrue); + cell.surviveRules.add(threeTrue); + cell.birthRules.add(threeTrue); return cell; } @@ -133,21 +137,20 @@ class Simulation { if (el.state != el.nextState) stateChanges = true; el.advanceState(); }); + stateChanges ? _dirty = true : false; return stateChanges; } int getSurroundingNeighbors(int x, int y, int range) { int count = 0; - for (int iy = y - range; iy <= y + range; iy++) { - for (int ix = x - range; ix <= x + range; ix++) { - if (ix > 0 && - iy > 0 && - iy < map.height && + for (int ix = -range + x; ix <= range + x; ix++) { + for (int iy = -range + y; iy <= range + y; iy++) { + if (ix >= 0 && + iy >= 0 && ix < map.width && - map.get(x, y).state == true && - !(x == ix && y == iy)) { - count++; - } + iy < map.height && + map.get(ix, iy).state == true && + !(x == ix && y == iy)) count++; } } return count; @@ -161,8 +164,7 @@ class Simulation { int brickW = (canvas.width ~/ map.width); int brickH = (canvas.height ~/ map.height); ctx.clearRect(0, 0, canvas.width, canvas.height); - - for (int i; i < map.length; i++) { + for (int i = 0; i < map.length; i++) { math.Point p = map.toCoordinates(i); if (_renderEdges) { ctx.setStrokeColorRgb(100, 100, 100); @@ -175,7 +177,6 @@ class Simulation { ctx.setFillColorRgb(0, 0, 0); ctx.fillRect(p.x * brickW, p.y * brickH, brickW, brickH); } - _dirty = false; }