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/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) { 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) {