cellular-automata/lib/Grid.dart

67 lines
1.6 KiB
Dart
Raw Normal View History

2018-07-06 12:27:25 +00:00
import 'Cell.dart';
import 'Rule.dart';
2018-07-05 15:59:11 +00:00
class Grid {
final int w;
final int h;
final List<List<Cell>> map;
Grid(int w, int h)
: this.w = w,
this.h = h,
this.map = new List() {
map.addAll(_buildGrid(w, h));
map[5][5].state = true;
2018-07-06 12:27:25 +00:00
print("Grid creation finished");
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);
Rule threeTrue = new Rule((int n) {
if(n==3) return true;
else return false;
});
Rule twoTrue = new Rule((int n) {
if(n==2) return true;
else 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.birthRules.add(twoTrue);
grid[y][x] = new 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
2018-07-06 12:27:25 +00:00
map[y][x].update( getSurroundingNeighbors(x, y, 1) );
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++) {
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;
}
}