2018-07-06 14:47:20 +00:00
|
|
|
import 'dart:html' as html;
|
2018-07-07 18:02:13 +00:00
|
|
|
import 'dart:math' as math;
|
2018-10-18 13:21:50 +00:00
|
|
|
import 'dart:math';
|
2018-07-06 14:47:20 +00:00
|
|
|
|
2018-08-30 08:57:14 +00:00
|
|
|
import 'package:rules_of_living/src/Grid.dart';
|
2018-10-18 09:27:44 +00:00
|
|
|
import 'package:rules_of_living/src/rules/GameOfLife.dart';
|
2018-10-18 09:21:09 +00:00
|
|
|
import 'package:rules_of_living/src/rules/RuleSet.dart';
|
2018-07-05 15:59:11 +00:00
|
|
|
|
2018-07-09 15:45:11 +00:00
|
|
|
enum CellPattern { SpaceShip, Blinker }
|
2018-07-07 18:02:13 +00:00
|
|
|
|
2018-08-29 18:11:56 +00:00
|
|
|
class Simulation {
|
2018-10-18 13:21:50 +00:00
|
|
|
Grid<bool> map;
|
2018-10-19 17:59:00 +00:00
|
|
|
Grid<bool> _snapshot;
|
|
|
|
|
2018-10-17 18:56:30 +00:00
|
|
|
RuleSet rules = GameOfLife();
|
2018-07-05 15:59:11 +00:00
|
|
|
|
2018-07-07 17:08:26 +00:00
|
|
|
bool _dirty = true;
|
2018-10-19 17:35:09 +00:00
|
|
|
bool get dirty => _dirty;
|
|
|
|
|
2018-07-08 17:01:14 +00:00
|
|
|
bool _renderEdges = true;
|
2018-10-19 17:35:09 +00:00
|
|
|
bool get renderEdges => _renderEdges;
|
2018-07-08 17:01:14 +00:00
|
|
|
|
2018-07-07 19:42:07 +00:00
|
|
|
int _amount;
|
|
|
|
int _dispersal;
|
2018-07-07 17:08:26 +00:00
|
|
|
|
2018-10-19 17:35:09 +00:00
|
|
|
Point get gridSize => Point<int>(map.width, map.height);
|
2018-10-18 13:21:50 +00:00
|
|
|
void set gridSize(Point<int> value) {
|
|
|
|
if (value.x <= 0 || value.y <= 0)
|
|
|
|
throw ArgumentError("grid size must not be smaller than 1");
|
|
|
|
map = Grid(value.x, value.y);
|
|
|
|
}
|
|
|
|
|
2018-08-30 10:03:23 +00:00
|
|
|
Simulation(int w, int h) : this.map = new Grid(w, h) {
|
2018-10-19 17:35:09 +00:00
|
|
|
this.map = reset();
|
2018-08-30 08:57:14 +00:00
|
|
|
}
|
|
|
|
|
2018-10-19 17:35:09 +00:00
|
|
|
Simulation.fromGrid(Grid<bool> map) : this.map = map;
|
|
|
|
|
|
|
|
Grid<bool> reset([Grid<bool> map]) {
|
|
|
|
map ??= this.map;
|
2018-07-07 19:42:07 +00:00
|
|
|
_dirty = true;
|
2018-10-19 17:35:09 +00:00
|
|
|
map.setAll(0, List.filled(map.length, false));
|
|
|
|
return map;
|
2018-07-07 19:42:07 +00:00
|
|
|
}
|
|
|
|
|
2018-10-18 09:32:00 +00:00
|
|
|
void addRandomPattern({int amount, int dispersal}) {
|
2018-10-18 07:57:08 +00:00
|
|
|
int _startingSeed = DateTime.now().millisecondsSinceEpoch;
|
2018-07-07 19:42:07 +00:00
|
|
|
math.Random rng = new math.Random(_startingSeed);
|
|
|
|
_amount = amount ?? rng.nextInt(20);
|
|
|
|
_dispersal = dispersal ?? 10;
|
2018-10-18 07:57:08 +00:00
|
|
|
int cx = rng.nextInt(map.width ~/ 3) + (map.width ~/ 3);
|
|
|
|
int cy = rng.nextInt(map.height ~/ 3) + (map.height ~/ 3);
|
2018-10-17 19:07:48 +00:00
|
|
|
|
|
|
|
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;
|
2018-07-07 18:02:13 +00:00
|
|
|
}
|
2018-10-17 19:07:48 +00:00
|
|
|
|
2018-07-07 20:02:04 +00:00
|
|
|
_dirty = true;
|
2018-07-07 18:02:13 +00:00
|
|
|
}
|
2018-07-07 18:49:02 +00:00
|
|
|
|
2018-07-07 18:02:13 +00:00
|
|
|
void setCellState(int x, int y, bool state) {
|
2018-10-16 16:21:01 +00:00
|
|
|
if (y >= map.height || x >= map.width) return null;
|
2018-10-15 15:28:09 +00:00
|
|
|
|
2018-10-16 16:21:01 +00:00
|
|
|
state ? map.set(x, y, true) : map.set(x, y, false);
|
2018-07-06 14:47:20 +00:00
|
|
|
}
|
|
|
|
|
2018-07-07 19:43:24 +00:00
|
|
|
bool getCellState(int x, int y) {
|
2018-10-15 15:28:09 +00:00
|
|
|
if (y >= map.height || x >= map.width) return null;
|
|
|
|
|
2018-10-16 16:21:01 +00:00
|
|
|
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);
|
2018-07-07 19:43:24 +00:00
|
|
|
}
|
|
|
|
|
2018-10-16 16:21:01 +00:00
|
|
|
Map<int, bool> update() {
|
|
|
|
Map<int, bool> stateChanges = calculateNextState(map);
|
2018-10-18 08:58:06 +00:00
|
|
|
return stateChanges;
|
|
|
|
}
|
2018-10-16 16:21:01 +00:00
|
|
|
|
2018-10-18 08:58:06 +00:00
|
|
|
void mergeStateChanges(Map<int, bool> stateChanges) {
|
2018-10-16 16:21:01 +00:00
|
|
|
stateChanges.forEach((i, el) => map[i] = el);
|
2018-10-18 08:58:06 +00:00
|
|
|
if (stateChanges.length != 0) _dirty = true;
|
2018-10-16 16:21:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Map<int, bool> calculateNextState(Grid<bool> oldState) {
|
|
|
|
Map<int, bool> stateChanges = Map();
|
2018-07-08 17:32:43 +00:00
|
|
|
|
2018-08-30 08:57:14 +00:00
|
|
|
for (int i = 0; i < map.length; i++) {
|
|
|
|
math.Point p = map.toCoordinates(i);
|
2018-10-16 16:21:01 +00:00
|
|
|
bool cell = map[i];
|
2018-10-18 09:30:14 +00:00
|
|
|
int neighbors = getNeighbors(p.x, p.y, rules.range);
|
2018-10-17 18:56:30 +00:00
|
|
|
if (cell == false && rules.checkBirth(neighbors) == true) {
|
2018-10-16 16:21:01 +00:00
|
|
|
stateChanges[i] = true;
|
2018-10-17 18:56:30 +00:00
|
|
|
} else if (cell == true && rules.checkSurvival(neighbors) == false) {
|
2018-10-16 16:21:01 +00:00
|
|
|
stateChanges[i] = false;
|
2018-10-15 15:28:09 +00:00
|
|
|
}
|
2018-07-05 15:59:11 +00:00
|
|
|
}
|
2018-07-08 17:32:43 +00:00
|
|
|
return stateChanges;
|
2018-07-05 15:59:11 +00:00
|
|
|
}
|
|
|
|
|
2018-10-17 18:56:30 +00:00
|
|
|
int getNeighbors(int x, int y, int range) {
|
2018-07-05 15:59:11 +00:00
|
|
|
int count = 0;
|
2018-08-30 10:03:23 +00:00
|
|
|
for (int ix = -range + x; ix <= range + x; ix++) {
|
|
|
|
for (int iy = -range + y; iy <= range + y; iy++) {
|
|
|
|
if (ix >= 0 &&
|
|
|
|
iy >= 0 &&
|
2018-08-30 08:57:14 +00:00
|
|
|
ix < map.width &&
|
2018-08-30 10:03:23 +00:00
|
|
|
iy < map.height &&
|
2018-10-16 16:21:01 +00:00
|
|
|
getCellState(ix, iy) == true &&
|
2018-08-30 10:03:23 +00:00
|
|
|
!(x == ix && y == iy)) count++;
|
2018-07-05 15:59:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return count;
|
|
|
|
}
|
2018-07-06 14:47:20 +00:00
|
|
|
|
|
|
|
void render(html.CanvasElement canvas, [num interp]) {
|
2018-07-07 17:08:26 +00:00
|
|
|
// only renders if any cells changed between renders
|
|
|
|
if (!_dirty) return;
|
|
|
|
|
2018-07-06 14:47:20 +00:00
|
|
|
html.CanvasRenderingContext2D ctx = canvas.getContext('2d');
|
2018-08-30 08:57:14 +00:00
|
|
|
int brickW = (canvas.width ~/ map.width);
|
|
|
|
int brickH = (canvas.height ~/ map.height);
|
2018-07-06 14:47:20 +00:00
|
|
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
2018-08-30 10:03:23 +00:00
|
|
|
for (int i = 0; i < map.length; i++) {
|
2018-08-30 08:57:14 +00:00
|
|
|
math.Point p = map.toCoordinates(i);
|
|
|
|
if (_renderEdges) {
|
|
|
|
ctx.setStrokeColorRgb(100, 100, 100);
|
|
|
|
ctx.strokeRect(p.x * brickW, p.y * brickH, brickW, brickH);
|
2018-07-06 14:47:20 +00:00
|
|
|
}
|
2018-08-30 08:57:14 +00:00
|
|
|
|
2018-10-16 16:21:01 +00:00
|
|
|
if (map[i] == true)
|
2018-08-30 08:57:14 +00:00
|
|
|
ctx.setFillColorRgb(155, 155, 255);
|
|
|
|
else
|
|
|
|
ctx.setFillColorRgb(0, 0, 0);
|
|
|
|
ctx.fillRect(p.x * brickW, p.y * brickH, brickW, brickH);
|
2018-07-06 14:47:20 +00:00
|
|
|
}
|
2018-07-07 17:08:26 +00:00
|
|
|
_dirty = false;
|
2018-07-06 14:47:20 +00:00
|
|
|
}
|
2018-07-08 17:01:14 +00:00
|
|
|
|
2018-07-09 15:27:11 +00:00
|
|
|
void set renderEdges(bool on) {
|
|
|
|
_renderEdges = on;
|
2018-07-08 17:01:14 +00:00
|
|
|
_dirty = true;
|
|
|
|
}
|
2018-10-19 17:59:00 +00:00
|
|
|
|
|
|
|
void saveSnapshot() => _snapshot = map;
|
|
|
|
Grid<bool> loadSnapshot() => map = _snapshot;
|
2018-07-05 15:59:11 +00:00
|
|
|
}
|