From ae264967309e56773f1c5dc391d8894c26c59057 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sun, 21 Oct 2018 11:07:46 +0200 Subject: [PATCH 01/16] Fix grid resize test to check for object identity --- test/simulation_test.dart | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/simulation_test.dart b/test/simulation_test.dart index 7b8bf82..1f99ed1 100644 --- a/test/simulation_test.dart +++ b/test/simulation_test.dart @@ -24,7 +24,7 @@ void main() { test("creates a new underlying grid on resizing", () { var oldMap = sut.map; sut.gridSize = Point(10, 10); - expect(sut.map, isNot(oldMap)); + expect(sut.map, isNot(same(oldMap))); }); }); group("reset", () { From 1f435617dabd4a999d3f97a2841a04c40a7f6038 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sun, 21 Oct 2018 11:38:57 +0200 Subject: [PATCH 02/16] Re-Enable Simulation dirty-flag test In preparation for refactoring, and exposing the dirty flag we re-enable the dirty flag test on resetMap --- test/simulation_test.dart | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/test/simulation_test.dart b/test/simulation_test.dart index 1f99ed1..4dc7f5f 100644 --- a/test/simulation_test.dart +++ b/test/simulation_test.dart @@ -27,14 +27,17 @@ void main() { expect(sut.map, isNot(same(oldMap))); }); }); - group("reset", () { - test("returns a map filled with 'false' ", () { - expect(sut.reset(), allOf(TypeMatcher(), isNot(contains(true)))); + group("resetMap", () { + test("sets the internal map filled with 'false' ", () { + sut.map.set(1, 1, true); + sut.reset(); + expect(sut.map, allOf(TypeMatcher(), isNot(contains(true)))); }); test("sets the simulation to need re-rendering", () { + sut.dirty = false; sut.reset(); expect(sut.dirty, true); - }, skip: "can not find a way to set dirty to true first yet"); + }); }); group("save&load", () { test( From 66c273c783d6bad50e932d52d953d08c83cbec44 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sun, 21 Oct 2018 13:55:15 +0200 Subject: [PATCH 03/16] Make Simulation dirty-flag public --- lib/src/Simulation.dart | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart index 0955c9a..6a5e3bd 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -14,8 +14,7 @@ class Simulation { RuleSet rules = GameOfLife(); - bool _dirty = true; - bool get dirty => _dirty; + bool dirty = true; bool _renderEdges = true; bool get renderEdges => _renderEdges; @@ -38,8 +37,8 @@ class Simulation { Grid reset([Grid map]) { map ??= this.map; - _dirty = true; - map.setAll(0, List.filled(map.length, false)); +// dirty = true; +// map.setAll(0, List.filled(map.length, false)); return map; } @@ -61,7 +60,7 @@ class Simulation { if (sanityCheck > 100 && sanityCheck > i * 3) break; } - _dirty = true; + dirty = true; } void setCellState(int x, int y, bool state) { @@ -91,7 +90,7 @@ class Simulation { void mergeStateChanges(Map stateChanges) { stateChanges.forEach((i, el) => map[i] = el); - if (stateChanges.length != 0) _dirty = true; + if (stateChanges.length != 0) dirty = true; } Map calculateNextState(Grid oldState) { @@ -127,7 +126,7 @@ class Simulation { void render(html.CanvasElement canvas, [num interp]) { // only renders if any cells changed between renders - if (!_dirty) return; + if (!dirty) return; html.CanvasRenderingContext2D ctx = canvas.getContext('2d'); int brickW = (canvas.width ~/ map.width); @@ -146,18 +145,18 @@ class Simulation { ctx.setFillColorRgb(0, 0, 0); ctx.fillRect(p.x * brickW, p.y * brickH, brickW, brickH); } - _dirty = false; + dirty = false; } void set renderEdges(bool on) { _renderEdges = on; - _dirty = true; + dirty = true; } void saveSnapshot() => _snapshot = Grid.from(map); Grid loadSnapshot() { map = Grid.from(_snapshot); - _dirty = true; + dirty = true; return map; } } From e115ed2f48ddcba0c6cc8b62fd97ede442381e3f Mon Sep 17 00:00:00 2001 From: Unknown Date: Sun, 21 Oct 2018 14:00:01 +0200 Subject: [PATCH 04/16] Make clear map function name more concise Renam resetting the grid to clearMap. --- lib/service/simulation_service.dart | 2 +- lib/src/Simulation.dart | 4 ++-- test/simulation_test.dart | 14 +++++++------- 3 files changed, 10 insertions(+), 10 deletions(-) diff --git a/lib/service/simulation_service.dart b/lib/service/simulation_service.dart index 8e0c14f..9d5a95b 100644 --- a/lib/service/simulation_service.dart +++ b/lib/service/simulation_service.dart @@ -18,7 +18,7 @@ class SimulationService { } void reset() { - _sim.reset(); + _sim.clearMap(); } void addRandomPattern() { diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart index 6a5e3bd..92acd1a 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -30,12 +30,12 @@ class Simulation { } Simulation(int w, int h) : this.map = new Grid(w, h) { - this.map = reset(); + this.map = clearMap(); } Simulation.fromGrid(Grid map) : this.map = map; - Grid reset([Grid map]) { + Grid clearMap(Grid map) { map ??= this.map; // dirty = true; // map.setAll(0, List.filled(map.length, false)); diff --git a/test/simulation_test.dart b/test/simulation_test.dart index 4dc7f5f..07a1d29 100644 --- a/test/simulation_test.dart +++ b/test/simulation_test.dart @@ -1,9 +1,9 @@ import 'dart:math'; + import 'package:mockito/mockito.dart'; import 'package:rules_of_living/src/Grid.dart'; -import 'package:test/test.dart'; - import 'package:rules_of_living/src/Simulation.dart'; +import 'package:test/test.dart'; class MockGrid extends Mock implements Grid {} @@ -25,20 +25,20 @@ void main() { var oldMap = sut.map; sut.gridSize = Point(10, 10); expect(sut.map, isNot(same(oldMap))); - }); + }, tags: "nobrowser"); }); group("resetMap", () { test("sets the internal map filled with 'false' ", () { sut.map.set(1, 1, true); - sut.reset(); + sut.clearMap(); expect(sut.map, allOf(TypeMatcher(), isNot(contains(true)))); }); test("sets the simulation to need re-rendering", () { sut.dirty = false; - sut.reset(); + sut.clearMap(); expect(sut.dirty, true); }); - }); + }, tags: "nobrowser"); group("save&load", () { test( "saves a copy of the map which does not change when the actual map changes", @@ -49,5 +49,5 @@ void main() { expect(sut.loadSnapshot(), isNot(equals(snapshot))); }); - }); + }, tags: "nobrowser"); } From 3a6e34945a3bcb6e3ef52c8241265fee5503f989 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sun, 21 Oct 2018 14:00:53 +0200 Subject: [PATCH 05/16] Remove output argument from clearMap function --- lib/src/Simulation.dart | 7 +++---- 1 file changed, 3 insertions(+), 4 deletions(-) diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart index 92acd1a..07cd115 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -35,10 +35,9 @@ class Simulation { Simulation.fromGrid(Grid map) : this.map = map; - Grid clearMap(Grid map) { - map ??= this.map; -// dirty = true; -// map.setAll(0, List.filled(map.length, false)); + Grid clearMap() { + dirty = true; + map.setAll(0, List.filled(map.length, false)); return map; } From cb9da3bc54d2cd9507cb20bd49dd73c9043641a3 Mon Sep 17 00:00:00 2001 From: Unknown Date: Sun, 21 Oct 2018 14:01:34 +0200 Subject: [PATCH 06/16] Remove unnecessary CellPattern enum from Simulation --- lib/src/Simulation.dart | 2 -- 1 file changed, 2 deletions(-) diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart index 07cd115..79cc019 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -6,8 +6,6 @@ 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 } - class Simulation { Grid map; Grid _snapshot; From 0b5b1de4544a46ccfc8ab40ae188a3ae7c403e5c Mon Sep 17 00:00:00 2001 From: Unknown Date: Sun, 21 Oct 2018 14:06:36 +0200 Subject: [PATCH 07/16] Remove Duplicate Import --- lib/src/Simulation.dart | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart index 79cc019..f6d7a66 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -1,6 +1,5 @@ import 'dart:html' as html; import 'dart:math' as math; -import 'dart:math'; import 'package:rules_of_living/src/Grid.dart'; import 'package:rules_of_living/src/rules/GameOfLife.dart'; @@ -20,8 +19,8 @@ class Simulation { int _amount; int _dispersal; - Point get gridSize => Point(map.width, map.height); - void set gridSize(Point value) { + math.Point get gridSize => math.Point(map.width, map.height); + void set gridSize(math.Point value) { if (value.x <= 0 || value.y <= 0) throw ArgumentError("grid size must not be smaller than 1"); map = Grid(value.x, value.y); From 474adb740c1caca0ae9eaeb1eb65a47e2e9c9d18 Mon Sep 17 00:00:00 2001 From: Unknown Date: Mon, 22 Oct 2018 19:17:00 +0200 Subject: [PATCH 08/16] Split up addRandomPattern function --- lib/src/Simulation.dart | 45 +++++++++++++++++++++++++++++------------ 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart index f6d7a66..8144646 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -9,6 +9,10 @@ class Simulation { Grid map; Grid _snapshot; + final int _RANDOM_PATTERN_AMOUNT = 20; + final int _RANDOM_PATTERN_DISPERSAL = 10; + final double _RANDOM_PATTERN_SPREAD_FROM_CENTER = 1 / 3; + RuleSet rules = GameOfLife(); bool dirty = true; @@ -16,7 +20,6 @@ class Simulation { bool _renderEdges = true; bool get renderEdges => _renderEdges; - int _amount; int _dispersal; math.Point get gridSize => math.Point(map.width, map.height); @@ -38,25 +41,41 @@ class Simulation { return map; } - void addRandomPattern({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 = rng.nextInt(map.width ~/ 3) + (map.width ~/ 3); - int cy = rng.nextInt(map.height ~/ 3) + (map.height ~/ 3); + void addRandomPattern( + {int seed, int amount, int dispersal, num spreadFromCenter}) { + math.Random rng = _getRNG(seed ?? DateTime.now().millisecondsSinceEpoch); + amount ??= rng.nextInt(_RANDOM_PATTERN_AMOUNT); + dispersal ??= _RANDOM_PATTERN_DISPERSAL; + spreadFromCenter ??= _RANDOM_PATTERN_SPREAD_FROM_CENTER; int sanityCheck = 0; - for (var i = 0; i < (_amount); i++) { + Map changeSet = {}; + for (var i = 0; i < (amount); i++) { sanityCheck++; - getCellState(cx, cy) + math.Point cell = _getRandomPoint(rng, gridSize, spreadFromCenter); + getCellState(cell.x, cell.y) ? i-- - : setCellState( - cx + rng.nextInt(_dispersal), cy + rng.nextInt(_dispersal), true); + : changeSet[map.toIndex(cell.x, cell.y)] = true; if (sanityCheck > 100 && sanityCheck > i * 3) break; } + mergeStateChanges(changeSet); + } - dirty = true; + math.Random _getRNG(int seed) { + math.Random rng = new math.Random(seed); + return rng; + } + + math.Point _getRandomPoint( + math.Random rng, math.Point size, num spreadFromCenter) { + math.Point absoluteSpread = + math.Point(map.width * spreadFromCenter, map.height * spreadFromCenter); + math.Point center = math.Point(map.width / 2, map.height / 2); + num cx = rng.nextInt(absoluteSpread.x.toInt()) + + (center.x - absoluteSpread.x / 2); + num cy = rng.nextInt(absoluteSpread.y.toInt()) + + (center.y - absoluteSpread.y / 2); + return math.Point(cx.toInt(), cy.toInt()); } void setCellState(int x, int y, bool state) { From e840a3e58022e655f9057bbafc700c24a59d93db Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 23 Oct 2018 14:47:23 +0200 Subject: [PATCH 09/16] Remove unused variable dispersal --- lib/service/simulation_service.dart | 2 +- lib/src/Simulation.dart | 12 +++--------- 2 files changed, 4 insertions(+), 10 deletions(-) diff --git a/lib/service/simulation_service.dart b/lib/service/simulation_service.dart index 9d5a95b..fb2804e 100644 --- a/lib/service/simulation_service.dart +++ b/lib/service/simulation_service.dart @@ -14,7 +14,7 @@ class SimulationService { SimulationService(this._engine, [Simulation sim]) : this._sim = sim ?? Simulation(DEFAULT_GRID_SIZE, DEFAULT_GRID_SIZE) { _engine.simulation = _sim; - _sim.addRandomPattern(amount: 15, dispersal: 5); + _sim.addRandomPattern(amount: 15); } void reset() { diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart index 8144646..5800f70 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -10,7 +10,6 @@ class Simulation { Grid _snapshot; final int _RANDOM_PATTERN_AMOUNT = 20; - final int _RANDOM_PATTERN_DISPERSAL = 10; final double _RANDOM_PATTERN_SPREAD_FROM_CENTER = 1 / 3; RuleSet rules = GameOfLife(); @@ -20,8 +19,6 @@ class Simulation { bool _renderEdges = true; bool get renderEdges => _renderEdges; - int _dispersal; - math.Point get gridSize => math.Point(map.width, map.height); void set gridSize(math.Point value) { if (value.x <= 0 || value.y <= 0) @@ -41,18 +38,16 @@ class Simulation { return map; } - void addRandomPattern( - {int seed, int amount, int dispersal, num spreadFromCenter}) { + void addRandomPattern({int seed, int amount, num spreadFromCenter}) { math.Random rng = _getRNG(seed ?? DateTime.now().millisecondsSinceEpoch); amount ??= rng.nextInt(_RANDOM_PATTERN_AMOUNT); - dispersal ??= _RANDOM_PATTERN_DISPERSAL; spreadFromCenter ??= _RANDOM_PATTERN_SPREAD_FROM_CENTER; int sanityCheck = 0; Map changeSet = {}; for (var i = 0; i < (amount); i++) { sanityCheck++; - math.Point cell = _getRandomPoint(rng, gridSize, spreadFromCenter); + math.Point cell = _getRandomPoint(rng, spreadFromCenter); getCellState(cell.x, cell.y) ? i-- : changeSet[map.toIndex(cell.x, cell.y)] = true; @@ -66,8 +61,7 @@ class Simulation { return rng; } - math.Point _getRandomPoint( - math.Random rng, math.Point size, num spreadFromCenter) { + math.Point _getRandomPoint(math.Random rng, num spreadFromCenter) { math.Point absoluteSpread = math.Point(map.width * spreadFromCenter, map.height * spreadFromCenter); math.Point center = math.Point(map.width / 2, map.height / 2); From c8e2417ab87d00d246b2521a41c7dc90b88870dd Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 23 Oct 2018 20:21:02 +0200 Subject: [PATCH 10/16] Remove duplicate functions to set Grid Cells --- lib/src/Simulation.dart | 22 +++------------------- test/simulation_test.dart | 5 +++++ 2 files changed, 8 insertions(+), 19 deletions(-) diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart index 5800f70..b00ede5 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -48,7 +48,7 @@ class Simulation { for (var i = 0; i < (amount); i++) { sanityCheck++; math.Point cell = _getRandomPoint(rng, spreadFromCenter); - getCellState(cell.x, cell.y) + map.get(cell.x, cell.y) ? i-- : changeSet[map.toIndex(cell.x, cell.y)] = true; if (sanityCheck > 100 && sanityCheck > i * 3) break; @@ -72,24 +72,8 @@ class Simulation { return math.Point(cx.toInt(), cy.toInt()); } - void setCellState(int x, int y, bool 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 null; - - return map.get(x, y); - } - 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.get(x, y) == null ? map.set(x, y, true) : map.set(x, y, false); } Map update() { @@ -126,7 +110,7 @@ class Simulation { iy >= 0 && ix < map.width && iy < map.height && - getCellState(ix, iy) == true && + map.get(ix, iy) == true && !(x == ix && y == iy)) count++; } } diff --git a/test/simulation_test.dart b/test/simulation_test.dart index 07a1d29..a7073db 100644 --- a/test/simulation_test.dart +++ b/test/simulation_test.dart @@ -50,4 +50,9 @@ void main() { expect(sut.loadSnapshot(), isNot(equals(snapshot))); }); }, tags: "nobrowser"); + group("toggleCellState", () { + test("throws RangeError if outside the map bounds", () { + expect(() => sut.toggleCellState(10, 9), throwsRangeError); + }, tags: const ["nobrowser"]); + }); } From b1d96efc842636f04decfd91e6201f57986978b0 Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 23 Oct 2018 20:30:20 +0200 Subject: [PATCH 11/16] Fix toggleCell Function in Simulation --- lib/src/Simulation.dart | 2 +- test/simulation_test.dart | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart index b00ede5..ddd6086 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -73,7 +73,7 @@ class Simulation { } void toggleCellState(int x, int y) { - map.get(x, y) == null ? map.set(x, y, true) : map.set(x, y, false); + map.get(x, y) == false ? map.set(x, y, true) : map.set(x, y, false); } Map update() { diff --git a/test/simulation_test.dart b/test/simulation_test.dart index a7073db..e2c6610 100644 --- a/test/simulation_test.dart +++ b/test/simulation_test.dart @@ -54,5 +54,15 @@ void main() { test("throws RangeError if outside the map bounds", () { expect(() => sut.toggleCellState(10, 9), throwsRangeError); }, tags: const ["nobrowser"]); + test("sets the cell to false if currently true", () { + sut.map.set(1, 1, true); + sut.toggleCellState(1, 1); + expect(sut.map.get(1, 1), false); + }, tags: const ["nobrowser"]); + test("sets the cell to true if currently false", () { + sut.map.set(1, 1, false); + sut.toggleCellState(1, 1); + expect(sut.map.get(1, 1), true); + }, tags: const ["nobrowser"]); }); } From 5116aff8a45cf2df49a2856e19e165c4c5a08bf8 Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 23 Oct 2018 20:31:44 +0200 Subject: [PATCH 12/16] Refactor toggleCellState function --- 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 ddd6086..f3efd39 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -73,7 +73,7 @@ class Simulation { } void toggleCellState(int x, int y) { - map.get(x, y) == false ? map.set(x, y, true) : map.set(x, y, false); + map.set(x, y, !map.get(x, y)); } Map update() { From 52b440351ddf64a031ed4781eab2a59cd9c6d948 Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 23 Oct 2018 20:41:54 +0200 Subject: [PATCH 13/16] Add getCell and setCell method to Simulation --- lib/src/Simulation.dart | 4 ++++ test/simulation_test.dart | 32 ++++++++++++++++++++++++++------ 2 files changed, 30 insertions(+), 6 deletions(-) diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart index f3efd39..1c9461d 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -72,6 +72,10 @@ class Simulation { return math.Point(cx.toInt(), cy.toInt()); } + bool getCell(int x, int y) => map.get(x, y); + + void setCell(int x, int y, bool value) => map.set(x, y, value); + void toggleCellState(int x, int y) { map.set(x, y, !map.get(x, y)); } diff --git a/test/simulation_test.dart b/test/simulation_test.dart index e2c6610..66a027b 100644 --- a/test/simulation_test.dart +++ b/test/simulation_test.dart @@ -7,6 +7,8 @@ import 'package:test/test.dart'; class MockGrid extends Mock implements Grid {} +// TODO: reinstate when decoupling rendering from Simulation @Tags(const ["nobrowser"]) + void main() { Simulation sut; setUp(() { @@ -25,7 +27,7 @@ void main() { var oldMap = sut.map; sut.gridSize = Point(10, 10); expect(sut.map, isNot(same(oldMap))); - }, tags: "nobrowser"); + }); }); group("resetMap", () { test("sets the internal map filled with 'false' ", () { @@ -38,7 +40,7 @@ void main() { sut.clearMap(); expect(sut.dirty, true); }); - }, tags: "nobrowser"); + }); group("save&load", () { test( "saves a copy of the map which does not change when the actual map changes", @@ -49,20 +51,38 @@ void main() { expect(sut.loadSnapshot(), isNot(equals(snapshot))); }); - }, tags: "nobrowser"); + }); + group("getCellState", () { + test("throws RangeError if outside map bounds", () { + expect(() => sut.getCell(9, 10), throwsRangeError); + }); + test("returns cell value", () { + sut.map.set(1, 1, true); + expect(sut.getCell(1, 1), true); + }); + }); + group("setCellState", () { + test("throws RangeError if outside map bounds", () { + expect(() => sut.setCell(9, 10, true), throwsRangeError); + }); + test("sets cell value", () { + sut.setCell(1, 1, true); + expect(sut.getCell(1, 1), true); + }); + }); group("toggleCellState", () { test("throws RangeError if outside the map bounds", () { expect(() => sut.toggleCellState(10, 9), throwsRangeError); - }, tags: const ["nobrowser"]); + }); test("sets the cell to false if currently true", () { sut.map.set(1, 1, true); sut.toggleCellState(1, 1); expect(sut.map.get(1, 1), false); - }, tags: const ["nobrowser"]); + }); test("sets the cell to true if currently false", () { sut.map.set(1, 1, false); sut.toggleCellState(1, 1); expect(sut.map.get(1, 1), true); - }, tags: const ["nobrowser"]); + }); }); } From 2e7fd7c3d308041fe1535b769b8cd762341e7e2e Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 23 Oct 2018 20:41:54 +0200 Subject: [PATCH 14/16] Revert "Add getCell and setCell method to Simulation" This reverts commit 52b440351ddf64a031ed4781eab2a59cd9c6d948. --- lib/src/Simulation.dart | 4 ---- test/simulation_test.dart | 32 ++++++-------------------------- 2 files changed, 6 insertions(+), 30 deletions(-) diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart index 1c9461d..f3efd39 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -72,10 +72,6 @@ class Simulation { return math.Point(cx.toInt(), cy.toInt()); } - bool getCell(int x, int y) => map.get(x, y); - - void setCell(int x, int y, bool value) => map.set(x, y, value); - void toggleCellState(int x, int y) { map.set(x, y, !map.get(x, y)); } diff --git a/test/simulation_test.dart b/test/simulation_test.dart index 66a027b..e2c6610 100644 --- a/test/simulation_test.dart +++ b/test/simulation_test.dart @@ -7,8 +7,6 @@ import 'package:test/test.dart'; class MockGrid extends Mock implements Grid {} -// TODO: reinstate when decoupling rendering from Simulation @Tags(const ["nobrowser"]) - void main() { Simulation sut; setUp(() { @@ -27,7 +25,7 @@ void main() { var oldMap = sut.map; sut.gridSize = Point(10, 10); expect(sut.map, isNot(same(oldMap))); - }); + }, tags: "nobrowser"); }); group("resetMap", () { test("sets the internal map filled with 'false' ", () { @@ -40,7 +38,7 @@ void main() { sut.clearMap(); expect(sut.dirty, true); }); - }); + }, tags: "nobrowser"); group("save&load", () { test( "saves a copy of the map which does not change when the actual map changes", @@ -51,38 +49,20 @@ void main() { expect(sut.loadSnapshot(), isNot(equals(snapshot))); }); - }); - group("getCellState", () { - test("throws RangeError if outside map bounds", () { - expect(() => sut.getCell(9, 10), throwsRangeError); - }); - test("returns cell value", () { - sut.map.set(1, 1, true); - expect(sut.getCell(1, 1), true); - }); - }); - group("setCellState", () { - test("throws RangeError if outside map bounds", () { - expect(() => sut.setCell(9, 10, true), throwsRangeError); - }); - test("sets cell value", () { - sut.setCell(1, 1, true); - expect(sut.getCell(1, 1), true); - }); - }); + }, tags: "nobrowser"); group("toggleCellState", () { test("throws RangeError if outside the map bounds", () { expect(() => sut.toggleCellState(10, 9), throwsRangeError); - }); + }, tags: const ["nobrowser"]); test("sets the cell to false if currently true", () { sut.map.set(1, 1, true); sut.toggleCellState(1, 1); expect(sut.map.get(1, 1), false); - }); + }, tags: const ["nobrowser"]); test("sets the cell to true if currently false", () { sut.map.set(1, 1, false); sut.toggleCellState(1, 1); expect(sut.map.get(1, 1), true); - }); + }, tags: const ["nobrowser"]); }); } From e40dddde528ebea8ef677fc8cf62751df5f55023 Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 23 Oct 2018 21:01:27 +0200 Subject: [PATCH 15/16] Shorten toggleCell Function Name --- lib/src/Simulation.dart | 2 +- test/simulation_test.dart | 6 +++--- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart index f3efd39..dfcf2bd 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -72,7 +72,7 @@ class Simulation { return math.Point(cx.toInt(), cy.toInt()); } - void toggleCellState(int x, int y) { + void toggleCell(int x, int y) { map.set(x, y, !map.get(x, y)); } diff --git a/test/simulation_test.dart b/test/simulation_test.dart index e2c6610..4c87191 100644 --- a/test/simulation_test.dart +++ b/test/simulation_test.dart @@ -52,16 +52,16 @@ void main() { }, tags: "nobrowser"); group("toggleCellState", () { test("throws RangeError if outside the map bounds", () { - expect(() => sut.toggleCellState(10, 9), throwsRangeError); + expect(() => sut.toggleCell(10, 9), throwsRangeError); }, tags: const ["nobrowser"]); test("sets the cell to false if currently true", () { sut.map.set(1, 1, true); - sut.toggleCellState(1, 1); + sut.toggleCell(1, 1); expect(sut.map.get(1, 1), false); }, tags: const ["nobrowser"]); test("sets the cell to true if currently false", () { sut.map.set(1, 1, false); - sut.toggleCellState(1, 1); + sut.toggleCell(1, 1); expect(sut.map.get(1, 1), true); }, tags: const ["nobrowser"]); }); From bdb698394a5ae39c87d4b84b8325498bcb2d2e19 Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 23 Oct 2018 21:02:42 +0200 Subject: [PATCH 16/16] Re-Order Simulation Class --- lib/src/Simulation.dart | 68 ++++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 34 deletions(-) diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart index dfcf2bd..8341944 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -38,40 +38,6 @@ class Simulation { return map; } - void addRandomPattern({int seed, int amount, num spreadFromCenter}) { - math.Random rng = _getRNG(seed ?? DateTime.now().millisecondsSinceEpoch); - amount ??= rng.nextInt(_RANDOM_PATTERN_AMOUNT); - spreadFromCenter ??= _RANDOM_PATTERN_SPREAD_FROM_CENTER; - - int sanityCheck = 0; - Map changeSet = {}; - for (var i = 0; i < (amount); i++) { - sanityCheck++; - math.Point cell = _getRandomPoint(rng, spreadFromCenter); - map.get(cell.x, cell.y) - ? i-- - : changeSet[map.toIndex(cell.x, cell.y)] = true; - if (sanityCheck > 100 && sanityCheck > i * 3) break; - } - mergeStateChanges(changeSet); - } - - math.Random _getRNG(int seed) { - math.Random rng = new math.Random(seed); - return rng; - } - - math.Point _getRandomPoint(math.Random rng, num spreadFromCenter) { - math.Point absoluteSpread = - math.Point(map.width * spreadFromCenter, map.height * spreadFromCenter); - math.Point center = math.Point(map.width / 2, map.height / 2); - num cx = rng.nextInt(absoluteSpread.x.toInt()) + - (center.x - absoluteSpread.x / 2); - num cy = rng.nextInt(absoluteSpread.y.toInt()) + - (center.y - absoluteSpread.y / 2); - return math.Point(cx.toInt(), cy.toInt()); - } - void toggleCell(int x, int y) { map.set(x, y, !map.get(x, y)); } @@ -117,6 +83,40 @@ class Simulation { return count; } + void addRandomPattern({int seed, int amount, num spreadFromCenter}) { + math.Random rng = _getRNG(seed ?? DateTime.now().millisecondsSinceEpoch); + amount ??= rng.nextInt(_RANDOM_PATTERN_AMOUNT); + spreadFromCenter ??= _RANDOM_PATTERN_SPREAD_FROM_CENTER; + + int sanityCheck = 0; + Map changeSet = {}; + for (var i = 0; i < (amount); i++) { + sanityCheck++; + math.Point cell = _getRandomPoint(rng, spreadFromCenter); + map.get(cell.x, cell.y) + ? i-- + : changeSet[map.toIndex(cell.x, cell.y)] = true; + if (sanityCheck > 100 && sanityCheck > i * 3) break; + } + mergeStateChanges(changeSet); + } + + math.Random _getRNG(int seed) { + math.Random rng = new math.Random(seed); + return rng; + } + + math.Point _getRandomPoint(math.Random rng, num spreadFromCenter) { + math.Point absoluteSpread = + math.Point(map.width * spreadFromCenter, map.height * spreadFromCenter); + math.Point center = math.Point(map.width / 2, map.height / 2); + num cx = rng.nextInt(absoluteSpread.x.toInt()) + + (center.x - absoluteSpread.x / 2); + num cy = rng.nextInt(absoluteSpread.y.toInt()) + + (center.y - absoluteSpread.y / 2); + return math.Point(cx.toInt(), cy.toInt()); + } + void render(html.CanvasElement canvas, [num interp]) { // only renders if any cells changed between renders if (!dirty) return;