Andrew Pearson
Andrew Pearson

Reputation: 3

Game of life Algorithm/ Structure of if else statements

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

Upvotes: 0

Views: 1700

Answers (3)

subodh
subodh

Reputation: 6158

Error because of two lines in a same block

 1. if(lastgen[col][row + 1] == true) acount++;
 2. if(lastgen[col + 1][row + 1] == true) acount++;

suppose your lastgen element size is 4 and you are trying to access with [row+1] which is obviously 5

Edit

put else if(col == 0 && row > 0 && row < lastgen[0].length-1)

instead of else if(col == 0 && row > 0 && row < lastgen[0].length)

array.length-1

Upvotes: 0

Cyrgo
Cyrgo

Reputation: 31

Are these conditions correct?

      col < 0

It seems to me that you should evaluate for "col > 0"

Upvotes: 1

PermGenError
PermGenError

Reputation: 46428

your exception is quite obvious, you are trying to accessan array index which is out of bound.

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)

your code fails on this line:

if(lastgen[col][row + 1] == true) 

say lastgen array is 2X3 array and row = 2, else if part would be true, and consider if is also true, now in here

if(lastgen[col][row + 1] == true) 

youa re trying to access 3 index, remember: Array indexes start with zero. if your arrays length is 3, your array index will be 0,1,2. 2 will the last index.

Upvotes: 1

Related Questions