From 9fb67d019437a489561a6c956fd6060c8b6ce9b9 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Sun, 8 Jul 2018 19:08:40 +0200 Subject: [PATCH 1/2] Add return of new cell state to advanceState Function --- lib/src/Cell.dart | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/lib/src/Cell.dart b/lib/src/Cell.dart index f472280..b10142b 100644 --- a/lib/src/Cell.dart +++ b/lib/src/Cell.dart @@ -11,11 +11,15 @@ class Cell { Cell([bool state = false]) : this.state = state; - void advanceState() { + // Sets the actual state to what it should be next update + // Returns the newly assumed actual state of the cell. + bool advanceState() { this.state = this.nextState; this.nextState = false; this.dirty = true; + + return this.state; } void update(int neighbors) { From 1146c7d265838f2691b33fa5b00517c49e4ff3c8 Mon Sep 17 00:00:00 2001 From: Marty Oehme Date: Sun, 8 Jul 2018 19:32:43 +0200 Subject: [PATCH 2/2] Add Check for State Changes During Grid Update --- lib/src/App.dart | 2 +- lib/src/Grid.dart | 10 +++++++--- 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/lib/src/App.dart b/lib/src/App.dart index 8ab933a..140e9a3 100644 --- a/lib/src/App.dart +++ b/lib/src/App.dart @@ -56,7 +56,7 @@ class App { void update() { // print("updating"); - grid.update(); + if(!grid.update()) running = false; } void render([num interp]) { diff --git a/lib/src/Grid.dart b/lib/src/Grid.dart index a25a50e..f47e5bf 100644 --- a/lib/src/Grid.dart +++ b/lib/src/Grid.dart @@ -143,7 +143,8 @@ class Grid { return grid; } - void update() { + bool update() { + bool stateChanges = false; for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { // DEFAULTS TO CONWAY GAME OF LIFE RANGE OF ONE @@ -152,11 +153,14 @@ class Grid { } for (int y = 0; y < h; y++) { for (int x = 0; x < w; x++) { - // DEFAULTS TO CONWAY GAME OF LIFE RANGE OF ONE - map[y][x].advanceState(); + Cell c = map[y][x]; + if(c.state != c.nextState) stateChanges = true; + c.advanceState(); + if (!_dirty && map[y][x].dirty) _dirty = true; } } + return stateChanges; } int getSurroundingNeighbors(int x, int y, int range) {