gpt4 book ai didi

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

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:37:04 25 4
gpt4 key购买 nike

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

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();
}
}

最佳答案

你用

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.

关于java - 我已经开始用 Java 编写生命游戏,但周围单元格的计数不正确,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40207000/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com