Merge branch '43-add-statechange-data-structure' into 'master'

Resolve "Add StateChange data structure"

Closes #43

See merge request marty.oehme/cellular-automata!13
This commit is contained in:
Marty 2018-10-16 16:23:29 +00:00
commit 07f176be3e
3 changed files with 39 additions and 69 deletions

View File

@ -1,36 +1,6 @@
import 'package:rules_of_living/src/Rule.dart';
class Cell {
bool state;
bool nextState = false;
List<Rule> surviveRules = new List<Rule>();
List<Rule> birthRules = new List<Rule>();
/// For determining if render updates are necessary in [Grid].render() function
bool dirty = false;
Cell([bool state = false]) : this.state = state;
// 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) {
if (state == true) {
surviveRules.forEach((Rule rule) {
if (rule.evaluate(neighbors) == true) this.nextState = true;
});
} else {
birthRules.forEach((Rule rule) {
if (rule.evaluate(neighbors) == true) this.nextState = true;
});
}
}
}

View File

@ -101,7 +101,8 @@ class Engine {
/// directly, since it is automatically taken care of by the processing function.
/// If simulation should be advanced manually one time, prefer using step().
void update() {
if (!_grid.update()) running = false;
// TODO: create hasUpdated/hasAdvanced method in simulation to abstract actual updating away
if (_grid.update().length == 0) running = false;
}
/// Advances Logic One Update

View File

@ -1,14 +1,12 @@
import 'dart:html' as html;
import 'dart:math' as math;
import 'package:rules_of_living/src/Cell.dart';
import 'package:rules_of_living/src/Grid.dart';
import 'package:rules_of_living/src/Rule.dart';
enum CellPattern { SpaceShip, Blinker }
class Simulation {
final Grid<Cell> map;
final Grid<bool> map;
bool _dirty = true;
bool _renderEdges = true;
@ -24,30 +22,12 @@ class Simulation {
int get h => map.height;
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");
}
Cell _getGOLCell([bool defaultState = false]) {
Cell cell = Cell(defaultState);
Rule threeTrue = new Rule((int n) {
if (n == 3) return true;
return false;
});
Rule twoTrue = new Rule((int n) {
if (n == 2) return true;
return false;
});
cell.surviveRules.add(twoTrue);
cell.surviveRules.add(threeTrue);
cell.birthRules.add(threeTrue);
return cell;
reset();
print("Grid Created");
}
void reset() {
map.setAll(0, List.filled(map.length, _getGOLCell()));
map.setAll(0, List.filled(map.length, false));
if (_startingSeed != null)
addPattern(
pattern: _pattern,
@ -117,27 +97,46 @@ class Simulation {
}
void setCellState(int x, int y, bool state) {
if (y < map.height && x < map.width) map.get(x, y).state = state;
if (y >= map.height || x >= map.width) return null;
state ? map.set(x, y, true) : map.set(x, y, false);
}
bool getCellState(int x, int y) {
if (y < map.height && x < map.width) return map.get(x, y).state;
return null;
if (y >= map.height || x >= map.width) return null;
return map.get(x, y);
}
bool update() {
bool stateChanges = false;
void toggleCellState(int x, int y) {
if (y >= map.height || x >= map.width) return null;
getCellState(x, y) == null
? setCellState(x, y, true)
: setCellState(x, y, false);
}
Map<int, bool> update() {
Map<int, bool> stateChanges = calculateNextState(map);
stateChanges.forEach((i, el) => map[i] = el);
stateChanges.length != 0 ? _dirty = true : false;
return stateChanges;
}
Map<int, bool> calculateNextState(Grid<bool> oldState) {
Map<int, bool> stateChanges = Map();
for (int i = 0; i < map.length; i++) {
math.Point p = map.toCoordinates(i);
map[i].update(getSurroundingNeighbors(p.x, p.y, 1));
bool cell = map[i];
int neighbors = getSurroundingNeighbors(p.x, p.y, 1);
if (cell == false && neighbors == 3) {
stateChanges[i] = true;
} else if (cell == true && neighbors != 2 && neighbors != 3) {
stateChanges[i] = false;
}
}
// TODO when implementing changeSet we can remove this second loop and add to changeSet in the first
map.forEach((Cell el) {
if (el.state != el.nextState) stateChanges = true;
el.advanceState();
});
stateChanges ? _dirty = true : false;
return stateChanges;
}
@ -149,7 +148,7 @@ class Simulation {
iy >= 0 &&
ix < map.width &&
iy < map.height &&
map.get(ix, iy).state == true &&
getCellState(ix, iy) == true &&
!(x == ix && y == iy)) count++;
}
}
@ -171,7 +170,7 @@ class Simulation {
ctx.strokeRect(p.x * brickW, p.y * brickH, brickW, brickH);
}
if (map[i].state == true)
if (map[i] == true)
ctx.setFillColorRgb(155, 155, 255);
else
ctx.setFillColorRgb(0, 0, 0);