简体   繁体   English

康威的生命游戏

[英]Conway’s Game of Life

My question is boolean isLive = false; 我的问题是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 ( if clause) is verified. 这只是默认值, if验证了test( if子句),则可以更改默认值。

It defines the convention that cells outside the board aren't live. 它定义了电路板外的单元不存在的惯例。

This could have been 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;
    }
}

First we assign boolean value as 'false' (ensuring default condition) 首先我们将布尔值指定为'false'(确保默认条件)
Then if valid condition is found we change the value, else default false will be returned. 然后,如果找到有效条件,我们更改值,否则将返回默认值false。

boolean isLive = false;

This is the default value of a boolean variable. 这是布尔变量的默认值。 If you declare as an instance variable it is automatically initialized to false. 如果声明为实例变量,则会自动初始化为false。

why is this assigned as false? 为什么这被指定为假?

Well, we do this, just to start with a default value, then we can change later on to true value, based on certain condition. 好吧,我们这样做,只是以默认值开始,然后我们可以根据某些条件稍后更改为true值。

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

So, in above code, if your if condition is false, then it is similar to returning a false value. 因此,在上面的代码中,如果if条件为false,则类似于返回false值。 Because, the variable isLive is not changed. 因为,变量isLive不会改变。

But if your condition is true, then the return value will depend upon the value of board[row][col] . 但是如果你的条件为真,那么return value将取决于board[row][col] If it is false , return value will still be false , else true . 如果为false ,则返回值仍为false ,否则为true

boolean isLive = false;

It is a default value assigned to the boolean variable. 它是分配给布尔变量的默认值。

Just like: 就像:

int num = 0;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM