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 13:00:45 +00:00
|
|
|
map[5][6].state = true;
|
|
|
|
map[5][7].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;
|
2018-07-06 13:00:45 +00:00
|
|
|
return false;
|
2018-07-05 15:59:11 +00:00
|
|
|
});
|
|
|
|
Rule twoTrue = new Rule((int n) {
|
|
|
|
if(n==2) return true;
|
2018-07-06 13:00:45 +00:00
|
|
|
return false;
|
2018-07-05 15:59:11 +00:00
|
|
|
});
|
|
|
|
|
|
|
|
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);
|
2018-07-06 13:00:45 +00:00
|
|
|
cell.birthRules.add(threeTrue);
|
2018-07-05 15:59:11 +00:00
|
|
|
|
2018-07-06 13:00:45 +00:00
|
|
|
grid[y][x] = cell;
|
2018-07-05 15:59:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|