My question is boolean isLive = false; why is this assigned as false? I have seen very similer examples but I never quet understand it. could anyone explain what this line is doing?
/**
* Method that counts the number of live cells around a specified cell
* @param board 2D array of booleans representing the live and dead cells
* @param row The specific row of the cell in question
* @param col The specific col of the cell in question
* @returns The number of live cells around the cell in question
*/
public static int countNeighbours(boolean[][] board, int row, int col)
{
int count = 0;
for (int i = row-1; i <= row+1; i++) {
for (int j = col-1; j <= col+1; j++) {
// Check all cells around (not including) row and col
if (i != row || j != col) {
if (checkIfLive(board, i, j) == LIVE) {
count++;
}
}
}
}
return count;
}
/**
* Returns if a given cell is live or dead and checks whether it is on the board
*
* @param board 2D array of booleans representing the live and dead cells
* @param row The specific row of the cell in question
* @param col The specific col of the cell in question
*
* @returns Returns true if the specified cell is true and on the board, otherwise false
*/
private static boolean checkIfLive (boolean[][] board, int row, int col) {
boolean isLive = false;
int lastRow = board.length-1;
int lastCol = board[0].length-1;
if ((row >= 0 && row <= lastRow) && (col >= 0 && col <= lastCol)) {
isLive = board[row][col];
}
return isLive;
}
That’s simply the default value, which may be changed if the test (
ifclause) is verified.It defines the convention that cells outside the board aren’t live.
This could have been written as :