= Solution
Count only the existing neighbours, excluding the center, then apply the <Conway Game of Life> transition:
``
java
static boolean nextCell(boolean[][] old, int i, int j) {
int height = old.length;
int width = old[0].length;
int neighbours = 0;
for (int di = -1; di <= 1; di++) {
for (int dj = -1; dj <= 1; dj++) {
if (di == 0 && dj == 0) continue;
int r = i + di;
int c = j + dj;
if (r >= 0 && r < height && c >= 0 && c < width
&& old[r][c]) {
neighbours++;
}
}
}
return neighbours == 3 || (old[i][j] && neighbours == 2);
}
``
The short-circuit bounds test occurs before indexing a neighbour. Out-of-range cells therefore contribute zero, including at corners, and no opposite-edge wrapping occurs. The center is explicitly excluded. \b[Birth requires three neighbours; survival requires two or three.] The method accepts a nonempty rectangular board and a valid cell index and does not change the old <Java array>.
Back to article page