cellular-automata/lib/src/Simulation.dart

188 lines
5 KiB
Dart
Raw Normal View History

2018-07-06 14:47:20 +00:00
import 'dart:html' as html;
import 'dart:math' as math;
2018-07-06 14:47:20 +00:00
import 'package:rules_of_living/src/Cell.dart';
2018-08-30 08:57:14 +00:00
import 'package:rules_of_living/src/Grid.dart';
import 'package:rules_of_living/src/Rule.dart';
2018-07-05 15:59:11 +00:00
enum CellPattern { SpaceShip, Blinker }
class Simulation {
2018-08-30 08:57:14 +00:00
final Grid<Cell> map;
2018-07-05 15:59:11 +00:00
bool _dirty = true;
2018-07-08 17:01:14 +00:00
bool _renderEdges = true;
int _startingSeed;
int _x;
int _y;
int _amount;
int _dispersal;
CellPattern _pattern;
int get w => map.width;
int get h => map.height;
2018-08-30 10:03:23 +00:00
Simulation(int w, int h) : this.map = new Grid(w, h) {
for (int i = 0; i < map.length; i++) {
map[i] = _getGOLCell();
}
2018-07-06 12:27:25 +00:00
print("Grid creation finished");
2018-07-05 15:59:11 +00:00
}
2018-08-30 10:03:23 +00:00
Cell _getGOLCell([bool defaultState = false]) {
2018-08-30 08:57:14 +00:00
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;
});
2018-08-30 10:03:23 +00:00
cell.surviveRules.add(twoTrue);
cell.surviveRules.add(threeTrue);
cell.birthRules.add(threeTrue);
2018-08-30 08:57:14 +00:00
return cell;
}
void reset() {
2018-08-30 08:57:14 +00:00
map.setAll(0, List.filled(map.length, _getGOLCell()));
2018-07-08 17:05:11 +00:00
if (_startingSeed != null)
addPattern(
pattern: _pattern,
dispersal: _dispersal,
amount: _amount,
seed: _startingSeed,
x: _x,
y: _y);
_dirty = true;
}
void addPattern(
2018-08-23 10:38:34 +00:00
{CellPattern pattern,
int x,
int y,
int amount,
int dispersal,
int seed}) {
_startingSeed = seed ?? DateTime.now().millisecondsSinceEpoch;
math.Random rng = new math.Random(_startingSeed);
_x = x;
_y = y;
_amount = amount ?? rng.nextInt(20);
_dispersal = dispersal ?? 10;
_pattern = pattern;
2018-08-30 08:57:14 +00:00
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:
2018-07-07 18:35:04 +00:00
setCellState(cx, cy, true);
2018-07-07 18:49:02 +00:00
setCellState(cx + 1, cy, true);
setCellState(cx, cy + 1, true);
setCellState(cx + 1, cy + 1, true);
2018-07-07 18:49:02 +00:00
setCellState(cx + 2, cy + 2, true);
setCellState(cx + 3, cy + 2, true);
setCellState(cx + 2, cy + 3, true);
setCellState(cx + 3, cy + 3, true);
break;
// A 'gliding' Spaceship
// #
// #
// ###
case CellPattern.SpaceShip:
2018-07-07 18:35:04 +00:00
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);
2018-07-08 17:05:11 +00:00
if (sanityCheck > 100 && sanityCheck > i * 3) break;
}
break;
}
_dirty = true;
}
2018-07-07 18:49:02 +00:00
void setCellState(int x, int y, bool state) {
2018-08-30 08:57:14 +00:00
if (y < map.height && x < map.width) map.get(x, y).state = state;
2018-07-06 14:47:20 +00:00
}
bool getCellState(int x, int y) {
2018-08-30 08:57:14 +00:00
if (y < map.height && x < map.width) return map.get(x, y).state;
2018-07-08 17:03:48 +00:00
return null;
}
Map<int, Cell> update() {
Map<int, Cell> stateChanges = Map();
2018-08-30 08:57:14 +00:00
for (int i = 0; i < map.length; i++) {
math.Point p = map.toCoordinates(i);
Cell el = map[i];
el.update(getSurroundingNeighbors(p.x, p.y, 1));
stateChanges[i] = el;
2018-07-05 15:59:11 +00:00
}
stateChanges.forEach((_, el) => el.advanceState());
stateChanges.length != 0 ? _dirty = true : false;
return stateChanges;
2018-07-05 15:59:11 +00:00
}
2018-07-06 12:27:25 +00:00
int getSurroundingNeighbors(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 &&
map.get(ix, iy).state == true &&
!(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]) {
// 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
if (map[i].state == true)
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
}
_dirty = false;
2018-07-06 14:47:20 +00:00
}
2018-07-08 17:01:14 +00:00
void set renderEdges(bool on) {
_renderEdges = on;
2018-07-08 17:01:14 +00:00
_dirty = true;
}
2018-08-23 10:38:34 +00:00
bool get renderEdges => _renderEdges;
2018-07-05 15:59:11 +00:00
}