Extract CellPattern and GameOfLife into own files
This commit is contained in:
parent
fbdf114fed
commit
0aa3df30b4
4 changed files with 51 additions and 47 deletions
|
@ -2,6 +2,7 @@ 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';
|
import 'package:rules_of_living/src/rules/RuleSet.dart';
|
||||||
|
|
||||||
enum CellPattern { SpaceShip, Blinker }
|
enum CellPattern { SpaceShip, Blinker }
|
||||||
|
|
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 checkRange = 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;
|
||||||
|
}
|
|
@ -1,54 +1,9 @@
|
||||||
import 'dart:math';
|
import 'package:rules_of_living/src/rules/CellPattern.dart';
|
||||||
|
|
||||||
import 'package:collection/collection.dart';
|
|
||||||
|
|
||||||
abstract class RuleSet {
|
abstract class RuleSet {
|
||||||
int checkRange;
|
int checkRange;
|
||||||
List<Pattern> patterns;
|
List<CellPattern> patterns;
|
||||||
|
|
||||||
bool checkSurvival(int neighbors);
|
bool checkSurvival(int neighbors);
|
||||||
bool checkBirth(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;
|
|
||||||
List<Pattern> patterns = [
|
|
||||||
// Two blocks, offset
|
|
||||||
// ##
|
|
||||||
// ##
|
|
||||||
Pattern("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
|
|
||||||
// #
|
|
||||||
// #
|
|
||||||
// ###
|
|
||||||
Pattern("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;
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue