Merge branch '6-stop-simulation-if-all-cells-are-empty' into 'master'

Resolve "Stop Simulation if all cells are empty"

Closes #6

See merge request webdevexp/rules-of-living!1
This commit is contained in:
Marty 2018-07-08 17:34:39 +00:00
commit 49b036ffe9
3 changed files with 13 additions and 5 deletions

View file

@ -56,7 +56,7 @@ class App {
void update() { void update() {
// print("updating"); // print("updating");
grid.update(); if(!grid.update()) running = false;
} }
void render([num interp]) { void render([num interp]) {

View file

@ -11,11 +11,15 @@ class Cell {
Cell([bool state = false]) : this.state = state; 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.state = this.nextState;
this.nextState = false; this.nextState = false;
this.dirty = true; this.dirty = true;
return this.state;
} }
void update(int neighbors) { void update(int neighbors) {

View file

@ -143,7 +143,8 @@ class Grid {
return grid; return grid;
} }
void update() { bool update() {
bool stateChanges = false;
for (int y = 0; y < h; y++) { for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) { for (int x = 0; x < w; x++) {
// DEFAULTS TO CONWAY GAME OF LIFE RANGE OF ONE // DEFAULTS TO CONWAY GAME OF LIFE RANGE OF ONE
@ -152,11 +153,14 @@ class Grid {
} }
for (int y = 0; y < h; y++) { for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) { for (int x = 0; x < w; x++) {
// DEFAULTS TO CONWAY GAME OF LIFE RANGE OF ONE Cell c = map[y][x];
map[y][x].advanceState(); if(c.state != c.nextState) stateChanges = true;
c.advanceState();
if (!_dirty && map[y][x].dirty) _dirty = true; if (!_dirty && map[y][x].dirty) _dirty = true;
} }
} }
return stateChanges;
} }
int getSurroundingNeighbors(int x, int y, int range) { int getSurroundingNeighbors(int x, int y, int range) {