Remove Cell Data Structure
Cells are only boolean values of true or false for now.
This commit is contained in:
parent
71f4df85af
commit
245d9a22c2
1 changed files with 32 additions and 27 deletions
|
@ -1,13 +1,12 @@
|
||||||
import 'dart:html' as html;
|
import 'dart:html' as html;
|
||||||
import 'dart:math' as math;
|
import 'dart:math' as math;
|
||||||
|
|
||||||
import 'package:rules_of_living/src/Cell.dart';
|
|
||||||
import 'package:rules_of_living/src/Grid.dart';
|
import 'package:rules_of_living/src/Grid.dart';
|
||||||
|
|
||||||
enum CellPattern { SpaceShip, Blinker }
|
enum CellPattern { SpaceShip, Blinker }
|
||||||
|
|
||||||
class Simulation {
|
class Simulation {
|
||||||
final Grid<Cell> map;
|
final Grid<bool> map;
|
||||||
|
|
||||||
bool _dirty = true;
|
bool _dirty = true;
|
||||||
bool _renderEdges = true;
|
bool _renderEdges = true;
|
||||||
|
@ -23,19 +22,12 @@ class Simulation {
|
||||||
int get h => map.height;
|
int get h => map.height;
|
||||||
|
|
||||||
Simulation(int w, int h) : this.map = new Grid(w, h) {
|
Simulation(int w, int h) : this.map = new Grid(w, h) {
|
||||||
for (int i = 0; i < map.length; i++) {
|
reset();
|
||||||
map[i] = _getGOLCell();
|
print("Grid Created");
|
||||||
}
|
|
||||||
print("Grid creation finished");
|
|
||||||
}
|
|
||||||
|
|
||||||
Cell _getGOLCell([bool defaultState = false]) {
|
|
||||||
Cell cell = Cell();
|
|
||||||
return cell;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void reset() {
|
void reset() {
|
||||||
map.setAll(0, List.filled(map.length, _getGOLCell()));
|
map.setAll(0, List.filled(map.length, false));
|
||||||
if (_startingSeed != null)
|
if (_startingSeed != null)
|
||||||
addPattern(
|
addPattern(
|
||||||
pattern: _pattern,
|
pattern: _pattern,
|
||||||
|
@ -105,33 +97,46 @@ class Simulation {
|
||||||
}
|
}
|
||||||
|
|
||||||
void setCellState(int x, int y, bool state) {
|
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) {
|
bool getCellState(int x, int y) {
|
||||||
if (y >= map.height || x >= map.width) return null;
|
if (y >= map.height || x >= map.width) return null;
|
||||||
|
|
||||||
return map.get(x, y) == null ? false : true;
|
return map.get(x, y);
|
||||||
}
|
}
|
||||||
|
|
||||||
Map<int, Cell> update() {
|
void toggleCellState(int x, int y) {
|
||||||
Map<int, Cell> stateChanges = Map();
|
if (y >= map.height || x >= map.width) return null;
|
||||||
|
|
||||||
|
getCellState(x, y) == null
|
||||||
|
? setCellState(x, y, true)
|
||||||
|
: setCellState(x, y, false);
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<int, bool> update() {
|
||||||
|
Map<int, bool> stateChanges = calculateNextState(map);
|
||||||
|
|
||||||
|
stateChanges.forEach((i, el) => map[i] = el);
|
||||||
|
stateChanges.length != 0 ? _dirty = true : false;
|
||||||
|
return stateChanges;
|
||||||
|
}
|
||||||
|
|
||||||
|
Map<int, bool> calculateNextState(Grid<bool> oldState) {
|
||||||
|
Map<int, bool> stateChanges = Map();
|
||||||
|
|
||||||
for (int i = 0; i < map.length; i++) {
|
for (int i = 0; i < map.length; i++) {
|
||||||
math.Point p = map.toCoordinates(i);
|
math.Point p = map.toCoordinates(i);
|
||||||
Cell el = map[i];
|
bool cell = map[i];
|
||||||
bool changed = false;
|
|
||||||
int neighbors = getSurroundingNeighbors(p.x, p.y, 1);
|
int neighbors = getSurroundingNeighbors(p.x, p.y, 1);
|
||||||
if (el == null && neighbors == 3) {
|
if (cell == false && neighbors == 3) {
|
||||||
stateChanges[i] = Cell();
|
stateChanges[i] = true;
|
||||||
} else if (el != null && neighbors != 2 && neighbors != 3) {
|
} else if (cell == true && neighbors != 2 && neighbors != 3) {
|
||||||
stateChanges[i] = null;
|
stateChanges[i] = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
stateChanges.forEach((i, el) => map[i] = el);
|
|
||||||
stateChanges.length != 0 ? _dirty = true : false;
|
|
||||||
return stateChanges;
|
return stateChanges;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -143,7 +148,7 @@ class Simulation {
|
||||||
iy >= 0 &&
|
iy >= 0 &&
|
||||||
ix < map.width &&
|
ix < map.width &&
|
||||||
iy < map.height &&
|
iy < map.height &&
|
||||||
map.get(ix, iy) != null &&
|
getCellState(ix, iy) == true &&
|
||||||
!(x == ix && y == iy)) count++;
|
!(x == ix && y == iy)) count++;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -165,7 +170,7 @@ class Simulation {
|
||||||
ctx.strokeRect(p.x * brickW, p.y * brickH, brickW, brickH);
|
ctx.strokeRect(p.x * brickW, p.y * brickH, brickW, brickH);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (map[i] != null)
|
if (map[i] == true)
|
||||||
ctx.setFillColorRgb(155, 155, 255);
|
ctx.setFillColorRgb(155, 155, 255);
|
||||||
else
|
else
|
||||||
ctx.setFillColorRgb(0, 0, 0);
|
ctx.setFillColorRgb(0, 0, 0);
|
||||||
|
|
Loading…
Reference in a new issue