From 27d4879b1bca3f11cbeb16efae3b295b6a9df9ea Mon Sep 17 00:00:00 2001 From: Unknown Date: Wed, 17 Oct 2018 20:56:30 +0200 Subject: [PATCH] Extract RuleSet Class from Simulation --- lib/src/RuleSet.dart | 27 ++++++++++++++++++++++++++ lib/src/Simulation.dart | 43 ++++++----------------------------------- 2 files changed, 33 insertions(+), 37 deletions(-) create mode 100644 lib/src/RuleSet.dart diff --git a/lib/src/RuleSet.dart b/lib/src/RuleSet.dart new file mode 100644 index 0000000..edfc5a9 --- /dev/null +++ b/lib/src/RuleSet.dart @@ -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 extends DelegatingList { + 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; +} diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart index 02c3c3a..05db54b 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -2,11 +2,13 @@ import 'dart:html' as html; import 'dart:math' as math; import 'package:rules_of_living/src/Grid.dart'; +import 'package:rules_of_living/src/RuleSet.dart'; enum CellPattern { SpaceShip, Blinker } class Simulation { final Grid map; + RuleSet rules = GameOfLife(); bool _dirty = true; bool _renderEdges = true; @@ -28,14 +30,6 @@ class Simulation { void reset() { 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; } @@ -56,31 +50,6 @@ class Simulation { 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); - - 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: int sanityCheck = 0; for (var i = 0; i < (_amount); i++) { @@ -130,17 +99,17 @@ class Simulation { for (int i = 0; i < map.length; i++) { math.Point p = map.toCoordinates(i); bool cell = map[i]; - int neighbors = getSurroundingNeighbors(p.x, p.y, 1); - if (cell == false && neighbors == 3) { + int neighbors = getNeighbors(p.x, p.y, 1); + if (cell == false && rules.checkBirth(neighbors) == true) { stateChanges[i] = true; - } else if (cell == true && neighbors != 2 && neighbors != 3) { + } else if (cell == true && rules.checkSurvival(neighbors) == false) { stateChanges[i] = false; } } return stateChanges; } - int getSurroundingNeighbors(int x, int y, int range) { + int getNeighbors(int x, int y, int range) { int count = 0; for (int ix = -range + x; ix <= range + x; ix++) { for (int iy = -range + y; iy <= range + y; iy++) {