Compare commits
No commits in common. "master" and "44-change-grid-data-structure-to-list" have entirely different histories.
master
...
44-change-
23 changed files with 372 additions and 503 deletions
|
@ -1,6 +1,6 @@
|
||||||
analyzer:
|
analyzer:
|
||||||
exclude: [build/**]
|
exclude: [build/**]
|
||||||
|
strong-mode: true
|
||||||
errors:
|
errors:
|
||||||
uri_has_not_been_generated: ignore
|
uri_has_not_been_generated: ignore
|
||||||
plugins:
|
plugins:
|
||||||
|
|
|
@ -4,8 +4,9 @@ import 'package:rules_of_living/components/configuration_component.dart';
|
||||||
import 'package:rules_of_living/components/controls_component.dart';
|
import 'package:rules_of_living/components/controls_component.dart';
|
||||||
import 'package:rules_of_living/components/header_component.dart';
|
import 'package:rules_of_living/components/header_component.dart';
|
||||||
import 'package:rules_of_living/components/simulation_component.dart';
|
import 'package:rules_of_living/components/simulation_component.dart';
|
||||||
|
import 'package:rules_of_living/service/configuration_service.dart';
|
||||||
|
import 'package:rules_of_living/service/control_service.dart';
|
||||||
import 'package:rules_of_living/service/engine_service.dart';
|
import 'package:rules_of_living/service/engine_service.dart';
|
||||||
import 'package:rules_of_living/service/simulation_service.dart';
|
|
||||||
|
|
||||||
@Component(
|
@Component(
|
||||||
selector: 'my-app',
|
selector: 'my-app',
|
||||||
|
@ -23,7 +24,8 @@ import 'package:rules_of_living/service/simulation_service.dart';
|
||||||
providers: [
|
providers: [
|
||||||
materialProviders,
|
materialProviders,
|
||||||
ClassProvider(EngineService),
|
ClassProvider(EngineService),
|
||||||
ClassProvider(SimulationService)
|
ClassProvider(ConfigurationService),
|
||||||
|
ClassProvider(ControlService)
|
||||||
],
|
],
|
||||||
styleUrls: const [
|
styleUrls: const [
|
||||||
'package:angular_components/app_layout/layout.scss.css',
|
'package:angular_components/app_layout/layout.scss.css',
|
||||||
|
|
|
@ -1,5 +1,3 @@
|
||||||
import 'dart:math';
|
|
||||||
|
|
||||||
import 'package:angular/angular.dart';
|
import 'package:angular/angular.dart';
|
||||||
import 'package:angular_components/material_button/material_button.dart';
|
import 'package:angular_components/material_button/material_button.dart';
|
||||||
import 'package:angular_components/material_icon/material_icon.dart';
|
import 'package:angular_components/material_icon/material_icon.dart';
|
||||||
|
@ -7,8 +5,7 @@ import 'package:angular_components/material_input/material_input.dart';
|
||||||
import 'package:angular_components/material_input/material_number_accessor.dart';
|
import 'package:angular_components/material_input/material_number_accessor.dart';
|
||||||
import 'package:angular_components/material_slider/material_slider.dart';
|
import 'package:angular_components/material_slider/material_slider.dart';
|
||||||
import 'package:angular_components/material_tooltip/material_tooltip.dart';
|
import 'package:angular_components/material_tooltip/material_tooltip.dart';
|
||||||
import 'package:rules_of_living/service/engine_service.dart';
|
import 'package:rules_of_living/service/configuration_service.dart';
|
||||||
import 'package:rules_of_living/service/simulation_service.dart';
|
|
||||||
|
|
||||||
@Component(
|
@Component(
|
||||||
selector: "configuration",
|
selector: "configuration",
|
||||||
|
@ -26,29 +23,28 @@ import 'package:rules_of_living/service/simulation_service.dart';
|
||||||
NgModel
|
NgModel
|
||||||
])
|
])
|
||||||
class ConfigurationComponent {
|
class ConfigurationComponent {
|
||||||
final EngineService engine;
|
final ConfigurationService config;
|
||||||
final SimulationService sim;
|
|
||||||
|
|
||||||
int get width => sim.gridSize.x;
|
int get width => config.gridSize.x;
|
||||||
void set width(num value) {
|
void set width(num value) {
|
||||||
if (value == null || value <= 0) return;
|
if (value == null || value <= 0) return;
|
||||||
sim.gridSize = Point(value, sim.gridSize.y);
|
config.setGridSize(x: value.toInt());
|
||||||
}
|
}
|
||||||
|
|
||||||
int get height => sim.gridSize.y;
|
int get height => config.gridSize.y;
|
||||||
void set height(num value) {
|
void set height(num value) {
|
||||||
if (value == null || value <= 0) return;
|
if (value == null || value <= 0) return;
|
||||||
sim.gridSize = Point(sim.gridSize.x, value);
|
config.setGridSize(y: value.toInt());
|
||||||
}
|
}
|
||||||
|
|
||||||
int get simSpeed => engine.simSpeed;
|
int get simSpeed => config.simSpeed;
|
||||||
void set simSpeed(int value) => engine.simSpeed = value;
|
void set simSpeed(int value) => config.simSpeed = value;
|
||||||
|
|
||||||
String get speedSliderTooltip => "Simulation Speed: $simSpeed";
|
String get speedSliderTooltip => "Simulation Speed: $simSpeed";
|
||||||
|
|
||||||
ConfigurationComponent(this.engine, this.sim);
|
ConfigurationComponent(this.config);
|
||||||
|
|
||||||
void onEdgesClicked() {
|
void onEdgesClicked() {
|
||||||
sim.toggleGrid();
|
config.toggleGrid();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,7 +1,6 @@
|
||||||
import 'package:angular/angular.dart';
|
import 'package:angular/angular.dart';
|
||||||
import 'package:angular_components/angular_components.dart';
|
import 'package:angular_components/angular_components.dart';
|
||||||
import 'package:rules_of_living/service/engine_service.dart';
|
import 'package:rules_of_living/service/control_service.dart';
|
||||||
import 'package:rules_of_living/service/simulation_service.dart';
|
|
||||||
|
|
||||||
@Component(
|
@Component(
|
||||||
selector: 'sim-controls',
|
selector: 'sim-controls',
|
||||||
|
@ -16,33 +15,27 @@ import 'package:rules_of_living/service/simulation_service.dart';
|
||||||
styleUrls: const ["controls_component.css"],
|
styleUrls: const ["controls_component.css"],
|
||||||
)
|
)
|
||||||
class ControlsComponent {
|
class ControlsComponent {
|
||||||
final EngineService engine;
|
final ControlService ctrl;
|
||||||
final SimulationService sim;
|
|
||||||
|
|
||||||
ControlsComponent(this.engine, this.sim);
|
ControlsComponent(this.ctrl);
|
||||||
|
|
||||||
void onStartClicked() {
|
void onStartClicked() {
|
||||||
engine.toggleRunning();
|
ctrl.toggleRunning();
|
||||||
}
|
}
|
||||||
|
|
||||||
void onStepClicked() {
|
void onStepClicked() {
|
||||||
engine.step();
|
ctrl.step();
|
||||||
}
|
}
|
||||||
|
|
||||||
void onSaveClicked() {
|
void onResetClicked() {
|
||||||
sim.save();
|
ctrl.reset();
|
||||||
}
|
|
||||||
|
|
||||||
void onLoadClicked() {
|
|
||||||
sim.load();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void onRandomClicked() {
|
void onRandomClicked() {
|
||||||
sim.addRandomPattern();
|
ctrl.addRandomPattern();
|
||||||
engine.stop();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void onClearClicked() {
|
void onClearClicked() {
|
||||||
sim.reset();
|
ctrl.clear();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
<div id="controls">
|
<div id="controls">
|
||||||
<material-button id="save" (click)="onSaveClicked()"><material-icon icon="save" baseline></material-icon></material-button>
|
<material-button id="reset" (click)="onResetClicked()"><material-icon icon="replay" baseline></material-icon></material-button>
|
||||||
<material-button id="load" (click)="onLoadClicked()"><material-icon icon="history" baseline></material-icon></material-button>
|
|
||||||
<material-button id="run" (click)="onStartClicked()">
|
<material-button id="run" (click)="onStartClicked()">
|
||||||
<span [ngSwitch]="engine.isRunning">
|
<span [ngSwitch]="ctrl.isRunning">
|
||||||
<material-icon *ngSwitchCase="false" icon="play_arrow" baseline></material-icon>
|
<material-icon *ngSwitchCase="false" icon="play_arrow" baseline></material-icon>
|
||||||
<material-icon *ngSwitchCase="true" icon="pause" baseline></material-icon>
|
<material-icon *ngSwitchCase="true" icon="pause" baseline></material-icon>
|
||||||
</span>
|
</span>
|
||||||
|
|
|
@ -1,8 +1,7 @@
|
||||||
import 'dart:html' as html;
|
import 'dart:html' as html;
|
||||||
|
|
||||||
import 'package:angular/angular.dart';
|
import 'package:angular/angular.dart';
|
||||||
import 'package:rules_of_living/service/engine_service.dart';
|
import 'package:rules_of_living/service/configuration_service.dart';
|
||||||
import 'package:rules_of_living/service/simulation_service.dart';
|
|
||||||
|
|
||||||
@Component(
|
@Component(
|
||||||
selector: 'gol-simulation',
|
selector: 'gol-simulation',
|
||||||
|
@ -11,10 +10,9 @@ import 'package:rules_of_living/service/simulation_service.dart';
|
||||||
providers: [],
|
providers: [],
|
||||||
)
|
)
|
||||||
class SimulationComponent implements OnInit {
|
class SimulationComponent implements OnInit {
|
||||||
final EngineService engine;
|
final ConfigurationService config;
|
||||||
final SimulationService sim;
|
|
||||||
|
|
||||||
SimulationComponent(this.engine, this.sim);
|
SimulationComponent(this.config);
|
||||||
|
|
||||||
@override
|
@override
|
||||||
void ngOnInit() {
|
void ngOnInit() {
|
||||||
|
@ -31,6 +29,6 @@ class SimulationComponent implements OnInit {
|
||||||
|
|
||||||
the canvas did not load correctly :(
|
the canvas did not load correctly :(
|
||||||
''', canvas.width / 2 - 50, canvas.height / 2);
|
''', canvas.width / 2 - 50, canvas.height / 2);
|
||||||
sim.canvas = canvas;
|
config.canvas = canvas;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
43
lib/service/configuration_service.dart
Normal file
43
lib/service/configuration_service.dart
Normal file
|
@ -0,0 +1,43 @@
|
||||||
|
import 'dart:html' as html;
|
||||||
|
import 'dart:math';
|
||||||
|
|
||||||
|
import 'package:rules_of_living/service/engine_service.dart';
|
||||||
|
|
||||||
|
class ConfigurationService {
|
||||||
|
final EngineService _es;
|
||||||
|
|
||||||
|
bool showGrid;
|
||||||
|
|
||||||
|
int _simSpeed;
|
||||||
|
|
||||||
|
ConfigurationService(this._es) {
|
||||||
|
showGrid = false;
|
||||||
|
simSpeed = 5;
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Simulation Speed
|
||||||
|
///
|
||||||
|
/// Sets the number of updates the simulation takes per second. Can range from
|
||||||
|
/// 1 to arbitrarily high numbers (though setting it too high can potentially
|
||||||
|
/// make the app brittle).
|
||||||
|
int get simSpeed => _simSpeed;
|
||||||
|
void set simSpeed(int val) {
|
||||||
|
_simSpeed = val;
|
||||||
|
_es.engine.stepsPerSecond = simSpeed;
|
||||||
|
}
|
||||||
|
|
||||||
|
void set canvas(html.CanvasElement canvas) => _es.engine.canvas = canvas;
|
||||||
|
html.CanvasElement get canvas => _es.engine.canvas;
|
||||||
|
|
||||||
|
void toggleGrid() {
|
||||||
|
showGrid = !showGrid;
|
||||||
|
}
|
||||||
|
|
||||||
|
void setGridSize({int x, int y}) {
|
||||||
|
x = x ?? _es.engine.gridSize.x;
|
||||||
|
y = y ?? _es.engine.gridSize.y;
|
||||||
|
_es.engine.gridSize = Point(x, y);
|
||||||
|
}
|
||||||
|
|
||||||
|
Point<int> get gridSize => _es.engine.gridSize;
|
||||||
|
}
|
38
lib/service/control_service.dart
Normal file
38
lib/service/control_service.dart
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
import 'package:rules_of_living/service/engine_service.dart';
|
||||||
|
|
||||||
|
class ControlService {
|
||||||
|
EngineService _es;
|
||||||
|
|
||||||
|
ControlService(this._es);
|
||||||
|
|
||||||
|
void run() {
|
||||||
|
_es.engine.running = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
void stop() {
|
||||||
|
_es.engine.running = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void toggleRunning() {
|
||||||
|
_es.engine.running = !_es.engine.running;
|
||||||
|
}
|
||||||
|
|
||||||
|
void step() {
|
||||||
|
_es.engine.step();
|
||||||
|
}
|
||||||
|
|
||||||
|
void reset() {
|
||||||
|
_es.engine.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
void addRandomPattern() {
|
||||||
|
_es.engine.running = false;
|
||||||
|
_es.engine.addPattern();
|
||||||
|
}
|
||||||
|
|
||||||
|
void clear() {
|
||||||
|
_es.engine.clear();
|
||||||
|
}
|
||||||
|
|
||||||
|
bool get isRunning => _es.engine.running;
|
||||||
|
}
|
|
@ -1,13 +1,8 @@
|
||||||
import 'package:rules_of_living/src/Engine.dart';
|
import 'package:rules_of_living/src/Engine.dart';
|
||||||
import 'package:rules_of_living/src/Simulation.dart';
|
|
||||||
|
|
||||||
class EngineService {
|
class EngineService {
|
||||||
Engine _uncachedEngineAccess;
|
Engine _uncachedEngineAccess;
|
||||||
|
|
||||||
EngineService() {
|
|
||||||
simSpeed = 5;
|
|
||||||
}
|
|
||||||
|
|
||||||
Engine get engine => _uncachedEngineAccess ?? _setCachedAndReturn(Engine());
|
Engine get engine => _uncachedEngineAccess ?? _setCachedAndReturn(Engine());
|
||||||
void set engine(Engine newEngine) {
|
void set engine(Engine newEngine) {
|
||||||
_uncachedEngineAccess = newEngine;
|
_uncachedEngineAccess = newEngine;
|
||||||
|
@ -17,32 +12,4 @@ class EngineService {
|
||||||
engine = newEngine;
|
engine = newEngine;
|
||||||
return newEngine;
|
return newEngine;
|
||||||
}
|
}
|
||||||
|
|
||||||
void run() {
|
|
||||||
engine.running = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
void stop() {
|
|
||||||
engine.running = false;
|
|
||||||
}
|
|
||||||
|
|
||||||
void toggleRunning() {
|
|
||||||
engine.running = !engine.running;
|
|
||||||
}
|
|
||||||
|
|
||||||
void step() {
|
|
||||||
engine.step();
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Simulation Speed
|
|
||||||
///
|
|
||||||
/// Sets the number of updates the simulation takes per second. Can range from
|
|
||||||
/// 1 to arbitrarily high numbers (though setting it too high can potentially
|
|
||||||
/// make the app brittle).
|
|
||||||
int get simSpeed => engine.stepsPerSecond;
|
|
||||||
void set simSpeed(int val) => engine.stepsPerSecond = val;
|
|
||||||
|
|
||||||
bool get isRunning => engine.running;
|
|
||||||
|
|
||||||
void set simulation(Simulation value) => engine.simulation = value;
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,43 +0,0 @@
|
||||||
import 'dart:html' as html;
|
|
||||||
import 'dart:math';
|
|
||||||
|
|
||||||
import 'package:rules_of_living/service/engine_service.dart';
|
|
||||||
import 'package:rules_of_living/src/Simulation.dart';
|
|
||||||
|
|
||||||
class SimulationService {
|
|
||||||
// DEFAULT VALUES
|
|
||||||
static final int DEFAULT_GRID_SIZE = 50;
|
|
||||||
|
|
||||||
final EngineService _engine;
|
|
||||||
final Simulation _sim;
|
|
||||||
|
|
||||||
SimulationService(this._engine, [Simulation sim])
|
|
||||||
: this._sim = sim ?? Simulation(DEFAULT_GRID_SIZE, DEFAULT_GRID_SIZE) {
|
|
||||||
_engine.simulation = _sim;
|
|
||||||
_sim.addRandomPattern(amount: 15, dispersal: 5);
|
|
||||||
}
|
|
||||||
|
|
||||||
void reset() {
|
|
||||||
_sim.reset();
|
|
||||||
}
|
|
||||||
|
|
||||||
void addRandomPattern() {
|
|
||||||
_sim.addRandomPattern();
|
|
||||||
}
|
|
||||||
|
|
||||||
Point<int> get gridSize => _sim.gridSize;
|
|
||||||
void set gridSize(Point<int> size) {
|
|
||||||
_sim.gridSize = size;
|
|
||||||
}
|
|
||||||
|
|
||||||
//TODO split into RenderService when rendering is decoupled from engine.
|
|
||||||
html.CanvasElement get canvas => _engine.engine.canvas;
|
|
||||||
void set canvas(html.CanvasElement canvas) => _engine.engine.canvas = canvas;
|
|
||||||
|
|
||||||
void toggleGrid() {
|
|
||||||
_sim.renderEdges = !_sim.renderEdges;
|
|
||||||
}
|
|
||||||
|
|
||||||
void save() => _sim.saveSnapshot();
|
|
||||||
void load() => _sim.loadSnapshot();
|
|
||||||
}
|
|
36
lib/src/Cell.dart
Normal file
36
lib/src/Cell.dart
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
import 'package:rules_of_living/src/Rule.dart';
|
||||||
|
|
||||||
|
class Cell {
|
||||||
|
bool state;
|
||||||
|
bool nextState = false;
|
||||||
|
List<Rule> surviveRules = new List<Rule>();
|
||||||
|
List<Rule> birthRules = new List<Rule>();
|
||||||
|
|
||||||
|
/// 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;
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,4 +1,5 @@
|
||||||
import 'dart:html' as html;
|
import 'dart:html' as html;
|
||||||
|
import 'dart:math';
|
||||||
|
|
||||||
import 'package:rules_of_living/src/Simulation.dart';
|
import 'package:rules_of_living/src/Simulation.dart';
|
||||||
|
|
||||||
|
@ -28,6 +29,16 @@ class Engine {
|
||||||
// ms stuck in updateloop after which game will declare itself unresponsive
|
// ms stuck in updateloop after which game will declare itself unresponsive
|
||||||
final int SAFETY_TIMEOUT = 2000;
|
final int SAFETY_TIMEOUT = 2000;
|
||||||
|
|
||||||
|
/// Grid Size
|
||||||
|
///
|
||||||
|
/// Number of cells on x coordinate and y coordinate. Can be set individually.
|
||||||
|
Point get gridSize => Point<int>(_grid.w, _grid.h);
|
||||||
|
void set gridSize(Point<int> value) {
|
||||||
|
if (value.x <= 0 || value.y <= 0)
|
||||||
|
throw ArgumentError("grid size must not be smaller than 1");
|
||||||
|
_grid = Simulation(value.x, value.y);
|
||||||
|
}
|
||||||
|
|
||||||
num _updateLag = 0.0;
|
num _updateLag = 0.0;
|
||||||
num _drawLag = 0.0;
|
num _drawLag = 0.0;
|
||||||
|
|
||||||
|
@ -37,58 +48,60 @@ class Engine {
|
||||||
/// be used if no canvas was defined at engine creation and it should be
|
/// be used if no canvas was defined at engine creation and it should be
|
||||||
/// rendered later.
|
/// rendered later.
|
||||||
html.CanvasElement canvas;
|
html.CanvasElement canvas;
|
||||||
Simulation _simulation;
|
Simulation _grid;
|
||||||
bool running = false;
|
bool running = false;
|
||||||
|
|
||||||
Engine() {
|
Engine([x = 100, y = 100, this.canvas]) {
|
||||||
|
_grid = Simulation(x, y);
|
||||||
|
|
||||||
_elapsed.start();
|
_elapsed.start();
|
||||||
|
_grid.addPattern(amount: 15, dispersal: 5);
|
||||||
html.window.animationFrame.then(animFrame);
|
html.window.animationFrame.then(animFrame);
|
||||||
}
|
}
|
||||||
|
|
||||||
void animFrame(num now) {
|
void animFrame(num now) {
|
||||||
int elapsed = _elapsed.elapsedMilliseconds;
|
process(now);
|
||||||
_elapsed.reset();
|
|
||||||
process(elapsed, SAFETY_TIMEOUT, update: this.update, render: this.render);
|
|
||||||
html.window.animationFrame.then(animFrame);
|
html.window.animationFrame.then(animFrame);
|
||||||
}
|
}
|
||||||
|
|
||||||
void process(int elapsed, int timeOut, {Function update, Function render}) {
|
void reset() {
|
||||||
_drawLag += elapsed;
|
_grid.reset();
|
||||||
_updateLag += elapsed;
|
running = false;
|
||||||
|
}
|
||||||
|
|
||||||
while (running == true &&
|
void clear() {
|
||||||
_shouldUpdate(_updateLag, elapsed, timeOut) == true) {
|
_grid = new Simulation(gridSize.x, gridSize.y);
|
||||||
|
running = false;
|
||||||
|
}
|
||||||
|
|
||||||
|
void process(num now) {
|
||||||
|
_drawLag += _elapsed.elapsedMilliseconds;
|
||||||
|
_updateLag += _elapsed.elapsedMilliseconds;
|
||||||
|
_elapsed.reset();
|
||||||
|
|
||||||
|
while (_updateLag >= _MS_PER_STEP) {
|
||||||
|
if (_elapsed.elapsedMilliseconds > SAFETY_TIMEOUT) {
|
||||||
|
// TODO stub - give warning etc when this occurs
|
||||||
|
print("ERROR STUCK IN UPDATE LOOP");
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
if (running == true) update();
|
||||||
_updateLag -= _MS_PER_STEP;
|
_updateLag -= _MS_PER_STEP;
|
||||||
if (update == null) break;
|
|
||||||
update();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (_drawLag >= _MS_PER_FRAME) {
|
if (_drawLag >= _MS_PER_FRAME) {
|
||||||
_drawLag = 0;
|
|
||||||
if (render == null) return;
|
|
||||||
render(_updateLag / _MS_PER_STEP);
|
render(_updateLag / _MS_PER_STEP);
|
||||||
|
_drawLag = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
bool _shouldUpdate(int updateLag, int elapsed, int timeOut) {
|
|
||||||
if (updateLag < _MS_PER_STEP) return false;
|
|
||||||
if (elapsed > timeOut) throw StackOverflowError;
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
/// Update Engine Logic
|
/// Update Engine Logic
|
||||||
///
|
///
|
||||||
/// Updates the logic of the engine by one tick. Should usually not be called
|
/// Updates the logic of the engine by one tick. Should usually not be called
|
||||||
/// directly, since it is automatically taken care of by the processing function.
|
/// directly, since it is automatically taken care of by the processing function.
|
||||||
/// If simulation should be advanced manually one time, prefer using step().
|
/// If simulation should be advanced manually one time, prefer using step().
|
||||||
void update() {
|
void update() {
|
||||||
if (_simulation == null) return;
|
if (!_grid.update()) running = false;
|
||||||
|
|
||||||
Map<int, bool> simulationUpdate = _simulation.update();
|
|
||||||
_simulation.mergeStateChanges(simulationUpdate);
|
|
||||||
|
|
||||||
if (simulationUpdate.length == 0) running = false;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Advances Logic One Update
|
/// Advances Logic One Update
|
||||||
|
@ -97,8 +110,8 @@ class Engine {
|
||||||
/// simulation. Does not automatically re-render the new state
|
/// simulation. Does not automatically re-render the new state
|
||||||
/// (though this should usually not pose a problem).
|
/// (though this should usually not pose a problem).
|
||||||
void step() {
|
void step() {
|
||||||
update();
|
|
||||||
running = false;
|
running = false;
|
||||||
|
_grid.update();
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Renders the Current Simulation State
|
/// Renders the Current Simulation State
|
||||||
|
@ -107,10 +120,26 @@ class Engine {
|
||||||
/// the internal engine processing. Does not do anything if no canvas is
|
/// the internal engine processing. Does not do anything if no canvas is
|
||||||
/// defined.
|
/// defined.
|
||||||
void render([num interp]) {
|
void render([num interp]) {
|
||||||
if (canvas == null || _simulation == null) return;
|
if (canvas != null) _grid.render(canvas, interp);
|
||||||
|
|
||||||
_simulation.render(canvas, interp);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void set simulation(Simulation value) => _simulation = value;
|
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 toggleEdgeRendering() {
|
||||||
|
_grid.renderEdges = !_grid.renderEdges;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
5
lib/src/Rule.dart
Normal file
5
lib/src/Rule.dart
Normal file
|
@ -0,0 +1,5 @@
|
||||||
|
class Rule {
|
||||||
|
final Function evaluate;
|
||||||
|
|
||||||
|
Rule(this.evaluate);
|
||||||
|
}
|
|
@ -1,116 +1,147 @@
|
||||||
import 'dart:html' as html;
|
import 'dart:html' as html;
|
||||||
import 'dart:math' as math;
|
import 'dart:math' as math;
|
||||||
import 'dart:math';
|
|
||||||
|
|
||||||
|
import 'package:rules_of_living/src/Cell.dart';
|
||||||
import 'package:rules_of_living/src/Grid.dart';
|
import 'package:rules_of_living/src/Grid.dart';
|
||||||
import 'package:rules_of_living/src/rules/GameOfLife.dart';
|
import 'package:rules_of_living/src/Rule.dart';
|
||||||
import 'package:rules_of_living/src/rules/RuleSet.dart';
|
|
||||||
|
|
||||||
enum CellPattern { SpaceShip, Blinker }
|
enum CellPattern { SpaceShip, Blinker }
|
||||||
|
|
||||||
class Simulation {
|
class Simulation {
|
||||||
Grid<bool> map;
|
final Grid<Cell> map;
|
||||||
Grid<bool> _snapshot;
|
|
||||||
|
|
||||||
RuleSet rules = GameOfLife();
|
|
||||||
|
|
||||||
bool _dirty = true;
|
bool _dirty = true;
|
||||||
bool get dirty => _dirty;
|
|
||||||
|
|
||||||
bool _renderEdges = true;
|
bool _renderEdges = true;
|
||||||
bool get renderEdges => _renderEdges;
|
|
||||||
|
|
||||||
|
int _startingSeed;
|
||||||
|
int _x;
|
||||||
|
int _y;
|
||||||
int _amount;
|
int _amount;
|
||||||
int _dispersal;
|
int _dispersal;
|
||||||
|
CellPattern _pattern;
|
||||||
|
|
||||||
Point get gridSize => Point<int>(map.width, map.height);
|
int get w => map.width;
|
||||||
void set gridSize(Point<int> value) {
|
int get h => map.height;
|
||||||
if (value.x <= 0 || value.y <= 0)
|
|
||||||
throw ArgumentError("grid size must not be smaller than 1");
|
|
||||||
map = Grid(value.x, value.y);
|
|
||||||
}
|
|
||||||
|
|
||||||
Simulation(int w, int h) : this.map = new Grid(w, h) {
|
Simulation(int w, int h) : this.map = new Grid(w, h) {
|
||||||
this.map = reset();
|
for (int i = 0; i < map.length; i++) {
|
||||||
|
map[i] = _getGOLCell();
|
||||||
|
}
|
||||||
|
print("Grid creation finished");
|
||||||
}
|
}
|
||||||
|
|
||||||
Simulation.fromGrid(Grid<bool> map) : this.map = map;
|
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);
|
||||||
|
return cell;
|
||||||
|
}
|
||||||
|
|
||||||
Grid<bool> reset([Grid<bool> map]) {
|
void reset() {
|
||||||
map ??= this.map;
|
map.setAll(0, List.filled(map.length, _getGOLCell()));
|
||||||
|
if (_startingSeed != null)
|
||||||
|
addPattern(
|
||||||
|
pattern: _pattern,
|
||||||
|
dispersal: _dispersal,
|
||||||
|
amount: _amount,
|
||||||
|
seed: _startingSeed,
|
||||||
|
x: _x,
|
||||||
|
y: _y);
|
||||||
_dirty = true;
|
_dirty = true;
|
||||||
map.setAll(0, List.filled(map.length, false));
|
|
||||||
return map;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void addRandomPattern({int amount, int dispersal}) {
|
void addPattern(
|
||||||
int _startingSeed = DateTime.now().millisecondsSinceEpoch;
|
{CellPattern pattern,
|
||||||
|
int x,
|
||||||
|
int y,
|
||||||
|
int amount,
|
||||||
|
int dispersal,
|
||||||
|
int seed}) {
|
||||||
|
_startingSeed = seed ?? DateTime.now().millisecondsSinceEpoch;
|
||||||
math.Random rng = new math.Random(_startingSeed);
|
math.Random rng = new math.Random(_startingSeed);
|
||||||
|
_x = x;
|
||||||
|
_y = y;
|
||||||
_amount = amount ?? rng.nextInt(20);
|
_amount = amount ?? rng.nextInt(20);
|
||||||
_dispersal = dispersal ?? 10;
|
_dispersal = dispersal ?? 10;
|
||||||
int cx = rng.nextInt(map.width ~/ 3) + (map.width ~/ 3);
|
_pattern = pattern;
|
||||||
int cy = rng.nextInt(map.height ~/ 3) + (map.height ~/ 3);
|
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);
|
||||||
|
|
||||||
int sanityCheck = 0;
|
setCellState(cx + 2, cy + 2, true);
|
||||||
for (var i = 0; i < (_amount); i++) {
|
setCellState(cx + 3, cy + 2, true);
|
||||||
sanityCheck++;
|
setCellState(cx + 2, cy + 3, true);
|
||||||
getCellState(cx, cy)
|
setCellState(cx + 3, cy + 3, true);
|
||||||
? i--
|
break;
|
||||||
: setCellState(
|
// A 'gliding' Spaceship
|
||||||
cx + rng.nextInt(_dispersal), cy + rng.nextInt(_dispersal), true);
|
// #
|
||||||
if (sanityCheck > 100 && sanityCheck > i * 3) break;
|
// #
|
||||||
|
// ###
|
||||||
|
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++) {
|
||||||
|
sanityCheck++;
|
||||||
|
getCellState(cx, cy)
|
||||||
|
? i--
|
||||||
|
: setCellState(cx + rng.nextInt(_dispersal),
|
||||||
|
cy + rng.nextInt(_dispersal), true);
|
||||||
|
if (sanityCheck > 100 && sanityCheck > i * 3) break;
|
||||||
|
}
|
||||||
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
_dirty = true;
|
_dirty = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void setCellState(int x, int y, bool state) {
|
void setCellState(int x, int y, bool state) {
|
||||||
if (y >= map.height || x >= map.width) return null;
|
if (y < map.height && x < map.width) map.get(x, y).state = state;
|
||||||
|
|
||||||
state ? map.set(x, y, true) : map.set(x, y, false);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
bool getCellState(int x, int y) {
|
bool getCellState(int x, int y) {
|
||||||
if (y >= map.height || x >= map.width) return null;
|
if (y < map.height && x < map.width) return map.get(x, y).state;
|
||||||
|
return null;
|
||||||
return map.get(x, y);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void toggleCellState(int x, int y) {
|
bool update() {
|
||||||
if (y >= map.height || x >= map.width) return null;
|
bool stateChanges = false;
|
||||||
|
|
||||||
getCellState(x, y) == null
|
|
||||||
? setCellState(x, y, true)
|
|
||||||
: setCellState(x, y, false);
|
|
||||||
}
|
|
||||||
|
|
||||||
Map<int, bool> update() {
|
|
||||||
Map<int, bool> stateChanges = calculateNextState(map);
|
|
||||||
return stateChanges;
|
|
||||||
}
|
|
||||||
|
|
||||||
void mergeStateChanges(Map<int, bool> stateChanges) {
|
|
||||||
stateChanges.forEach((i, el) => map[i] = el);
|
|
||||||
if (stateChanges.length != 0) _dirty = true;
|
|
||||||
}
|
|
||||||
|
|
||||||
Map<int, bool> calculateNextState(Grid<bool> oldState) {
|
|
||||||
Map<int, bool> stateChanges = Map();
|
|
||||||
|
|
||||||
for (int i = 0; i < map.length; i++) {
|
for (int i = 0; i < map.length; i++) {
|
||||||
math.Point p = map.toCoordinates(i);
|
math.Point p = map.toCoordinates(i);
|
||||||
bool cell = map[i];
|
map[i].update(getSurroundingNeighbors(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) {
|
|
||||||
stateChanges[i] = false;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
// TODO when implementing changeSet we can remove this second loop and add to changeSet in the first
|
||||||
|
map.forEach((Cell el) {
|
||||||
|
if (el.state != el.nextState) stateChanges = true;
|
||||||
|
el.advanceState();
|
||||||
|
});
|
||||||
|
stateChanges ? _dirty = true : false;
|
||||||
return stateChanges;
|
return stateChanges;
|
||||||
}
|
}
|
||||||
|
|
||||||
int getNeighbors(int x, int y, int range) {
|
int getSurroundingNeighbors(int x, int y, int range) {
|
||||||
int count = 0;
|
int count = 0;
|
||||||
for (int ix = -range + x; ix <= range + x; ix++) {
|
for (int ix = -range + x; ix <= range + x; ix++) {
|
||||||
for (int iy = -range + y; iy <= range + y; iy++) {
|
for (int iy = -range + y; iy <= range + y; iy++) {
|
||||||
|
@ -118,7 +149,7 @@ class Simulation {
|
||||||
iy >= 0 &&
|
iy >= 0 &&
|
||||||
ix < map.width &&
|
ix < map.width &&
|
||||||
iy < map.height &&
|
iy < map.height &&
|
||||||
getCellState(ix, iy) == true &&
|
map.get(ix, iy).state == true &&
|
||||||
!(x == ix && y == iy)) count++;
|
!(x == ix && y == iy)) count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -140,7 +171,7 @@ class Simulation {
|
||||||
ctx.strokeRect(p.x * brickW, p.y * brickH, brickW, brickH);
|
ctx.strokeRect(p.x * brickW, p.y * brickH, brickW, brickH);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (map[i] == true)
|
if (map[i].state == true)
|
||||||
ctx.setFillColorRgb(155, 155, 255);
|
ctx.setFillColorRgb(155, 155, 255);
|
||||||
else
|
else
|
||||||
ctx.setFillColorRgb(0, 0, 0);
|
ctx.setFillColorRgb(0, 0, 0);
|
||||||
|
@ -154,10 +185,5 @@ class Simulation {
|
||||||
_dirty = true;
|
_dirty = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void saveSnapshot() => _snapshot = Grid.from(map);
|
bool get renderEdges => _renderEdges;
|
||||||
Grid<bool> loadSnapshot() {
|
|
||||||
map = Grid.from(_snapshot);
|
|
||||||
_dirty = true;
|
|
||||||
return map;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,17 +0,0 @@
|
||||||
import 'dart:math';
|
|
||||||
|
|
||||||
import 'package:collection/collection.dart';
|
|
||||||
|
|
||||||
class CellPattern extends DelegatingList<Point> {
|
|
||||||
final String _name;
|
|
||||||
CellPattern(String name, List<Point> base)
|
|
||||||
: _name = name,
|
|
||||||
super(base);
|
|
||||||
|
|
||||||
String get name => _name;
|
|
||||||
|
|
||||||
@override
|
|
||||||
String toString() {
|
|
||||||
return "$name: ${super.toString()}";
|
|
||||||
}
|
|
||||||
}
|
|
|
@ -1,38 +0,0 @@
|
||||||
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 range = 1;
|
|
||||||
List<CellPattern> patterns = <CellPattern>[
|
|
||||||
// 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;
|
|
||||||
}
|
|
|
@ -1,9 +0,0 @@
|
||||||
import 'package:rules_of_living/src/rules/CellPattern.dart';
|
|
||||||
|
|
||||||
abstract class RuleSet {
|
|
||||||
int range;
|
|
||||||
List<CellPattern> patterns;
|
|
||||||
|
|
||||||
bool checkSurvival(int neighbors);
|
|
||||||
bool checkBirth(int neighbors);
|
|
||||||
}
|
|
46
test/service/configuration_service_test.dart
Normal file
46
test/service/configuration_service_test.dart
Normal file
|
@ -0,0 +1,46 @@
|
||||||
|
import 'dart:math';
|
||||||
|
|
||||||
|
import 'package:mockito/mockito.dart';
|
||||||
|
import 'package:rules_of_living/service/configuration_service.dart';
|
||||||
|
import 'package:rules_of_living/service/engine_service.dart';
|
||||||
|
import 'package:rules_of_living/src/Engine.dart';
|
||||||
|
@TestOn('browser')
|
||||||
|
import 'package:test/test.dart';
|
||||||
|
|
||||||
|
class MockEngine extends Mock implements Engine {}
|
||||||
|
|
||||||
|
void main() {
|
||||||
|
ConfigurationService sut;
|
||||||
|
EngineService engineService;
|
||||||
|
MockEngine me;
|
||||||
|
setUp(() {
|
||||||
|
me = MockEngine();
|
||||||
|
engineService = EngineService();
|
||||||
|
engineService.engine = me;
|
||||||
|
sut = ConfigurationService(engineService);
|
||||||
|
});
|
||||||
|
|
||||||
|
group("simulation speed", () {
|
||||||
|
test("speed changes propagate to engine", () {
|
||||||
|
sut.simSpeed = 312;
|
||||||
|
verify(me.stepsPerSecond = 312);
|
||||||
|
});
|
||||||
|
});
|
||||||
|
|
||||||
|
group("grid size", () {
|
||||||
|
test("grid changes are sent to engine", () {
|
||||||
|
sut.setGridSize(x: 512, y: 388);
|
||||||
|
verify(me.gridSize = Point(512, 388));
|
||||||
|
});
|
||||||
|
test("grid can be changed solely on x axis", () {
|
||||||
|
when(me.gridSize).thenReturn(Point(100, 100));
|
||||||
|
sut.setGridSize(x: 555);
|
||||||
|
verify(me.gridSize = Point(555, 100));
|
||||||
|
});
|
||||||
|
test("grid can be changed solely on y axis", () {
|
||||||
|
when(me.gridSize).thenReturn(Point(100, 100));
|
||||||
|
sut.setGridSize(y: 556);
|
||||||
|
verify(me.gridSize = Point(100, 556));
|
||||||
|
});
|
||||||
|
});
|
||||||
|
}
|
|
@ -1,23 +0,0 @@
|
||||||
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());
|
|
||||||
});
|
|
||||||
}
|
|
|
@ -1,50 +0,0 @@
|
||||||
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';
|
|
||||||
|
|
||||||
class MockGrid extends Mock implements Grid<bool> {}
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
Simulation sut;
|
|
||||||
setUp(() {
|
|
||||||
sut = Simulation(10, 10);
|
|
||||||
});
|
|
||||||
group("gridSize", () {
|
|
||||||
test(
|
|
||||||
"returns the width and height of the underlying grid",
|
|
||||||
() => expect(
|
|
||||||
sut.gridSize, equals(Point<int>(sut.map.width, sut.map.height))));
|
|
||||||
test("sets the underlying grid width and height", () {
|
|
||||||
sut.gridSize = Point(2, 3);
|
|
||||||
expect(sut.gridSize, equals(Point(2, 3)));
|
|
||||||
});
|
|
||||||
test("creates a new underlying grid on resizing", () {
|
|
||||||
var oldMap = sut.map;
|
|
||||||
sut.gridSize = Point(10, 10);
|
|
||||||
expect(sut.map, isNot(oldMap));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
group("reset", () {
|
|
||||||
test("returns a map filled with 'false' ", () {
|
|
||||||
expect(sut.reset(), allOf(TypeMatcher<Grid>(), isNot(contains(true))));
|
|
||||||
});
|
|
||||||
test("sets the simulation to need re-rendering", () {
|
|
||||||
sut.reset();
|
|
||||||
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)));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
}
|
|
|
@ -1,106 +1,15 @@
|
||||||
import 'dart:html' as html;
|
import 'dart:html' as html;
|
||||||
import 'package:rules_of_living/src/Simulation.dart';
|
import 'dart:math';
|
||||||
import 'package:test/test.dart';
|
|
||||||
import 'package:mockito/mockito.dart';
|
|
||||||
|
|
||||||
@TestOn('browser')
|
@TestOn('browser')
|
||||||
import 'package:rules_of_living/src/Engine.dart';
|
import 'package:rules_of_living/src/Engine.dart';
|
||||||
|
import 'package:test/test.dart';
|
||||||
class MockSimulation extends Mock implements Simulation {
|
|
||||||
int updateNum = 0;
|
|
||||||
bool hasChanges = false;
|
|
||||||
|
|
||||||
@override
|
|
||||||
Map<int, bool> update() {
|
|
||||||
updateNum++;
|
|
||||||
return hasChanges ? {1: true, 2: false} : {};
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
void main() {
|
void main() {
|
||||||
Engine sut;
|
Engine sut;
|
||||||
setUp(() {
|
setUp(() {
|
||||||
sut = Engine();
|
sut = Engine();
|
||||||
});
|
});
|
||||||
group("process", () {
|
|
||||||
setUp(() => sut.running = true);
|
|
||||||
test("errors out if updating takes too long",
|
|
||||||
() => expect(() => sut.process(5000, 10), throwsA(StackOverflowError)));
|
|
||||||
test("does not update if not enough time elapsed to pass ms per step", () {
|
|
||||||
bool result = false;
|
|
||||||
sut.stepsPerSecond = 1000;
|
|
||||||
|
|
||||||
sut.process(999, 2000,
|
|
||||||
update: () => result = true, render: (double interp) => null);
|
|
||||||
|
|
||||||
expect(result, true);
|
|
||||||
});
|
|
||||||
test("updates only when the ms per step threshold is crossed", () {
|
|
||||||
int updateNum = 0;
|
|
||||||
sut.stepsPerSecond = 1;
|
|
||||||
|
|
||||||
sut.process(1001, 2000, update: () => updateNum++);
|
|
||||||
expect(updateNum, equals(1));
|
|
||||||
});
|
|
||||||
test("updates until updateLag has been resolved", () {
|
|
||||||
int updateNum = 0;
|
|
||||||
sut.stepsPerSecond = 1;
|
|
||||||
|
|
||||||
sut.process(2999, 5000, update: () => updateNum++);
|
|
||||||
expect(updateNum, equals(2));
|
|
||||||
});
|
|
||||||
test("works without passing in update or render function arguments", () {
|
|
||||||
sut.stepsPerSecond = 1000;
|
|
||||||
expect(() => sut.process(500, 5000), isNot(throwsA(anything)));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
group("update", () {
|
|
||||||
MockSimulation mockSim;
|
|
||||||
setUp(() {
|
|
||||||
mockSim = MockSimulation();
|
|
||||||
sut.simulation = mockSim;
|
|
||||||
});
|
|
||||||
test("does not error out if no simulation variable is set", () {
|
|
||||||
sut.simulation = null;
|
|
||||||
expect(() => sut.update(), isNot(throwsA(anything)));
|
|
||||||
});
|
|
||||||
test("updates simulation one tick for every time it is called", () {
|
|
||||||
sut.update();
|
|
||||||
sut.update();
|
|
||||||
sut.update();
|
|
||||||
expect(mockSim.updateNum, equals(3));
|
|
||||||
});
|
|
||||||
test("sets running to false when simulation returns no changes", () {
|
|
||||||
sut.running = true;
|
|
||||||
sut.update();
|
|
||||||
expect(sut.running, equals(false));
|
|
||||||
});
|
|
||||||
test("keeps running when simulation returns changes", () {
|
|
||||||
sut.running = true;
|
|
||||||
mockSim.hasChanges = true;
|
|
||||||
|
|
||||||
sut.update();
|
|
||||||
|
|
||||||
expect(sut.running, equals(true));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
group("step", () {
|
|
||||||
MockSimulation mockSim;
|
|
||||||
setUp(() {
|
|
||||||
mockSim = MockSimulation();
|
|
||||||
sut.simulation = mockSim;
|
|
||||||
});
|
|
||||||
test("advances the simulation by one update", () {
|
|
||||||
sut.step();
|
|
||||||
expect(mockSim.updateNum, equals(1));
|
|
||||||
});
|
|
||||||
test("turns off continuous engine updates", () {
|
|
||||||
sut.running = true;
|
|
||||||
sut.step();
|
|
||||||
|
|
||||||
expect(sut.running, equals(false));
|
|
||||||
});
|
|
||||||
});
|
|
||||||
group("canvas", () {
|
group("canvas", () {
|
||||||
test("Engine can be instantiated without canvas", () {
|
test("Engine can be instantiated without canvas", () {
|
||||||
expect(sut, isNot(throwsNoSuchMethodError));
|
expect(sut, isNot(throwsNoSuchMethodError));
|
||||||
|
@ -122,4 +31,12 @@ void main() {
|
||||||
expect(sut.canvas, isNotNull);
|
expect(sut.canvas, isNotNull);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
group("gridSize", () {
|
||||||
|
test("zero gridSizes throw ArgumentErrors", () {
|
||||||
|
expect(() => sut.gridSize = Point(0, 5), throwsArgumentError);
|
||||||
|
});
|
||||||
|
test("negative gridSizes throw ArgumentErrors", () {
|
||||||
|
expect(() => sut.gridSize = Point(1, -5), throwsArgumentError);
|
||||||
|
});
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,19 +0,0 @@
|
||||||
import 'dart:math';
|
|
||||||
|
|
||||||
import 'package:rules_of_living/src/rules/CellPattern.dart';
|
|
||||||
import 'package:test/test.dart';
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
CellPattern sut;
|
|
||||||
setUp(() {
|
|
||||||
sut = CellPattern("testPattern", [Point(1, 1), Point(0, 0), Point(-1, -1)]);
|
|
||||||
});
|
|
||||||
group("Naming", () {
|
|
||||||
test("contains the name passed in for name variable",
|
|
||||||
() => expect(sut.name, "testPattern"));
|
|
||||||
test(
|
|
||||||
"Contains the name passed in on being formatted as String",
|
|
||||||
() => expect(sut.toString(),
|
|
||||||
"testPattern: [Point(1, 1), Point(0, 0), Point(-1, -1)]"));
|
|
||||||
});
|
|
||||||
}
|
|
|
@ -1,27 +0,0 @@
|
||||||
import 'package:rules_of_living/src/rules/GameOfLife.dart';
|
|
||||||
import 'package:test/test.dart';
|
|
||||||
|
|
||||||
void main() {
|
|
||||||
GameOfLife sut;
|
|
||||||
setUp(() {
|
|
||||||
sut = GameOfLife();
|
|
||||||
});
|
|
||||||
group("BirthRules", () {
|
|
||||||
test("will return true when being passed three neighbors",
|
|
||||||
() => expect(sut.checkBirth(3), true));
|
|
||||||
test("will return false when being passed zero neighbors",
|
|
||||||
() => expect(sut.checkBirth(0), false));
|
|
||||||
test("will return false when being passed two neighbors",
|
|
||||||
() => expect(sut.checkBirth(2), false));
|
|
||||||
});
|
|
||||||
group("SurviveRules", () {
|
|
||||||
test("will return true when being passed two neighbors",
|
|
||||||
() => expect(sut.checkSurvival(2), true));
|
|
||||||
test("will return true when being passed three neighbors",
|
|
||||||
() => expect(sut.checkSurvival(3), true));
|
|
||||||
test("will return false when being passed 0 neighbors",
|
|
||||||
() => expect(sut.checkSurvival(0), false));
|
|
||||||
test("will return false when being passed more than 3 neighbors",
|
|
||||||
() => expect(sut.checkSurvival(4), false));
|
|
||||||
});
|
|
||||||
}
|
|
Loading…
Reference in a new issue