我目前正在介绍 Java 类编程,但遇到了一两个问题。
首先,我目前的程序是“康威生命游戏”,除了检查相邻单元格的算法外,我已经完成了所有工作。
我在 Stackoverflow 上查看了大约 7 或 8 个不同的帖子,在其他网站上查看了一些帖子,到目前为止,我找不到任何采用我所拥有的方法的人。
现在我只需要有人来查看我的代码,看看它是否应该工作,如果不能工作,为什么不呢?目前我收到一个运行时错误,上面写着:
“ArrayIndexOutOfBoundsException: 5 at Life.checkold(Life.java:151)”。
我已经尽我所能在代码中标记了这个地方。
我对这些方法的输入是一个大小为 5 x 5 的数组
首先,感谢您至少阅读本文。其次,如果我需要包含任何其他内容,请发表评论或回复(这里是新的,抱歉)
构建下一代:
public static boolean[][] buildnext(boolean[][] lastgen)
{
boolean[][] nextgen = new boolean[lastgen.length][lastgen[0].length];
for(int r = 0; r < lastgen[0].length; r++)
{
for(int c = 0; c < lastgen.length; c++)
{
nextgen[c][r] = checkold(lastgen, c, r);
}
}
return nextgen;
}
我的检查方法:
public static boolean checkold(boolean[][] lastgen, int col, int row)
{
int acount = 0;
boolean alive = lastgen[col][row];
if(col == 0 && row == 0) //Top Left Corner
{
if(lastgen[col][row + 1] == true) acount++; //Below
if(lastgen[col + 1][row] == true) acount++; //Right
if(lastgen[col + 1][row + 1] == true) acount++; //Below Right
}
else if(col == lastgen.length && row == 0)//Top Right Corner
{
if(lastgen[col][row + 1] == true) acount++; //Below
if(lastgen[col - 1][row] == true) acount++; //Left
if(lastgen[col - 1][row + 1] == true) acount++; //Below Left
}
else if(col == 0 && row == lastgen[0].length)//Bottom Left Corner
{
if(lastgen[col][row - 1] == true) acount++; //Above
if(lastgen[col + 1][row] == true) acount++; //Right
if(lastgen[col + 1][row - 1] == true) acount++; //Above Right
}
else if(col == lastgen.length && row == lastgen[0].length) //Bottom Right Corner
{
if(lastgen[col][row - 1] == true) acount++; //Above
if(lastgen[col - 1][row] == true) acount++; //Left
if(lastgen[col - 1][row - 1] == true) acount++; //Above Left
}
else if(col == 0 && row > 0 && row < lastgen[0].length) //Left Col
{
if(lastgen[col][row - 1] == true) acount++; //Above
if(lastgen[col][row + 1] == true) acount++; //Below (This is the code that the runtime error is about)
if(lastgen[col + 1][row] == true) acount++; //Right
if(lastgen[col + 1][row - 1] == true) acount++; //Above Right
if(lastgen[col + 1][row + 1] == true) acount++; //Below Right
}
else if(col == lastgen.length && row > 0 && row < lastgen[0].length) //Right Col
{
if(lastgen[col - 1][row] == true) acount++; //Left
if(lastgen[col][row - 1] == true) acount++; //Above
if(lastgen[col][row + 1] == true) acount++; //Below
if(lastgen[col - 1][row - 1] == true) acount++; //Above Left
if(lastgen[col - 1][row + 1] == true) acount++; //Below Left
}
else if(col < 0 && row == 0) //Top Row
{
if(lastgen[col][row + 1] == true) acount++; //Below
if(lastgen[col - 1][row] == true) acount++; //Left
if(lastgen[col + 1][row] == true) acount++; //Right
if(lastgen[col - 1][row + 1] == true) acount++; //Below Left
if(lastgen[col + 1][row + 1] == true) acount++; //Below Right
}
else if(col < 0 && row == lastgen[0].length) //Bottom Row
{
if(lastgen[col][row - 1] == true) acount++; //Above
if(lastgen[col - 1][row] == true) acount++; //Left
if(lastgen[col + 1][row] == true) acount++; //Right
if(lastgen[col - 1][row - 1] == true) acount++; //Above Left
if(lastgen[col + 1][row - 1] == true) acount++; //Above Right
}
else if(col < 0 && row < 0) //Middle Cells
{
if(lastgen[col][row + 1] == true) acount++; //Below
if(lastgen[col][row - 1] == true) acount++; //Above
if(lastgen[col - 1][row] == true) acount++; //Left
if(lastgen[col + 1][row] == true) acount++; //Right
if(lastgen[col - 1][row - 1] == true) acount++; //Above Left
if(lastgen[col + 1][row - 1] == true) acount++; //Above Right
if(lastgen[col - 1][row + 1] == true) acount++; //Below Left
if(lastgen[col + 1][row + 1] == true) acount++; //Below Right
}
if(acount == 3 && alive == false) alive = true;
if(acount == 1) alive = false;
if(acount == 3 && alive == true) alive = false;
return alive;
}