diff --git a/lib/src/Cell.dart b/lib/src/Cell.dart index b10142b..4e12f04 100644 --- a/lib/src/Cell.dart +++ b/lib/src/Cell.dart @@ -1,36 +1,6 @@ import 'package:rules_of_living/src/Rule.dart'; class Cell { - bool state; - bool nextState = false; - List surviveRules = new List(); - List birthRules = new List(); - /// 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; - }); - } - } } diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart index e451bf0..4f6a864 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -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 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);