I'm currently in an intro to programming in Java class and I've ran into a problem or two.
First off my currently program is "Conway's Game of Life" I've got everything working except for the algorithm to check the neighboring cells.
I've checked out about 7 or 8 different post on Stackoverflow and a couple on other sites and so far I can't find anyone who has taken the approach I have.
Right now all I need is someone to look over my code and see if it should work and if not why not? Currently I'm getting a runtime error that says:
"ArrayIndexOutOfBoundsException: 5 at Life.checkold(Life.java:151)".
I've marked this place in the code the best I could.
My input to this these methods are an array of size 5 by 5
First off, thank you for at least reading this. Second if I need to include anything else please just leave a comment or reply (new here sorry)
Building NextGen:
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;
}
My Checking Method:
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;
}