Conway game of life

My question is boolean isLive = false; why is it assigned as false? I have seen simulator examples, but I never understand it. can 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; } 
+4
source share
4 answers

This is just the default value, which can be changed if the check ( if ) is checked.

It defines an agreement that cells outside the board are not alive.

This could be written as:

 private static boolean checkIfLive (boolean[][] board, int row, int col) { int lastRow = board.length-1; int lastCol = board[0].length-1; if ((row >= 0 && row <= lastRow) && (col >= 0 && col <= lastCol)) { return board[row][col]; } else { return false; } } 
+4
source

First, we set boolean to false (the default condition)
Then, if a valid condition is found, we will change the value, otherwise false will be returned.

+1
source
 boolean isLive = false; 

This is the default value for a boolean variable. If you declare an instance variable, it is automatically initialized to false.

why is it assigned as false?

Well, we do this, just to start with the default value, then we can change the true value later, based on a certain condition.

 if ((row >= 0 && row <= lastRow) && (col >= 0 && col <= lastCol)) { isLive = board[row][col]; } return isLive; 

So, in the above code, if your if condition is false, then it is like returning false . Since the variable isLive does not change.

But if your condition is true, then the return value will depend on the value of board[row][col] . If it is false , the return value will be false , else true .

+1
source
 boolean isLive = false; 

This is the default value assigned to a boolean variable.

As well as:

 int num = 0; 
+1
source

Source: https://habr.com/ru/post/1439349/


All Articles