import 'dart:html' as html; import 'dart:math' as math; import 'package:rules_of_living/src/Cell.dart'; import 'package:rules_of_living/src/Rule.dart'; enum Pattern { SpaceShip, Blinker } class Grid { final int w; final int h; final List> map; bool _dirty = true; Grid(int w, int h) : this.w = w, this.h = h, this.map = new List() { map.addAll(_buildGrid(w, h)); print("Grid creation finished"); } void startingPattern( {Pattern pattern, int x, int y, int amount, int dispersal}) { math.Random rng = new math.Random(); int cx = x ?? rng.nextInt(w ~/ 3) + (w ~/ 3); int cy = y ?? rng.nextInt(h ~/ 3) + (h ~/ 3); switch (pattern) { // Two blocks, offset // ## // ## case Pattern.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 Pattern.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: for (var i = 0; i < amount ?? rng.nextInt(20); i++) { setCellState(cx + dispersal ?? rng.nextInt(10), cy + dispersal ?? rng.nextInt(10), true); } break; } } void setCellState(int x, int y, bool state) { if (y < map.length && x < map[y].length) map[y][x].state = state; } List> _buildGrid(int w, int h) { print("grid being created"); List> grid = new List(h); // GENERAL RULE LAYOUT Rule threeTrue = new Rule((int n) { if (n == 3) return true; return false; }); Rule twoTrue = new Rule((int n) { 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; return false; }); 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(); // cell.surviveRules.add(twoTrue); cell.surviveRules.add(threeTrue); cell.surviveRules.add(twoTrue); cell.birthRules.add(threeTrue); grid[y][x] = cell; } } 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 map[y][x].update(getSurroundingNeighbors(x, y, 1)); if (!_dirty && map[y][x].dirty) _dirty = true; } } 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(); } } } int getSurroundingNeighbors(int x, int y, int range) { int count = 0; for (int iy = y - range; iy <= y + range; iy++) { for (int ix = x - range; ix <= x + range; ix++) { if (ix > 0 && iy > 0 && iy < map.length && ix < map[iy].length && map[iy][ix].state == true && !(x == ix && y == iy)) { count++; } } } return count; } void render(html.CanvasElement canvas, [num interp]) { // only renders if any cells changed between renders if (!_dirty) return; 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; } }