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() {
// print("updating");
grid.update();
if(!grid.update()) running = false;
}
void render([num interp]) {

View File

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

View File

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