Fix Simulation Neighbor Propagation

This commit is contained in:
Marty Oehme 2018-08-30 12:03:23 +02:00
parent b95d39d2b4
commit a324d52df5
1 changed files with 17 additions and 16 deletions

View File

@ -23,11 +23,14 @@ class Simulation {
int get w => map.width;
int get h => map.height;
Simulation(int w, int h) : this.map = new Grid.fill(w, h, _getGOLCell()) {
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");
}
static Cell _getGOLCell([bool defaultState = false]) {
Cell _getGOLCell([bool defaultState = false]) {
Cell cell = Cell(defaultState);
Rule threeTrue = new Rule((int n) {
if (n == 3) return true;
@ -37,8 +40,9 @@ class Simulation {
if (n == 2) return true;
return false;
});
cell.surviveRules..add(twoTrue)..add(threeTrue);
cell.birthRules.add(twoTrue);
cell.surviveRules.add(twoTrue);
cell.surviveRules.add(threeTrue);
cell.birthRules.add(threeTrue);
return cell;
}
@ -133,21 +137,20 @@ class Simulation {
if (el.state != el.nextState) stateChanges = true;
el.advanceState();
});
stateChanges ? _dirty = true : false;
return stateChanges;
}
int getSurroundingNeighbors(int x, int y, int range) {
int count = 0;
for (int iy = y - range; iy <= y + range; iy++) {
for (int ix = x - range; ix <= x + range; ix++) {
if (ix > 0 &&
iy > 0 &&
iy < map.height &&
for (int ix = -range + x; ix <= range + x; ix++) {
for (int iy = -range + y; iy <= range + y; iy++) {
if (ix >= 0 &&
iy >= 0 &&
ix < map.width &&
map.get(x, y).state == true &&
!(x == ix && y == iy)) {
count++;
}
iy < map.height &&
map.get(ix, iy).state == true &&
!(x == ix && y == iy)) count++;
}
}
return count;
@ -161,8 +164,7 @@ class Simulation {
int brickW = (canvas.width ~/ map.width);
int brickH = (canvas.height ~/ map.height);
ctx.clearRect(0, 0, canvas.width, canvas.height);
for (int i; i < map.length; i++) {
for (int i = 0; i < map.length; i++) {
math.Point p = map.toCoordinates(i);
if (_renderEdges) {
ctx.setStrokeColorRgb(100, 100, 100);
@ -175,7 +177,6 @@ class Simulation {
ctx.setFillColorRgb(0, 0, 0);
ctx.fillRect(p.x * brickW, p.y * brickH, brickW, brickH);
}
_dirty = false;
}