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"]); + }); }