コードの次のpiceは私の生命のための私のコードです。何らかの理由で、それは非常に奇妙に動作します。ゲームの最初の数ステップは間違っており、出力全体がゼロになります。どの方法が問題を引き起こしているのか分かりませんが、それは数の隣人法だと思います。Conwayのゲームのゲームは働いていない数の隣人の方法は働いていない
ボードはGOLBoard
と呼ばれる2D配列で、x座標とy座標はcellRow
とcellCol
です。生存細胞は1であり、死細胞は0である。範囲外の問題を回避する方法は、ボードを12x12にすることですが、行と列には1から11しか使用しません。
<code> @Override
public int countNeighbours(int cellRow, int cellCol) {
int neighbours = 0;
for (int i = cellRow-1; i < cellRow + 2; i++) {
for (int j = cellCol - 1; j < cellCol + 2; j++) {
if (GOLBoard[i][j] == 1) {
neighbours += 1;
}
}
}
if (GOLBoard[cellRow][cellCol] == 1) {
return(neighbours-1);
} else {
return(neighbours);
}
}
@Override
public int applyRules(int cellRow, int cellCol) {
int alive = 0;
if (GOLBoard[cellRow][cellCol] == 1) {
if (countNeighbours(cellRow, cellCol) == 2 || countNeighbours(cellRow, cellCol) == 3) {
alive = 1;
} else if (countNeighbours(cellRow, cellCol) < 2 || countNeighbours(cellRow, cellCol) > 3) {
alive = 0;
}
}
if (GOLBoard[cellRow][cellCol] == 0) {
if (countNeighbours(cellRow, cellCol) == 3) {
alive = 1;
}
}
return (alive);
}
@Override
public void takeStep() {
for (int row = 1; row < 11; row++) {
for (int col = 1; col < 11; col++) {
GOLBoard[row][col] = applyRules(row, col);
}
}
}
@Override
public String toString() {
for (int row = 1; row < 11; row++) {
for (int col = 1; col < 11; col++) {
System.out.print("[" + GOLBoard[row][col] + "]");
}
System.out.println("");
}
return("");
}
} <code>