0

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;
    }
Toon Krijthe
  • 52,876
  • 38
  • 145
  • 202
  • Possible Duplicate : http://stackoverflow.com/questions/12743748/find-elements-surrounding-and-element-in-an-array/12743834#12743834 and http://stackoverflow.com/questions/652106/finding-neighbours-in-a-two-dimensional-array/652123#652123 – Rohit Jain Oct 06 '12 at 06:06
  • @RohitJain Thanks, these will help me. Sorry for creating a similar question. – Andrew Pearson Oct 06 '12 at 06:27
  • @AndrewPearson.. Its no problem.. And those are better ways to find `neighbours`. You should change your code completely.. Don't need so much of if-else.. – Rohit Jain Oct 06 '12 at 06:29

3 Answers3

1

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.

PermGenError
  • 45,977
  • 8
  • 87
  • 106
  • Ah I see what I did. Thanks for the explanation. So one possible way to correct this problem is to replace lastgen[0].length with lastgen[0].length-1. – Andrew Pearson Oct 06 '12 at 06:30
1

Are these conditions correct?

      col < 0

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

Cyrgo
  • 31
  • 2
  • 1
    You are correct, thanks for the find. That would have got me next. (I guess this is what I get for trying to write code at midnight) – Andrew Pearson Oct 06 '12 at 06:28
0

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

subodh
  • 6,136
  • 12
  • 51
  • 73