diff --git a/lib/components/controls_component.dart b/lib/components/controls_component.dart
index 3f31741..538bc27 100644
--- a/lib/components/controls_component.dart
+++ b/lib/components/controls_component.dart
@@ -29,8 +29,12 @@ class ControlsComponent {
engine.step();
}
- void onResetClicked() {
- sim.reset();
+ void onSaveClicked() {
+ sim.save();
+ }
+
+ void onLoadClicked() {
+ sim.load();
}
void onRandomClicked() {
@@ -39,6 +43,6 @@ class ControlsComponent {
}
void onClearClicked() {
- sim.clear();
+ sim.reset();
}
}
diff --git a/lib/components/controls_component.html b/lib/components/controls_component.html
index 42e79f7..764ca37 100644
--- a/lib/components/controls_component.html
+++ b/lib/components/controls_component.html
@@ -1,5 +1,6 @@
-
+
+
diff --git a/lib/service/simulation_service.dart b/lib/service/simulation_service.dart
index 396290b..8e0c14f 100644
--- a/lib/service/simulation_service.dart
+++ b/lib/service/simulation_service.dart
@@ -9,9 +9,10 @@ class SimulationService {
static final int DEFAULT_GRID_SIZE = 50;
final EngineService _engine;
- final Simulation _sim = Simulation(DEFAULT_GRID_SIZE, DEFAULT_GRID_SIZE);
+ final Simulation _sim;
- SimulationService(this._engine) {
+ SimulationService(this._engine, [Simulation sim])
+ : this._sim = sim ?? Simulation(DEFAULT_GRID_SIZE, DEFAULT_GRID_SIZE) {
_engine.simulation = _sim;
_sim.addRandomPattern(amount: 15, dispersal: 5);
}
@@ -24,10 +25,6 @@ class SimulationService {
_sim.addRandomPattern();
}
- void clear() {
- _sim.reset();
- }
-
Point get gridSize => _sim.gridSize;
void set gridSize(Point size) {
_sim.gridSize = size;
@@ -40,4 +37,7 @@ class SimulationService {
void toggleGrid() {
_sim.renderEdges = !_sim.renderEdges;
}
+
+ void save() => _sim.saveSnapshot();
+ void load() => _sim.loadSnapshot();
}
diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart
index 54c7efa..0955c9a 100644
--- a/lib/src/Simulation.dart
+++ b/lib/src/Simulation.dart
@@ -10,6 +10,8 @@ enum CellPattern { SpaceShip, Blinker }
class Simulation {
Grid map;
+ Grid _snapshot;
+
RuleSet rules = GameOfLife();
bool _dirty = true;
@@ -151,4 +153,11 @@ class Simulation {
_renderEdges = on;
_dirty = true;
}
+
+ void saveSnapshot() => _snapshot = Grid.from(map);
+ Grid loadSnapshot() {
+ map = Grid.from(_snapshot);
+ _dirty = true;
+ return map;
+ }
}
diff --git a/test/service/simulation_service_test.dart b/test/service/simulation_service_test.dart
new file mode 100644
index 0000000..3816099
--- /dev/null
+++ b/test/service/simulation_service_test.dart
@@ -0,0 +1,23 @@
+import 'package:rules_of_living/service/engine_service.dart';
+import 'package:rules_of_living/service/simulation_service.dart';
+import 'package:rules_of_living/src/Simulation.dart';
+import 'package:test/test.dart';
+import 'package:mockito/mockito.dart';
+
+class MockSimulation extends Mock implements Simulation {}
+
+class MockEngineService extends Mock implements EngineService {}
+
+void main() {
+ SimulationService sut;
+ MockSimulation mockSim = MockSimulation();
+ setUp(() => sut = SimulationService(MockEngineService(), mockSim));
+ test("calling save calls through to Simulation.saveSnapshot", () {
+ sut.save();
+ verify(mockSim.saveSnapshot());
+ });
+ test("calling load calls through to Simulation.loadSnapshot", () {
+ sut.load();
+ verify(mockSim.loadSnapshot());
+ });
+}
diff --git a/test/simulation_test.dart b/test/simulation_test.dart
index b8f5db6..7b8bf82 100644
--- a/test/simulation_test.dart
+++ b/test/simulation_test.dart
@@ -5,6 +5,8 @@ import 'package:test/test.dart';
import 'package:rules_of_living/src/Simulation.dart';
+class MockGrid extends Mock implements Grid {}
+
void main() {
Simulation sut;
setUp(() {
@@ -34,4 +36,15 @@ void main() {
expect(sut.dirty, true);
}, skip: "can not find a way to set dirty to true first yet");
});
+ group("save&load", () {
+ test(
+ "saves a copy of the map which does not change when the actual map changes",
+ () {
+ sut.saveSnapshot();
+ sut.mergeStateChanges({1: true, 2: true});
+ var snapshot = Grid.from(sut.map);
+
+ expect(sut.loadSnapshot(), isNot(equals(snapshot)));
+ });
+ });
}