我的节目是康威人生游戏,规则是:
任何活细胞,如果活的邻居少于两个,就会像underpopulation.
我能够在一行中检查活邻居--我的问题是检查列中的活邻居,查看下一代在检查列时是活的还是死的,下面的代码演示了我得到了什么,其中1表示活的单元格,0表示死的单元格。
public static int[][] nextGeneration(int inputGrid[][]) {
int height = 10, width = 10;
int[][] future = new int[height][width];
for (int x = 1; x < height - 1; x++) {
for (int y = 1; y < width - 1; y++) {
int aliveNeighbours = 0;
for (int i = -1; i <= 1; i++)
for (int j = -1; j <= 1; j++)
aliveNeighbours += inputGrid[x + i][y + j];
aliveNeighbours -= inputGrid[x][y];
if ((inputGrid[x][y] == 1) && (aliveNeighbours < 2))
future[x][y] = 0;
else if (((inputGrid[x][y] == 1) && ((aliveNeighbours ==2) || (aliveNeighbours ==3))))
future[x][y] = 1;
else if ((inputGrid[x][y] == 1) && (aliveNeighbours > 3))
future[x][y] = 0;
else if ((inputGrid[x][y] == 0) && (aliveNeighbours == 3))
future[x][y] = 1;
else
future[x][y] = inputGrid[x][y];
}
}
return future;
}
public static void main(String[] args) {
int[][] game =
{{0, 0, 1, 1, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 1, 0}
};
System.out.println(nextGeneration(game));
}我的结果是:
{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0}
};但预期结果应是:
{{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 1, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 1, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 1}细胞10*10在我的结果应该是活着,而不是死亡
发布于 2022-01-26 19:37:23
循环的一个主要缺陷是Java数组(与大多数数组一样)有一个基于零的索引。但是,循环以x和y设置为1开始,因此根本不处理第一行。此外,在处理最后一行之前,循环条件将停止。此代码更正了该错误:
public static int[][] nextGeneration(int inputGrid[][]) {
int height = 10, width = 10;
int[][] future = new int[height][width];
// iterate over each row (start with 0 because an array index is 0 based)
for (int x = 0; x < height; x++) {
// iterate over each column (start with 0 as well)
for (int y = 0; y < width; y++) {
int aliveNeighbours = 0;
int rowAbove = Math.max(x -1, 0); // the row above is x-1 but never less than 0 because that row doesn't exist
int rowBelow = Math.min(x + 1, height - 1); // the row below is never greater than the last row in the array (height - 1)
int colLeft = Math.max(y -1, 0); // go to the left one column, unless we are at the edge, then don't go past 0
int colRight = Math.min(y + 1, width - 1); // ... continuing the same logic as above
for (int rowToCheck = rowAbove; rowToCheck <= rowBelow; rowToCheck++)
for (int colToCheck = colLeft; colToCheck <= colRight; colToCheck++)
aliveNeighbours += inputGrid[rowToCheck][colToCheck];
// remove the cell being evaluated from the neighbors count
aliveNeighbours -= inputGrid[x][y];
// simplified logic to remove unnecessary conditions
// any cell with three neighbors is alive (past value doesn't matter)
if (aliveNeighbours == 3)
future[x][y] = 1;
// any cell with fewer than two live neighbors is dead (past value doesn't matter)
else if (aliveNeighbours < 2)
future[x][y] = 0;
// any cell with more than three neighbors is dead (past value doesn't matter)
else if (aliveNeighbours >= 4)
future[x][y] = 0;
// any cell with two neighbors remains in its present state (regardless of what the past value was)
else if (aliveNeighbours == 2)
future[x][y] = inputGrid[x][y];
else
throw new RuntimeException("Unhandled neighbor condition");
}
}
return future;
}
public static void main(String[] args) {
int[][] game =
{{0, 0, 1, 1, 1, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 0, 1, 1},
{0, 0, 0, 0, 0, 0, 0, 0, 1, 0}
};
var nextGen = nextGeneration(game);
for(var row : nextGen)
{
for(var cell : row)
{
System.out.print(cell);
}
System.out.println();
}https://stackoverflow.com/questions/70827342
复制相似问题