Refactor Simulation to be List of dumb cells

Cells are now only an empty struct, they carry no information beyond needing to be re-rendered. All Simulation logic is handled in the Simulation Class.
This commit is contained in:
Unknown 2018-10-15 17:28:09 +02:00
parent 08155b70a5
commit 71f4df85af
2 changed files with 17 additions and 51 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

@ -3,7 +3,6 @@ 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 }
@ -31,18 +30,7 @@ class Simulation {
}
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);
Cell cell = Cell();
return cell;
}
@ -117,12 +105,15 @@ 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;
state ? map.set(x, y, Cell()) : map.set(x, y, null);
}
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) == null ? false : true;
}
Map<int, Cell> update() {
@ -131,10 +122,15 @@ class Simulation {
for (int i = 0; i < map.length; i++) {
math.Point p = map.toCoordinates(i);
Cell el = map[i];
el.update(getSurroundingNeighbors(p.x, p.y, 1));
stateChanges[i] = el;
bool changed = false;
int neighbors = getSurroundingNeighbors(p.x, p.y, 1);
if (el == null && neighbors == 3) {
stateChanges[i] = Cell();
} else if (el != null && neighbors != 2 && neighbors != 3) {
stateChanges[i] = null;
}
}
stateChanges.forEach((_, el) => el.advanceState());
stateChanges.forEach((i, el) => map[i] = el);
stateChanges.length != 0 ? _dirty = true : false;
return stateChanges;
}
@ -147,7 +143,7 @@ class Simulation {
iy >= 0 &&
ix < map.width &&
iy < map.height &&
map.get(ix, iy).state == true &&
map.get(ix, iy) != null &&
!(x == ix && y == iy)) count++;
}
}
@ -169,7 +165,7 @@ class Simulation {
ctx.strokeRect(p.x * brickW, p.y * brickH, brickW, brickH);
}
if (map[i].state == true)
if (map[i] != null)
ctx.setFillColorRgb(155, 155, 255);
else
ctx.setFillColorRgb(0, 0, 0);