[英]Java Conway Game of life
我的節目是康威人生游戲,規則是:
我能夠在一行中檢查活着的鄰居我的問題是檢查列中的活着的鄰居,並在檢查列時查看下一代是生是死,下面的代碼演示了我得到的結果,其中 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 應該是活的而不是死的
您的循環的主要缺陷之一是 Java arrays (像大多數)具有從零開始的索引。 但是,您的循環從 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();
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.