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: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<Cell> map;
|
||||
final Grid<bool> 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<int, Cell> update() {
|
||||
Map<int, Cell> 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<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++) {
|
||||
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);
|
||||
|
|
Loading…
Reference in a new issue