我已经开始在 Java 中编写生命游戏,但周围单元格的计数不正确

I have started writing the Game of Life in Java and the count of surrounding cells is not correct

我是 Java 编程的初学者(第一学期),我一直在为生命游戏编写代码。我正在计算二维数组中每个单元格的周围单元格。我已经达到程序编译良好的地步,但是当我用不同大小的数组测试它时,我可以看到单元格的计数不正确,尽管随后单元格状态的交换是正确执行的,并且我不知道这是怎么回事。请帮助:

public static void surrounding(boolean[][] around) {
    for (int i = 0;  i < around.length; i++)
        for (int j = 0; j < around[i].length; j++) {
            int minRow = i == 0 ? i : i - 1;
            int maxRow = i == (around[i].length - 1) ? around[i].length - 1 : i + 1;
            int minCol = j == 0 ? j : j - 1;
            int maxCol = j == (around[i].length - 1) ? around[i].length - 1 : j + 1;
            int count = 0;
            for (int a = minRow; a <= maxRow; a++)
                for (int b = minCol; b <= maxCol; b++) {
                    if ((around[a][b]) && !(a == i && b == j)) 
                        count++;                      

                }
            System.out.print(count + " ");

            if ((around [i][j]) && (count < 2 || count > 3))
                around[i][j] = false;
            else if (!(around[i][j]) && (count == 3))
                around[i][j] = true;

        }
    System.out.println();
    for (int row = 0; row < around.length; row++) {
        for(int column = 0; column < around[row].length; column++) {
            if (around[row][column])
              System.out.print("X ");
            else
              System.out.print(". ");
        }
        System.out.println();
    }
}

这是到目前为止的整个程序:

public static void main(String[] args) {

    boolean[][] world = randomBools(3);
    for (int row = 0; row < world.length; row++) {
        for(int column = 0; column < world[row].length; column++) {
            if (world[row][column])
              System.out.print("X ");
            else
              System.out.print(". ");
        }
        System.out.println();
    }

    System.out.println();
    surrounding(world);

}

public static boolean[][] randomBools(int len) {
    Random random = new Random();
    boolean[][] arr = new boolean[len][len];
    for(int i = 0; i < len; i++)
        for(int j = 0; j < arr[i].length; j++)
          arr[i][j] = random.nextBoolean();

    return arr;
}

public static void surrounding(boolean[][] around) {
    for (int i = 0;  i < around.length; i++)
        for (int j = 0; j < around[i].length; j++) {
            int minRow = i == 0 ? i : i - 1;
            int maxRow = i == (around[i].length - 1) ? around[i].length - 1 : i + 1;
            int minCol = j == 0 ? j : j - 1;
            int maxCol = j == (around[i].length - 1) ? around[i].length - 1 : j + 1;
            int count = 0;
            for (int a = minRow; a <= maxRow; a++)
                for (int b = minCol; b <= maxCol; b++) {
                    if ((around[a][b]) && !(a == i && b == j)) 
                        count++;                      

                }
            System.out.print(count + " ");

            if ((around [i][j]) && (count < 2 || count > 3))
                around[i][j] = false;
            else if (!(around[i][j]) && (count == 3))
                around[i][j] = true;

        }
    System.out.println();
    for (int row = 0; row < around.length; row++) {
        for(int column = 0; column < around[row].length; column++) {
            if (around[row][column])
              System.out.print("X ");
            else
              System.out.print(". ");
        }
        System.out.println();
    }
}

}

for (int a = minRow; a <= maxRow; a++)
            for (int b = minCol; b <= maxCol; b++) {
                if ((around[a][b]) && !(a == i && b == j)) 
                    count++;                      
            }
        System.out.print(count + " ");

你也计算当前单元格。您应该将其更改为:

if(a!=0&&b!=0) count++;

您使用

int minRow = i == 0 ? i : i - 1;
int maxRow = i == (around[i].length - 1) ? around[i].length - 1 : i + 1;
int minCol = j == 0 ? j : j - 1; 
int maxCol = j == (around[i].length - 1) ? around[i].length - 1 : j + 1; 

,但必须是

int minRow= Math.max(0,i-1);
int maxRow= Math.min(around.length-1,i+1);
int minCol= Math.max(0,j-1);
int maxCol = Math.min(around[i].length-1,j+1);

因为 around 是一个边长相等的 n*n 矩阵,所以你可以使用

int maxCol=Math.min(around.length-1,j+1);

您还可以将 maxRowminRow 一个 for 循环放在更高的位置(以 i 作为计数器的循环),因为 maxRow 和 minRow 对于相同的 i.