cellular-automata/lib/src/Grid.dart

137 lines
3.4 KiB
Dart
Raw Normal View History

2018-07-06 14:47:20 +00:00
import 'dart:html' as html;
import 'package:rules_of_living/src/Cell.dart';
import 'package:rules_of_living/src/Rule.dart';
2018-07-05 15:59:11 +00:00
class Grid {
final int w;
final int h;
final List<List<Cell>> map;
bool _dirty = true;
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 14:47:20 +00:00
// BLINKER
// map[5][5].state = true;
// map[5][6].state = true;
// map[6][5].state = true;
// map[6][6].state = true;
//
// map[7][7].state = true;
// map[7][8].state = true;
// map[8][7].state = true;
// map[8][8].state = true;
// SPACESHIP
setState(1 + 5, 0 + 5, true);
setState(2 + 5, 1 + 5, true);
setState(2 + 5, 2 + 5, true);
setState(1 + 5, 2 + 5, true);
setState(0 + 5, 2 + 5, true);
2018-07-06 12:27:25 +00:00
print("Grid creation finished");
2018-07-05 15:59:11 +00:00
}
2018-07-06 14:47:20 +00:00
void setState(int x, int y, bool state) {
if (y < map.length && x < map[y].length) map[y][x].state = 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) {
if (n==1) return true;
return false;
});
Rule coagBirth = new Rule((int n) {
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);
cell.surviveRules.add(coagSurvive);
cell.birthRules.add(coagBirth);
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));
if (!_dirty && map[y][x].dirty) _dirty = true;
2018-07-06 14:47:20 +00:00
}
}
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-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]) {
// 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);
for (int y = 0; y < map.length; y++) {
for (int x = 0; x < map[y].length; x++) {
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);
}
}
_dirty = false;
2018-07-06 14:47:20 +00:00
}
2018-07-05 15:59:11 +00:00
}