/** * Count how many 'live' neighbors are at a given cell (i,j). * * @param i, j two integers giving the coordinates of the cell * * @return the number of living cells among the eight neighbors as * an integer */ private int livingNeighbors(int i, int j) { int result = 0; if (isAlive(i-1, j-1)) result++; if (isAlive(i-1, j)) result++; if (isAlive(i-1, j+1)) result++; if (isAlive(i, j-1)) result++; if (isAlive(i, j+1)) result++; if (isAlive(i+1, j-1)) result++; if (isAlive(i+1, j)) result++; if (isAlive(i+1, j+1)) result++; return result; } /** * Determine if a cell is live. Here we assume the board is represented * by a 2-D boolean array. If the cell holds a value 'true', the cell * is live; otherwise the cell is dead. * * @param i, j two integers giving the coordinates of the cell * * @return true if the cell is live, false otherwise */ private boolean isAlive(int i, int j) { if (i >= 0 && i < mWorld.length && j >= 0 && j < mWorld[i].length && mWorld[i][j]) return true; else return false; }