0
我試圖讓康威的生命遊戲正常運行,但我一直得到不正確的結果,而且我似乎無法弄清楚問題所在。這裏是我的代碼執行生命遊戲的一代:康威的生命調試遊戲(Java)
public void generate(int gen)
{
generations = gen;
int count = 0;
for (int x = 0; x < generations; x++)
{
//Copies array to temp
for (int row = 0; row < 20; row++)
{
for (int col = 0; col < 20; col++)
{
temp[row][col] = mat[row][col];
}
}
//Gets count of living organisms surrounding
for (int row = 0; row < 20; row++)
{
for (int col = 0; col < 20; col++)
{
count = check(row, col);
//determines life or death
if (temp[row][col] == false)
{
if (count == 3)
{
mat[row][col] = true;
}
}
else
{
if (count > 3 || count < 2)
{
mat[row][col] = false;
}
}
}
}
}
displayGrid();
}
//Checks the number of living organisms in adjacent cells
public int check(int row, int col)
{
int count = 0;
for (int r = -1; r < 2; r++)
{
for (int c = -1; c < 2; c++)
{
if (isLegal((row + r),(col + c)) && temp[row + r][col + c] == true)
{
count++;
}
}
}
return count;
}
//Checks whether an adjacent space is in the array
public boolean isLegal(int row, int col)
{
if (row > 19 || row < 0 || col > 19 || col < 0)
{
return false;
}
return true;
}
有什麼根本性的錯誤,我想這個程序代碼的方式?
您是否附加了一個調試器並遍歷執行流程? – Kon
另請參閱顯示典型GoL行爲的[工作版本](http://stackoverflow.com/a/8200046/418556)。 –