From 27d4879b1bca3f11cbeb16efae3b295b6a9df9ea Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 17 Oct 2018 20:56:30 +0200 Subject: [PATCH 01/14] Extract RuleSet Class from Simulation --- lib/src/RuleSet.dart | 27 ++++++++++++++++++++++++++ lib/src/Simulation.dart | 43 ++++++----------------------------------- 2 files changed, 33 insertions(+), 37 deletions(-) create mode 100644 lib/src/RuleSet.dart diff --git a/lib/src/RuleSet.dart b/lib/src/RuleSet.dart new file mode 100644 index 0000000..edfc5a9 --- /dev/null +++ b/lib/src/RuleSet.dart @@ -0,0 +1,27 @@ +import 'dart:math'; + +import 'package:collection/collection.dart'; + +abstract class RuleSet { + int checkRange; + + bool checkSurvival(int neighbors); + bool checkBirth(int neighbors); +} + +class Pattern extends DelegatingList { + final String _name; + Pattern(String name, List base) + : _name = name, + super(base); + + String get name => _name; +} + +class GameOfLife implements RuleSet { + int checkRange = 1; + + bool checkSurvival(int neighbors) => + neighbors == 2 || neighbors == 3 ? true : false; + bool checkBirth(int neighbors) => neighbors == 3 ? true : false; +} diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart index 02c3c3a..05db54b 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -2,11 +2,13 @@ import 'dart:html' as html; import 'dart:math' as math; import 'package:rules_of_living/src/Grid.dart'; +import 'package:rules_of_living/src/RuleSet.dart'; enum CellPattern { SpaceShip, Blinker } class Simulation { final Grid map; + RuleSet rules = GameOfLife(); bool _dirty = true; bool _renderEdges = true; @@ -28,14 +30,6 @@ class Simulation { void reset() { map.setAll(0, List.filled(map.length, false)); - if (_startingSeed != null) - addPattern( - pattern: _pattern, - dispersal: _dispersal, - amount: _amount, - seed: _startingSeed, - x: _x, - y: _y); _dirty = true; } @@ -56,31 +50,6 @@ class Simulation { int cx = x ?? rng.nextInt(map.width ~/ 3) + (map.width ~/ 3); int cy = y ?? rng.nextInt(map.height ~/ 3) + (map.height ~/ 3); switch (pattern) { - // Two blocks, offset - // ## - // ## - case CellPattern.Blinker: - setCellState(cx, cy, true); - setCellState(cx + 1, cy, true); - setCellState(cx, cy + 1, true); - setCellState(cx + 1, cy + 1, true); - - setCellState(cx + 2, cy + 2, true); - setCellState(cx + 3, cy + 2, true); - setCellState(cx + 2, cy + 3, true); - setCellState(cx + 3, cy + 3, true); - break; - // A 'gliding' Spaceship - // # - // # - // ### - case CellPattern.SpaceShip: - setCellState(1 + cx, 0 + cy, true); - setCellState(2 + cx, 1 + cy, true); - setCellState(2 + cx, 2 + cy, true); - setCellState(1 + cx, 2 + cy, true); - setCellState(0 + cx, 2 + cy, true); - break; default: int sanityCheck = 0; for (var i = 0; i < (_amount); i++) { @@ -130,17 +99,17 @@ class Simulation { for (int i = 0; i < map.length; i++) { math.Point p = map.toCoordinates(i); bool cell = map[i]; - int neighbors = getSurroundingNeighbors(p.x, p.y, 1); - if (cell == false && neighbors == 3) { + int neighbors = getNeighbors(p.x, p.y, 1); + if (cell == false && rules.checkBirth(neighbors) == true) { stateChanges[i] = true; - } else if (cell == true && neighbors != 2 && neighbors != 3) { + } else if (cell == true && rules.checkSurvival(neighbors) == false) { stateChanges[i] = false; } } return stateChanges; } - int getSurroundingNeighbors(int x, int y, int range) { + int getNeighbors(int x, int y, int range) { int count = 0; for (int ix = -range + x; ix <= range + x; ix++) { for (int iy = -range + y; iy <= range + y; iy++) { From 9b2b5f3e55060bd500e1af6eb9faff7492d1272e Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 17 Oct 2018 21:00:14 +0200 Subject: [PATCH 02/14] Remove unnecessary Simulation variables --- lib/src/Simulation.dart | 6 ------ 1 file changed, 6 deletions(-) diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart index 05db54b..bfdfa2c 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -14,11 +14,8 @@ class Simulation { bool _renderEdges = true; int _startingSeed; - int _x; - int _y; int _amount; int _dispersal; - CellPattern _pattern; int get w => map.width; int get h => map.height; @@ -42,11 +39,8 @@ class Simulation { int seed}) { _startingSeed = seed ?? DateTime.now().millisecondsSinceEpoch; math.Random rng = new math.Random(_startingSeed); - _x = x; - _y = y; _amount = amount ?? rng.nextInt(20); _dispersal = dispersal ?? 10; - _pattern = pattern; int cx = x ?? rng.nextInt(map.width ~/ 3) + (map.width ~/ 3); int cy = y ?? rng.nextInt(map.height ~/ 3) + (map.height ~/ 3); switch (pattern) { From e6e82f78f24ab7c434e16f4ea66b5438352baced Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 17 Oct 2018 21:07:48 +0200 Subject: [PATCH 03/14] Remove unnecessary Switch Case in Simulation --- lib/src/Simulation.dart | 22 ++++++++++------------ 1 file changed, 10 insertions(+), 12 deletions(-) diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart index bfdfa2c..8342d7c 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -43,19 +43,17 @@ class Simulation { _dispersal = dispersal ?? 10; int cx = x ?? rng.nextInt(map.width ~/ 3) + (map.width ~/ 3); int cy = y ?? rng.nextInt(map.height ~/ 3) + (map.height ~/ 3); - switch (pattern) { - default: - int sanityCheck = 0; - for (var i = 0; i < (_amount); i++) { - sanityCheck++; - getCellState(cx, cy) - ? i-- - : setCellState(cx + rng.nextInt(_dispersal), - cy + rng.nextInt(_dispersal), true); - if (sanityCheck > 100 && sanityCheck > i * 3) break; - } - break; + + int sanityCheck = 0; + for (var i = 0; i < (_amount); i++) { + sanityCheck++; + getCellState(cx, cy) + ? i-- + : setCellState( + cx + rng.nextInt(_dispersal), cy + rng.nextInt(_dispersal), true); + if (sanityCheck > 100 && sanityCheck > i * 3) break; } + _dirty = true; } From 6d7120650fdb2ab2757cb8052bc18cd481cdf3db Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 17 Oct 2018 21:08:55 +0200 Subject: [PATCH 04/14] Add Special Patterns to RuleSet --- lib/src/RuleSet.dart | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/lib/src/RuleSet.dart b/lib/src/RuleSet.dart index edfc5a9..f115699 100644 --- a/lib/src/RuleSet.dart +++ b/lib/src/RuleSet.dart @@ -4,6 +4,7 @@ import 'package:collection/collection.dart'; abstract class RuleSet { int checkRange; + List patterns; bool checkSurvival(int neighbors); bool checkBirth(int neighbors); @@ -20,6 +21,32 @@ class Pattern extends DelegatingList { class GameOfLife implements RuleSet { int checkRange = 1; + List patterns = [ + // Two blocks, offset + // ## + // ## + Pattern("Blinker", [ + Point(0, 0), + Point(1, 0), + Point(0, 1), + Point(1, 1), + Point(2, 2), + Point(3, 2), + Point(2, 3), + Point(3, 3) + ]), + // A 'gliding' Spaceship + // # + // # + // ### + Pattern("SpaceShip", [ + Point(1, 0), + Point(2, 1), + Point(2, 2), + Point(1, 2), + Point(0, 2), + ]) + ]; bool checkSurvival(int neighbors) => neighbors == 2 || neighbors == 3 ? true : false; From bac65ef1163c6479675e6c2fbf5d1d0ce2e7d194 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 18 Oct 2018 09:57:08 +0200 Subject: [PATCH 05/14] Remove unnecessary pattern parameters --- lib/src/Engine.dart | 16 ++-------------- lib/src/Simulation.dart | 15 ++++----------- 2 files changed, 6 insertions(+), 25 deletions(-) diff --git a/lib/src/Engine.dart b/lib/src/Engine.dart index 36ab89a..73ab7ea 100644 --- a/lib/src/Engine.dart +++ b/lib/src/Engine.dart @@ -124,20 +124,8 @@ class Engine { if (canvas != null) _grid.render(canvas, interp); } - void addPattern( - {CellPattern pattern, - int x, - int y, - int amount, - int dispersal, - int seed}) { - _grid.addPattern( - pattern: pattern, - x: x, - y: y, - amount: amount, - dispersal: dispersal, - seed: seed); + void addPattern({int amount, int dispersal}) { + _grid.addPattern(amount: amount, dispersal: dispersal); } void toggleEdgeRendering() { diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart index 8342d7c..b0bbb46 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -13,7 +13,6 @@ class Simulation { bool _dirty = true; bool _renderEdges = true; - int _startingSeed; int _amount; int _dispersal; @@ -30,19 +29,13 @@ class Simulation { _dirty = true; } - void addPattern( - {CellPattern pattern, - int x, - int y, - int amount, - int dispersal, - int seed}) { - _startingSeed = seed ?? DateTime.now().millisecondsSinceEpoch; + void addPattern({int amount, int dispersal}) { + int _startingSeed = DateTime.now().millisecondsSinceEpoch; math.Random rng = new math.Random(_startingSeed); _amount = amount ?? rng.nextInt(20); _dispersal = dispersal ?? 10; - int cx = x ?? rng.nextInt(map.width ~/ 3) + (map.width ~/ 3); - int cy = y ?? rng.nextInt(map.height ~/ 3) + (map.height ~/ 3); + int cx = rng.nextInt(map.width ~/ 3) + (map.width ~/ 3); + int cy = rng.nextInt(map.height ~/ 3) + (map.height ~/ 3); int sanityCheck = 0; for (var i = 0; i < (_amount); i++) { From e16085153aa26670d0d8c7e3dab1791352341a12 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 18 Oct 2018 09:59:26 +0200 Subject: [PATCH 06/14] Rename Simulation in Engine Object --- lib/src/Engine.dart | 26 ++++++++++++++------------ 1 file changed, 14 insertions(+), 12 deletions(-) diff --git a/lib/src/Engine.dart b/lib/src/Engine.dart index 73ab7ea..e00b50f 100644 --- a/lib/src/Engine.dart +++ b/lib/src/Engine.dart @@ -32,11 +32,11 @@ class Engine { /// Grid Size /// /// Number of cells on x coordinate and y coordinate. Can be set individually. - Point get gridSize => Point(_grid.w, _grid.h); + Point get gridSize => Point(_simulation.w, _simulation.h); void set gridSize(Point value) { if (value.x <= 0 || value.y <= 0) throw ArgumentError("grid size must not be smaller than 1"); - _grid = Simulation(value.x, value.y); + _simulation = Simulation(value.x, value.y); } num _updateLag = 0.0; @@ -48,14 +48,14 @@ class Engine { /// be used if no canvas was defined at engine creation and it should be /// rendered later. html.CanvasElement canvas; - Simulation _grid; + Simulation _simulation; bool running = false; Engine([x = 100, y = 100, this.canvas]) { - _grid = Simulation(x, y); + _simulation = Simulation(x, y); _elapsed.start(); - _grid.addPattern(amount: 15, dispersal: 5); + _simulation.addPattern(amount: 15, dispersal: 5); html.window.animationFrame.then(animFrame); } @@ -65,12 +65,12 @@ class Engine { } void reset() { - _grid.reset(); + _simulation.reset(); running = false; } void clear() { - _grid = new Simulation(gridSize.x, gridSize.y); + _simulation = new Simulation(gridSize.x, gridSize.y); running = false; } @@ -102,7 +102,7 @@ class Engine { /// If simulation should be advanced manually one time, prefer using step(). void update() { // TODO: create hasUpdated/hasAdvanced method in simulation to abstract actual updating away - if (_grid.update().length == 0) running = false; + if (_simulation.update().length == 0) running = false; } /// Advances Logic One Update @@ -112,7 +112,7 @@ class Engine { /// (though this should usually not pose a problem). void step() { running = false; - _grid.update(); + _simulation.update(); } /// Renders the Current Simulation State @@ -121,14 +121,16 @@ class Engine { /// the internal engine processing. Does not do anything if no canvas is /// defined. void render([num interp]) { - if (canvas != null) _grid.render(canvas, interp); + if (canvas == null) return; + + _simulation.render(canvas, interp); } void addPattern({int amount, int dispersal}) { - _grid.addPattern(amount: amount, dispersal: dispersal); + _simulation.addPattern(amount: amount, dispersal: dispersal); } void toggleEdgeRendering() { - _grid.renderEdges = !_grid.renderEdges; + _simulation.renderEdges = !_simulation.renderEdges; } } From de1aa46743709fe45057a8972cfe9ac1f829b22c Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 18 Oct 2018 10:58:06 +0200 Subject: [PATCH 07/14] Separate Simulation calculating updates and merging Simulation updates were one step of calculation and merging the calculations into the map in one function. Separating the two allows checking for a new update without affecting the grid, allows passing the last Update around and allows custom changes to the grid by passing changes to the merge function that were not derived from the update function. --- lib/src/Engine.dart | 5 ++++- lib/src/Simulation.dart | 8 +++++--- 2 files changed, 9 insertions(+), 4 deletions(-) diff --git a/lib/src/Engine.dart b/lib/src/Engine.dart index e00b50f..59bcc66 100644 --- a/lib/src/Engine.dart +++ b/lib/src/Engine.dart @@ -102,7 +102,10 @@ class Engine { /// If simulation should be advanced manually one time, prefer using step(). void update() { // TODO: create hasUpdated/hasAdvanced method in simulation to abstract actual updating away - if (_simulation.update().length == 0) running = false; + Map simulationUpdate = _simulation.update(); + _simulation.mergeStateChanges(simulationUpdate); + + if (simulationUpdate.length == 0) running = false; } /// Advances Logic One Update diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart index b0bbb46..8cc04ef 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -72,12 +72,14 @@ class Simulation { Map update() { Map stateChanges = calculateNextState(map); - - stateChanges.forEach((i, el) => map[i] = el); - stateChanges.length != 0 ? _dirty = true : false; return stateChanges; } + void mergeStateChanges(Map stateChanges) { + stateChanges.forEach((i, el) => map[i] = el); + if (stateChanges.length != 0) _dirty = true; + } + Map calculateNextState(Grid oldState) { Map stateChanges = Map(); From c3244b085e152278c34739fcda864ca6de430e50 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 18 Oct 2018 11:04:23 +0200 Subject: [PATCH 08/14] Fix single steps not updating simulation --- lib/src/Engine.dart | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/lib/src/Engine.dart b/lib/src/Engine.dart index 59bcc66..e5190cf 100644 --- a/lib/src/Engine.dart +++ b/lib/src/Engine.dart @@ -101,7 +101,6 @@ 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() { - // TODO: create hasUpdated/hasAdvanced method in simulation to abstract actual updating away Map simulationUpdate = _simulation.update(); _simulation.mergeStateChanges(simulationUpdate); @@ -114,8 +113,8 @@ class Engine { /// simulation. Does not automatically re-render the new state /// (though this should usually not pose a problem). void step() { + update(); running = false; - _simulation.update(); } /// Renders the Current Simulation State From 4f63947ab9a999253119a8943651197863d821c9 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 18 Oct 2018 11:19:04 +0200 Subject: [PATCH 09/14] Delete unused Cell and Rule Classes They were used under the old system of every gridspace being its own cell data structure with its own rules to observe. Replaced by the RuleSet class. Cell has vanished in favor of simple boolean values filling the grid. --- lib/src/Cell.dart | 6 ------ lib/src/Rule.dart | 5 ----- 2 files changed, 11 deletions(-) delete mode 100644 lib/src/Cell.dart delete mode 100644 lib/src/Rule.dart diff --git a/lib/src/Cell.dart b/lib/src/Cell.dart deleted file mode 100644 index 4e12f04..0000000 --- a/lib/src/Cell.dart +++ /dev/null @@ -1,6 +0,0 @@ -import 'package:rules_of_living/src/Rule.dart'; - -class Cell { - /// For determining if render updates are necessary in [Grid].render() function - bool dirty = false; -} diff --git a/lib/src/Rule.dart b/lib/src/Rule.dart deleted file mode 100644 index 723b4d0..0000000 --- a/lib/src/Rule.dart +++ /dev/null @@ -1,5 +0,0 @@ -class Rule { - final Function evaluate; - - Rule(this.evaluate); -} From fbdf114fed22f9fe1fe8585d4234a546f25abba1 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 18 Oct 2018 11:21:09 +0200 Subject: [PATCH 10/14] Move RuleSet to its own directory --- lib/src/Simulation.dart | 2 +- lib/src/{ => rules}/RuleSet.dart | 0 2 files changed, 1 insertion(+), 1 deletion(-) rename lib/src/{ => rules}/RuleSet.dart (100%) diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart index 8cc04ef..2f167a3 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -2,7 +2,7 @@ import 'dart:html' as html; import 'dart:math' as math; import 'package:rules_of_living/src/Grid.dart'; -import 'package:rules_of_living/src/RuleSet.dart'; +import 'package:rules_of_living/src/rules/RuleSet.dart'; enum CellPattern { SpaceShip, Blinker } diff --git a/lib/src/RuleSet.dart b/lib/src/rules/RuleSet.dart similarity index 100% rename from lib/src/RuleSet.dart rename to lib/src/rules/RuleSet.dart From 0aa3df30b4e0f3cc261247c9c97a7a6275a89c64 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 18 Oct 2018 11:27:44 +0200 Subject: [PATCH 11/14] Extract CellPattern and GameOfLife into own files --- lib/src/Simulation.dart | 1 + lib/src/rules/CellPattern.dart | 10 +++++++ lib/src/rules/GameOfLife.dart | 38 ++++++++++++++++++++++++++ lib/src/rules/RuleSet.dart | 49 ++-------------------------------- 4 files changed, 51 insertions(+), 47 deletions(-) create mode 100644 lib/src/rules/CellPattern.dart create mode 100644 lib/src/rules/GameOfLife.dart diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart index 2f167a3..a3f99c5 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -2,6 +2,7 @@ import 'dart:html' as html; import 'dart:math' as math; import 'package:rules_of_living/src/Grid.dart'; +import 'package:rules_of_living/src/rules/GameOfLife.dart'; import 'package:rules_of_living/src/rules/RuleSet.dart'; enum CellPattern { SpaceShip, Blinker } diff --git a/lib/src/rules/CellPattern.dart b/lib/src/rules/CellPattern.dart new file mode 100644 index 0000000..9798948 --- /dev/null +++ b/lib/src/rules/CellPattern.dart @@ -0,0 +1,10 @@ +import 'package:collection/collection.dart'; + +class CellPattern extends DelegatingList { + final String _name; + CellPattern(String name, List base) + : _name = name, + super(base); + + String get name => _name; +} diff --git a/lib/src/rules/GameOfLife.dart b/lib/src/rules/GameOfLife.dart new file mode 100644 index 0000000..f812233 --- /dev/null +++ b/lib/src/rules/GameOfLife.dart @@ -0,0 +1,38 @@ +import 'dart:math'; + +import 'package:rules_of_living/src/rules/RuleSet.dart'; +import 'package:rules_of_living/src/rules/CellPattern.dart'; + +class GameOfLife implements RuleSet { + int checkRange = 1; + List patterns = [ + // Two blocks, offset + // ## + // ## + CellPattern("Blinker", [ + Point(0, 0), + Point(1, 0), + Point(0, 1), + Point(1, 1), + Point(2, 2), + Point(3, 2), + Point(2, 3), + Point(3, 3) + ]), + // A 'gliding' Spaceship + // # + // # + // ### + CellPattern("SpaceShip", [ + Point(1, 0), + Point(2, 1), + Point(2, 2), + Point(1, 2), + Point(0, 2), + ]) + ]; + + bool checkSurvival(int neighbors) => + neighbors == 2 || neighbors == 3 ? true : false; + bool checkBirth(int neighbors) => neighbors == 3 ? true : false; +} diff --git a/lib/src/rules/RuleSet.dart b/lib/src/rules/RuleSet.dart index f115699..ad2bb70 100644 --- a/lib/src/rules/RuleSet.dart +++ b/lib/src/rules/RuleSet.dart @@ -1,54 +1,9 @@ -import 'dart:math'; - -import 'package:collection/collection.dart'; +import 'package:rules_of_living/src/rules/CellPattern.dart'; abstract class RuleSet { int checkRange; - List patterns; + List patterns; bool checkSurvival(int neighbors); bool checkBirth(int neighbors); } - -class Pattern extends DelegatingList { - final String _name; - Pattern(String name, List base) - : _name = name, - super(base); - - String get name => _name; -} - -class GameOfLife implements RuleSet { - int checkRange = 1; - List patterns = [ - // Two blocks, offset - // ## - // ## - Pattern("Blinker", [ - Point(0, 0), - Point(1, 0), - Point(0, 1), - Point(1, 1), - Point(2, 2), - Point(3, 2), - Point(2, 3), - Point(3, 3) - ]), - // A 'gliding' Spaceship - // # - // # - // ### - Pattern("SpaceShip", [ - Point(1, 0), - Point(2, 1), - Point(2, 2), - Point(1, 2), - Point(0, 2), - ]) - ]; - - bool checkSurvival(int neighbors) => - neighbors == 2 || neighbors == 3 ? true : false; - bool checkBirth(int neighbors) => neighbors == 3 ? true : false; -} From e13962f3718ca307022e8ca195a35e2e121bfa57 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 18 Oct 2018 11:29:55 +0200 Subject: [PATCH 12/14] Shorten RuleSet variable for their checked range Range is self-explanatory and not as confusing as checkRange. --- lib/src/rules/GameOfLife.dart | 2 +- lib/src/rules/RuleSet.dart | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/src/rules/GameOfLife.dart b/lib/src/rules/GameOfLife.dart index f812233..edb2dde 100644 --- a/lib/src/rules/GameOfLife.dart +++ b/lib/src/rules/GameOfLife.dart @@ -4,7 +4,7 @@ import 'package:rules_of_living/src/rules/RuleSet.dart'; import 'package:rules_of_living/src/rules/CellPattern.dart'; class GameOfLife implements RuleSet { - int checkRange = 1; + int range = 1; List patterns = [ // Two blocks, offset // ## diff --git a/lib/src/rules/RuleSet.dart b/lib/src/rules/RuleSet.dart index ad2bb70..03aeb36 100644 --- a/lib/src/rules/RuleSet.dart +++ b/lib/src/rules/RuleSet.dart @@ -1,7 +1,7 @@ import 'package:rules_of_living/src/rules/CellPattern.dart'; abstract class RuleSet { - int checkRange; + int range; List patterns; bool checkSurvival(int neighbors); From f1399064a20c04feb89cc9a416bac3ee129c7d5e Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 18 Oct 2018 11:30:14 +0200 Subject: [PATCH 13/14] Fix Simulation using RuleSet range for neighbor checks --- lib/src/Simulation.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart index a3f99c5..90969ce 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -87,7 +87,7 @@ class Simulation { for (int i = 0; i < map.length; i++) { math.Point p = map.toCoordinates(i); bool cell = map[i]; - int neighbors = getNeighbors(p.x, p.y, 1); + int neighbors = getNeighbors(p.x, p.y, rules.range); if (cell == false && rules.checkBirth(neighbors) == true) { stateChanges[i] = true; } else if (cell == true && rules.checkSurvival(neighbors) == false) { From 4f92c69a82794b97365d5186eadb437a8a9bbaf7 Mon Sep 17 00:00:00 2001 From: Unknown Date: Thu, 18 Oct 2018 11:32:00 +0200 Subject: [PATCH 14/14] Rename Simulation function adding random patterns Rename from addPattern to addRandomPattern to more clearly signify its purpose. --- lib/src/Engine.dart | 4 ++-- lib/src/Simulation.dart | 2 +- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/src/Engine.dart b/lib/src/Engine.dart index e5190cf..3653353 100644 --- a/lib/src/Engine.dart +++ b/lib/src/Engine.dart @@ -55,7 +55,7 @@ class Engine { _simulation = Simulation(x, y); _elapsed.start(); - _simulation.addPattern(amount: 15, dispersal: 5); + _simulation.addRandomPattern(amount: 15, dispersal: 5); html.window.animationFrame.then(animFrame); } @@ -129,7 +129,7 @@ class Engine { } void addPattern({int amount, int dispersal}) { - _simulation.addPattern(amount: amount, dispersal: dispersal); + _simulation.addRandomPattern(amount: amount, dispersal: dispersal); } void toggleEdgeRendering() { diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart index 90969ce..42df2a4 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -30,7 +30,7 @@ class Simulation { _dirty = true; } - void addPattern({int amount, int dispersal}) { + void addRandomPattern({int amount, int dispersal}) { int _startingSeed = DateTime.now().millisecondsSinceEpoch; math.Random rng = new math.Random(_startingSeed); _amount = amount ?? rng.nextInt(20);