Extract RuleSet Class from Simulation

This commit is contained in:
Unknown 2018-10-17 20:56:30 +02:00
parent 07f176be3e
commit 27d4879b1b
2 changed files with 33 additions and 37 deletions

27
lib/src/RuleSet.dart Normal file
View file

@ -0,0 +1,27 @@
import 'dart:math';
import 'package:collection/collection.dart';
abstract class RuleSet {
int checkRange;
bool checkSurvival(int neighbors);
bool checkBirth(int neighbors);
}
class Pattern<Point> extends DelegatingList<Point> {
final String _name;
Pattern(String name, List base)
: _name = name,
super(base);
String get name => _name;
}
class GameOfLife implements RuleSet {
int checkRange = 1;
bool checkSurvival(int neighbors) =>
neighbors == 2 || neighbors == 3 ? true : false;
bool checkBirth(int neighbors) => neighbors == 3 ? true : false;
}

View file

@ -2,11 +2,13 @@ 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/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;
@ -28,14 +30,6 @@ 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;
} }
@ -56,31 +50,6 @@ class Simulation {
int cx = x ?? rng.nextInt(map.width ~/ 3) + (map.width ~/ 3); int cx = x ?? rng.nextInt(map.width ~/ 3) + (map.width ~/ 3);
int cy = y ?? rng.nextInt(map.height ~/ 3) + (map.height ~/ 3); int cy = y ?? rng.nextInt(map.height ~/ 3) + (map.height ~/ 3);
switch (pattern) { 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);
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:
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: default:
int sanityCheck = 0; int sanityCheck = 0;
for (var i = 0; i < (_amount); i++) { for (var i = 0; i < (_amount); i++) {
@ -130,17 +99,17 @@ class Simulation {
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, 1);
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++) {