Fix Neighbor Search

This commit is contained in:
Marty Oehme 2018-07-06 14:27:25 +02:00
parent 4c40dd2baa
commit e14e8fe505

View file

@ -1,5 +1,5 @@
import 'package:rules_of_living/Cell.dart'; import 'Cell.dart';
import 'package:rules_of_living/Rule.dart'; import 'Rule.dart';
class Grid { class Grid {
final int w; final int w;
@ -13,9 +13,11 @@ class Grid {
map.addAll(_buildGrid(w, h)); map.addAll(_buildGrid(w, h));
map[5][5].state = true; map[5][5].state = true;
print("Grid creation finished");
} }
List<List<Cell>> _buildGrid(int w, int h) { List<List<Cell>> _buildGrid(int w, int h) {
print("grid being created");
List<List<Cell>> grid = new List(h); List<List<Cell>> grid = new List(h);
Rule threeTrue = new Rule((int n) { Rule threeTrue = new Rule((int n) {
if(n==3) return true; if(n==3) return true;
@ -45,17 +47,18 @@ class Grid {
for (int y = 0; y < h; y++) { for (int y = 0; y < h; y++) {
for (int x = 0; x < w; x++) { for (int x = 0; x < w; x++) {
// DEFAULTS TO CONWAY GAME OF LIFE RANGE OF ONE // DEFAULTS TO CONWAY GAME OF LIFE RANGE OF ONE
map[y][x].update( getNeighbors(x, y, 1) ); map[y][x].update( getSurroundingNeighbors(x, y, 1) );
} }
} }
} }
int getNeighbors(int x, int y, int range) { int getSurroundingNeighbors(int x, int y, int range) {
int count = 0; int count = 0;
for (int iy = y - range ~/ 2; iy < iy + range / 2; iy++) { for (int iy = y - range; iy <= y + range; iy++) {
for (int ix = x - range ~/ 2; ix < ix + range / 2; ix++) { for (int ix = x - range; ix <= x + range; ix++) {
if (iy > 0 && iy < map.length && ix > 0 && ix < map[iy].length && if (ix > 0 && iy > 0 && iy < map.length && ix < map[iy].length && map[iy][ix].state == true && !(x == ix && y == iy)) {
map[iy][ix].state == true) count++; count++;
}
} }
} }
return count; return count;