2018-07-06 14:47:20 +00:00
|
|
|
import 'dart:html' as html;
|
2018-07-07 18:02:13 +00:00
|
|
|
import 'dart:math' as math;
|
2018-07-06 14:47:20 +00:00
|
|
|
|
2018-07-07 14:42:30 +00:00
|
|
|
import 'package:rules_of_living/src/Cell.dart';
|
|
|
|
import 'package:rules_of_living/src/Rule.dart';
|
2018-07-05 15:59:11 +00:00
|
|
|
|
2018-07-07 18:49:02 +00:00
|
|
|
enum Pattern { SpaceShip, Blinker }
|
2018-07-07 18:02:13 +00:00
|
|
|
|
2018-07-05 15:59:11 +00:00
|
|
|
class Grid {
|
|
|
|
final int w;
|
|
|
|
final int h;
|
|
|
|
final List<List<Cell>> map;
|
|
|
|
|
2018-07-07 17:08:26 +00:00
|
|
|
bool _dirty = true;
|
2018-07-08 17:01:14 +00:00
|
|
|
bool _renderEdges = true;
|
|
|
|
|
2018-07-07 19:42:07 +00:00
|
|
|
int _startingSeed;
|
|
|
|
int _x;
|
|
|
|
int _y;
|
|
|
|
int _amount;
|
|
|
|
int _dispersal;
|
|
|
|
Pattern _pattern;
|
2018-07-07 17:08:26 +00:00
|
|
|
|
2018-07-05 15:59:11 +00:00
|
|
|
Grid(int w, int h)
|
|
|
|
: this.w = w,
|
|
|
|
this.h = h,
|
|
|
|
this.map = new List() {
|
|
|
|
map.addAll(_buildGrid(w, h));
|
|
|
|
|
2018-07-06 12:27:25 +00:00
|
|
|
print("Grid creation finished");
|
2018-07-05 15:59:11 +00:00
|
|
|
}
|
|
|
|
|
2018-07-07 19:42:07 +00:00
|
|
|
void reset() {
|
|
|
|
map.setAll(0, _buildGrid(w, h));
|
|
|
|
if(_startingSeed != null) addPattern(pattern: _pattern, dispersal: _dispersal,amount: _amount,seed: _startingSeed, x: _x, y: _y);
|
|
|
|
_dirty = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void addPattern(
|
|
|
|
{Pattern 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-07-07 18:49:02 +00:00
|
|
|
int cx = x ?? rng.nextInt(w ~/ 3) + (w ~/ 3);
|
|
|
|
int cy = y ?? rng.nextInt(h ~/ 3) + (h ~/ 3);
|
2018-07-07 18:02:13 +00:00
|
|
|
switch (pattern) {
|
|
|
|
// Two blocks, offset
|
|
|
|
// ##
|
|
|
|
// ##
|
|
|
|
case Pattern.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:02:13 +00:00
|
|
|
|
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);
|
2018-07-07 18:02:13 +00:00
|
|
|
break;
|
|
|
|
// A 'gliding' Spaceship
|
|
|
|
// #
|
|
|
|
// #
|
|
|
|
// ###
|
|
|
|
case Pattern.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);
|
2018-07-07 18:02:13 +00:00
|
|
|
break;
|
|
|
|
default:
|
2018-07-07 19:42:07 +00:00
|
|
|
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;
|
2018-07-07 18:02:13 +00:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2018-07-07 20:02:04 +00:00
|
|
|
_dirty = true;
|
2018-07-07 18:02:13 +00:00
|
|
|
}
|
2018-07-07 18:49:02 +00:00
|
|
|
|
2018-07-07 18:02:13 +00:00
|
|
|
void setCellState(int x, int y, bool state) {
|
2018-07-06 14:47:20 +00:00
|
|
|
if (y < map.length && x < map[y].length) map[y][x].state = state;
|
|
|
|
}
|
|
|
|
|
2018-07-07 19:43:24 +00:00
|
|
|
bool getCellState(int x, int y) {
|
|
|
|
if (y < map.length && x < map[y].length) return map[y][x].state;
|
|
|
|
}
|
|
|
|
|
2018-07-05 15:59:11 +00:00
|
|
|
List<List<Cell>> _buildGrid(int w, int h) {
|
2018-07-06 12:27:25 +00:00
|
|
|
print("grid being created");
|
2018-07-05 15:59:11 +00:00
|
|
|
List<List<Cell>> grid = new List(h);
|
2018-07-06 14:47:20 +00:00
|
|
|
// GENERAL RULE LAYOUT
|
2018-07-05 15:59:11 +00:00
|
|
|
Rule threeTrue = new Rule((int n) {
|
2018-07-06 14:47:20 +00:00
|
|
|
if (n == 3) return true;
|
2018-07-06 13:00:45 +00:00
|
|
|
return false;
|
2018-07-05 15:59:11 +00:00
|
|
|
});
|
|
|
|
Rule twoTrue = new Rule((int n) {
|
2018-07-06 14:47:20 +00:00
|
|
|
if (n == 2) return true;
|
|
|
|
return false;
|
|
|
|
});
|
|
|
|
|
|
|
|
// DEBUG RULE TESTING FOR PATTERNS
|
|
|
|
Rule coagSurvive = new Rule((int n) {
|
2018-07-07 18:49:02 +00:00
|
|
|
if (n == 1) return true;
|
2018-07-06 14:47:20 +00:00
|
|
|
return false;
|
|
|
|
});
|
|
|
|
Rule coagBirth = new Rule((int n) {
|
2018-07-07 18:49:02 +00:00
|
|
|
if (n == 1) return true;
|
2018-07-06 13:00:45 +00:00
|
|
|
return false;
|
2018-07-05 15:59:11 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
for (int y = 0; y < h; y++) {
|
|
|
|
grid[y] = new List(w);
|
|
|
|
for (int x = 0; x < w; x++) {
|
|
|
|
// GIVES RULES FOR CONWAY GAME OF LIFE BY DEFAULT S23/B3
|
|
|
|
Cell cell = new Cell();
|
2018-07-06 14:47:20 +00:00
|
|
|
// cell.surviveRules.add(twoTrue);
|
2018-07-07 18:02:13 +00:00
|
|
|
cell.surviveRules.add(threeTrue);
|
|
|
|
cell.surviveRules.add(twoTrue);
|
|
|
|
cell.birthRules.add(threeTrue);
|
2018-07-05 15:59:11 +00:00
|
|
|
|
2018-07-06 13:00:45 +00:00
|
|
|
grid[y][x] = cell;
|
2018-07-05 15:59:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return grid;
|
|
|
|
}
|
|
|
|
|
|
|
|
void update() {
|
|
|
|
for (int y = 0; y < h; y++) {
|
|
|
|
for (int x = 0; x < w; x++) {
|
|
|
|
// DEFAULTS TO CONWAY GAME OF LIFE RANGE OF ONE
|
2018-07-06 14:47:20 +00:00
|
|
|
map[y][x].update(getSurroundingNeighbors(x, y, 1));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
for (int y = 0; y < h; y++) {
|
|
|
|
for (int x = 0; x < w; x++) {
|
|
|
|
// DEFAULTS TO CONWAY GAME OF LIFE RANGE OF ONE
|
|
|
|
map[y][x].advanceState();
|
2018-07-07 19:43:24 +00:00
|
|
|
if (!_dirty && map[y][x].dirty) _dirty = true;
|
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-07-06 12:27:25 +00:00
|
|
|
for (int iy = y - range; iy <= y + range; iy++) {
|
|
|
|
for (int ix = x - range; ix <= x + range; ix++) {
|
2018-07-06 14:47:20 +00:00
|
|
|
if (ix > 0 &&
|
|
|
|
iy > 0 &&
|
|
|
|
iy < map.length &&
|
|
|
|
ix < map[iy].length &&
|
|
|
|
map[iy][ix].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]) {
|
2018-07-07 17:08:26 +00:00
|
|
|
// 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');
|
|
|
|
int brickW = (canvas.width ~/ map[0].length);
|
|
|
|
int brickH = (canvas.height ~/ map.length);
|
|
|
|
ctx.clearRect(0, 0, canvas.width, canvas.height);
|
2018-07-08 17:01:14 +00:00
|
|
|
|
2018-07-06 14:47:20 +00:00
|
|
|
for (int y = 0; y < map.length; y++) {
|
|
|
|
for (int x = 0; x < map[y].length; x++) {
|
2018-07-08 17:01:14 +00:00
|
|
|
if(_renderEdges) {
|
|
|
|
ctx.setStrokeColorRgb(100, 100, 100);
|
|
|
|
ctx.strokeRect(x * brickW, y * brickH, brickW, brickH);
|
|
|
|
}
|
|
|
|
|
2018-07-06 14:47:20 +00:00
|
|
|
Cell c = map[y][x];
|
|
|
|
if (c.state == true)
|
|
|
|
ctx.setFillColorRgb(155, 155, 255);
|
|
|
|
else
|
|
|
|
ctx.setFillColorRgb(0, 0, 0);
|
|
|
|
ctx.fillRect(x * brickW, y * brickH, brickW, brickH);
|
|
|
|
}
|
|
|
|
}
|
2018-07-07 17:08:26 +00:00
|
|
|
|
|
|
|
_dirty = false;
|
2018-07-06 14:47:20 +00:00
|
|
|
}
|
2018-07-08 17:01:14 +00:00
|
|
|
|
|
|
|
void switchEdgeRendering([bool on]) {
|
|
|
|
_renderEdges = on ?? !_renderEdges;
|
|
|
|
_dirty = true;
|
|
|
|
}
|
2018-07-05 15:59:11 +00:00
|
|
|
}
|