From 245d9a22c2b844c6d4169ff131cebbdc40913732 Mon Sep 17 00:00:00 2001 From: Unknown Date: Tue, 16 Oct 2018 18:21:01 +0200 Subject: [PATCH] Remove Cell Data Structure Cells are only boolean values of true or false for now. --- lib/src/Simulation.dart | 59 ++++++++++++++++++++++------------------- 1 file changed, 32 insertions(+), 27 deletions(-) diff --git a/lib/src/Simulation.dart b/lib/src/Simulation.dart index 4f6a864..02c3c3a 100644 --- a/lib/src/Simulation.dart +++ b/lib/src/Simulation.dart @@ -1,13 +1,12 @@ import 'dart:html' as html; import 'dart:math' as math; -import 'package:rules_of_living/src/Cell.dart'; import 'package:rules_of_living/src/Grid.dart'; enum CellPattern { SpaceShip, Blinker } class Simulation { - final Grid map; + final Grid map; bool _dirty = true; bool _renderEdges = true; @@ -23,19 +22,12 @@ class Simulation { int get h => map.height; Simulation(int w, int h) : this.map = new Grid(w, h) { - for (int i = 0; i < map.length; i++) { - map[i] = _getGOLCell(); - } - print("Grid creation finished"); - } - - Cell _getGOLCell([bool defaultState = false]) { - Cell cell = Cell(); - return cell; + reset(); + print("Grid Created"); } void reset() { - map.setAll(0, List.filled(map.length, _getGOLCell())); + map.setAll(0, List.filled(map.length, false)); if (_startingSeed != null) addPattern( pattern: _pattern, @@ -105,33 +97,46 @@ class Simulation { } void setCellState(int x, int y, bool state) { - if (y >= map.height || x >= map.width) return; + if (y >= map.height || x >= map.width) return null; - state ? map.set(x, y, Cell()) : map.set(x, y, null); + state ? map.set(x, y, true) : map.set(x, y, false); } bool getCellState(int x, int y) { if (y >= map.height || x >= map.width) return null; - return map.get(x, y) == null ? false : true; + return map.get(x, y); } - Map update() { - Map stateChanges = Map(); + void toggleCellState(int x, int y) { + if (y >= map.height || x >= map.width) return null; + + getCellState(x, y) == null + ? setCellState(x, y, true) + : setCellState(x, y, false); + } + + Map update() { + Map stateChanges = calculateNextState(map); + + stateChanges.forEach((i, el) => map[i] = el); + stateChanges.length != 0 ? _dirty = true : false; + return stateChanges; + } + + Map calculateNextState(Grid oldState) { + Map stateChanges = Map(); for (int i = 0; i < map.length; i++) { math.Point p = map.toCoordinates(i); - Cell el = map[i]; - bool changed = false; + bool cell = map[i]; int neighbors = getSurroundingNeighbors(p.x, p.y, 1); - if (el == null && neighbors == 3) { - stateChanges[i] = Cell(); - } else if (el != null && neighbors != 2 && neighbors != 3) { - stateChanges[i] = null; + if (cell == false && neighbors == 3) { + stateChanges[i] = true; + } else if (cell == true && neighbors != 2 && neighbors != 3) { + stateChanges[i] = false; } } - stateChanges.forEach((i, el) => map[i] = el); - stateChanges.length != 0 ? _dirty = true : false; return stateChanges; } @@ -143,7 +148,7 @@ class Simulation { iy >= 0 && ix < map.width && iy < map.height && - map.get(ix, iy) != null && + getCellState(ix, iy) == true && !(x == ix && y == iy)) count++; } } @@ -165,7 +170,7 @@ class Simulation { ctx.strokeRect(p.x * brickW, p.y * brickH, brickW, brickH); } - if (map[i] != null) + if (map[i] == true) ctx.setFillColorRgb(155, 155, 255); else ctx.setFillColorRgb(0, 0, 0);