Merge branch '53-encapsulate-ruleset-patterns-in-separate-data-structure' into 'master'
Resolve "Encapsulate Ruleset, Patterns in separate Data Structure" Closes #53 See merge request marty.oehme/cellular-automata!14
This commit is contained in:
commit
b37487a222
7 changed files with 100 additions and 105 deletions
|
@ -1,6 +0,0 @@
|
||||||
import 'package:rules_of_living/src/Rule.dart';
|
|
||||||
|
|
||||||
class Cell {
|
|
||||||
/// For determining if render updates are necessary in [Grid].render() function
|
|
||||||
bool dirty = false;
|
|
||||||
}
|
|
|
@ -32,11 +32,11 @@ class Engine {
|
||||||
/// Grid Size
|
/// Grid Size
|
||||||
///
|
///
|
||||||
/// Number of cells on x coordinate and y coordinate. Can be set individually.
|
/// Number of cells on x coordinate and y coordinate. Can be set individually.
|
||||||
Point get gridSize => Point<int>(_grid.w, _grid.h);
|
Point get gridSize => Point<int>(_simulation.w, _simulation.h);
|
||||||
void set gridSize(Point<int> value) {
|
void set gridSize(Point<int> value) {
|
||||||
if (value.x <= 0 || value.y <= 0)
|
if (value.x <= 0 || value.y <= 0)
|
||||||
throw ArgumentError("grid size must not be smaller than 1");
|
throw ArgumentError("grid size must not be smaller than 1");
|
||||||
_grid = Simulation(value.x, value.y);
|
_simulation = Simulation(value.x, value.y);
|
||||||
}
|
}
|
||||||
|
|
||||||
num _updateLag = 0.0;
|
num _updateLag = 0.0;
|
||||||
|
@ -48,14 +48,14 @@ 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 _grid;
|
Simulation _simulation;
|
||||||
bool running = false;
|
bool running = false;
|
||||||
|
|
||||||
Engine([x = 100, y = 100, this.canvas]) {
|
Engine([x = 100, y = 100, this.canvas]) {
|
||||||
_grid = Simulation(x, y);
|
_simulation = Simulation(x, y);
|
||||||
|
|
||||||
_elapsed.start();
|
_elapsed.start();
|
||||||
_grid.addPattern(amount: 15, dispersal: 5);
|
_simulation.addRandomPattern(amount: 15, dispersal: 5);
|
||||||
html.window.animationFrame.then(animFrame);
|
html.window.animationFrame.then(animFrame);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -65,12 +65,12 @@ class Engine {
|
||||||
}
|
}
|
||||||
|
|
||||||
void reset() {
|
void reset() {
|
||||||
_grid.reset();
|
_simulation.reset();
|
||||||
running = false;
|
running = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
void clear() {
|
void clear() {
|
||||||
_grid = new Simulation(gridSize.x, gridSize.y);
|
_simulation = new Simulation(gridSize.x, gridSize.y);
|
||||||
running = false;
|
running = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -101,8 +101,10 @@ class Engine {
|
||||||
/// 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() {
|
||||||
// TODO: create hasUpdated/hasAdvanced method in simulation to abstract actual updating away
|
Map<int, bool> simulationUpdate = _simulation.update();
|
||||||
if (_grid.update().length == 0) running = false;
|
_simulation.mergeStateChanges(simulationUpdate);
|
||||||
|
|
||||||
|
if (simulationUpdate.length == 0) running = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Advances Logic One Update
|
/// Advances Logic One Update
|
||||||
|
@ -111,8 +113,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
|
||||||
|
@ -121,26 +123,16 @@ 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) _grid.render(canvas, interp);
|
if (canvas == null) return;
|
||||||
|
|
||||||
|
_simulation.render(canvas, interp);
|
||||||
}
|
}
|
||||||
|
|
||||||
void addPattern(
|
void addPattern({int amount, int dispersal}) {
|
||||||
{CellPattern pattern,
|
_simulation.addRandomPattern(amount: amount, dispersal: dispersal);
|
||||||
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() {
|
void toggleEdgeRendering() {
|
||||||
_grid.renderEdges = !_grid.renderEdges;
|
_simulation.renderEdges = !_simulation.renderEdges;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +0,0 @@
|
||||||
class Rule {
|
|
||||||
final Function evaluate;
|
|
||||||
|
|
||||||
Rule(this.evaluate);
|
|
||||||
}
|
|
|
@ -2,21 +2,20 @@ import 'dart:html' as html;
|
||||||
import 'dart:math' as math;
|
import 'dart:math' as math;
|
||||||
|
|
||||||
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/rules/RuleSet.dart';
|
||||||
|
|
||||||
enum CellPattern { SpaceShip, Blinker }
|
enum CellPattern { SpaceShip, Blinker }
|
||||||
|
|
||||||
class Simulation {
|
class Simulation {
|
||||||
final Grid<bool> map;
|
final Grid<bool> map;
|
||||||
|
RuleSet rules = GameOfLife();
|
||||||
|
|
||||||
bool _dirty = true;
|
bool _dirty = true;
|
||||||
bool _renderEdges = true;
|
bool _renderEdges = true;
|
||||||
|
|
||||||
int _startingSeed;
|
|
||||||
int _x;
|
|
||||||
int _y;
|
|
||||||
int _amount;
|
int _amount;
|
||||||
int _dispersal;
|
int _dispersal;
|
||||||
CellPattern _pattern;
|
|
||||||
|
|
||||||
int get w => map.width;
|
int get w => map.width;
|
||||||
int get h => map.height;
|
int get h => map.height;
|
||||||
|
@ -28,71 +27,27 @@ class Simulation {
|
||||||
|
|
||||||
void reset() {
|
void reset() {
|
||||||
map.setAll(0, List.filled(map.length, false));
|
map.setAll(0, List.filled(map.length, false));
|
||||||
if (_startingSeed != null)
|
|
||||||
addPattern(
|
|
||||||
pattern: _pattern,
|
|
||||||
dispersal: _dispersal,
|
|
||||||
amount: _amount,
|
|
||||||
seed: _startingSeed,
|
|
||||||
x: _x,
|
|
||||||
y: _y);
|
|
||||||
_dirty = true;
|
_dirty = true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void addPattern(
|
void addRandomPattern({int amount, int dispersal}) {
|
||||||
{CellPattern pattern,
|
int _startingSeed = DateTime.now().millisecondsSinceEpoch;
|
||||||
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;
|
||||||
_pattern = pattern;
|
int cx = rng.nextInt(map.width ~/ 3) + (map.width ~/ 3);
|
||||||
int cx = x ?? rng.nextInt(map.width ~/ 3) + (map.width ~/ 3);
|
int cy = rng.nextInt(map.height ~/ 3) + (map.height ~/ 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);
|
|
||||||
|
|
||||||
setCellState(cx + 2, cy + 2, true);
|
int sanityCheck = 0;
|
||||||
setCellState(cx + 3, cy + 2, true);
|
for (var i = 0; i < (_amount); i++) {
|
||||||
setCellState(cx + 2, cy + 3, true);
|
sanityCheck++;
|
||||||
setCellState(cx + 3, cy + 3, true);
|
getCellState(cx, cy)
|
||||||
break;
|
? i--
|
||||||
// A 'gliding' Spaceship
|
: setCellState(
|
||||||
// #
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -118,29 +73,31 @@ class Simulation {
|
||||||
|
|
||||||
Map<int, bool> update() {
|
Map<int, bool> update() {
|
||||||
Map<int, bool> stateChanges = calculateNextState(map);
|
Map<int, bool> stateChanges = calculateNextState(map);
|
||||||
|
|
||||||
stateChanges.forEach((i, el) => map[i] = el);
|
|
||||||
stateChanges.length != 0 ? _dirty = true : false;
|
|
||||||
return stateChanges;
|
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> calculateNextState(Grid<bool> oldState) {
|
||||||
Map<int, bool> stateChanges = Map();
|
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];
|
bool cell = map[i];
|
||||||
int neighbors = getSurroundingNeighbors(p.x, p.y, 1);
|
int neighbors = getNeighbors(p.x, p.y, rules.range);
|
||||||
if (cell == false && neighbors == 3) {
|
if (cell == false && rules.checkBirth(neighbors) == true) {
|
||||||
stateChanges[i] = true;
|
stateChanges[i] = true;
|
||||||
} else if (cell == true && neighbors != 2 && neighbors != 3) {
|
} else if (cell == true && rules.checkSurvival(neighbors) == false) {
|
||||||
stateChanges[i] = false;
|
stateChanges[i] = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return stateChanges;
|
return stateChanges;
|
||||||
}
|
}
|
||||||
|
|
||||||
int getSurroundingNeighbors(int x, int y, int range) {
|
int getNeighbors(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++) {
|
||||||
|
|
10
lib/src/rules/CellPattern.dart
Normal file
10
lib/src/rules/CellPattern.dart
Normal file
|
@ -0,0 +1,10 @@
|
||||||
|
import 'package:collection/collection.dart';
|
||||||
|
|
||||||
|
class CellPattern<Point> extends DelegatingList<Point> {
|
||||||
|
final String _name;
|
||||||
|
CellPattern(String name, List base)
|
||||||
|
: _name = name,
|
||||||
|
super(base);
|
||||||
|
|
||||||
|
String get name => _name;
|
||||||
|
}
|
38
lib/src/rules/GameOfLife.dart
Normal file
38
lib/src/rules/GameOfLife.dart
Normal file
|
@ -0,0 +1,38 @@
|
||||||
|
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;
|
||||||
|
}
|
9
lib/src/rules/RuleSet.dart
Normal file
9
lib/src/rules/RuleSet.dart
Normal file
|
@ -0,0 +1,9 @@
|
||||||
|
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);
|
||||||
|
}
|
Loading…
Reference in a new issue