gpt4 book ai didi

java - 康威的生命游戏调试 (Java)

转载 作者:行者123 更新时间:2023-12-01 11:33:36 24 4
gpt4 key购买 nike

我试图让我的康威生命游戏正确运行,但我不断得到不正确的结果,而且我似乎无法找出问题所在。这是我执行生命游戏各代的代码:

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

我尝试编写此程序的方式是否存在根本性错误?

最佳答案

在您的 check() 方法中,您将 row col 处的方 block 包含在总数中,而它应该忽略它。

关于java - 康威的生命游戏调试 (Java),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30225154/

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