简体   繁体   中英

Creating Life game in java

I am making a life game.

The following code is meant to check how many neighbours that a point has.

public int neighbours(int x, int y){
    int result = 0;
if(x!=0 && y!=0 && y!=getHeight() && x!= getLength()){
    if (life[x - 1][y + 1] == 1)
        result++;
    else if (life[x][y + 1] == 1)
        result++;
    else if (life[x + 1][y + 1] == 1)
        result++;
    else if (life[x + 1][y] == 1)
        result++;
    else if (life[x + 1][y - 1] == 1)
        result++;
    else if (life[x][y - 1] == 1)
        result++;
    else if (life[x - 1][y - 1] == 1)
        result++;
    else if (life[x - 1][y] == 1)
        result++;
} else if(x==0 && y==0){
    if (life[x][y + 1] == 1)
        result++;
    else if (life[x + 1][y + 1] == 1)
        result++;
    else if (life[x + 1][y] == 1)
        result++;
}
else if(x==0 && y==getHeight()){
    if (life[x + 1][y] == 1)
        result++;
    else if (life[x + 1][y - 1] == 1)
        result++;
    else if (life[x][y - 1] == 1)
        result++;
}
else if(y==getHeight() && x==getLength()){
    if (life[x][y - 1] == 1)
        result++;
    else if (life[x - 1][y - 1] == 1)
        result++;
    else if (life[x - 1][y] == 1)
        result++;
}
else if(y==0 && x==getLength()){
    if (life[x - 1][y + 1] == 1)
        result++;
    else if (life[x][y + 1] == 1)
        result++;
    else if (life[x - 1][y] == 1)
        result++;
}
else if (x==0){
    if (life[x][y + 1] == 1)
        result++;
    else if (life[x + 1][y + 1] == 1)
        result++;
    else if (life[x + 1][y] == 1)
        result++;
    else if (life[x + 1][y - 1] == 1)
        result++;
    else if (life[x][y - 1] == 1)
        result++;
}
else if(y==getHeight()){
    if (life[x + 1][y] == 1)
        result++;
    else if (life[x + 1][y - 1] == 1)
        result++;
    else if (life[x][y - 1] == 1)
        result++;
    else if (life[x - 1][y - 1] == 1)
        result++;
    else if (life[x - 1][y] == 1)
        result++;
}
else if(x==getLength()){
    if (life[x - 1][y + 1] == 1)
        result++;
    else if (life[x][y + 1] == 1)
        result++;
    else if (life[x][y - 1] == 1)
        result++;
    else if (life[x - 1][y - 1] == 1)
        result++;
    else if (life[x - 1][y] == 1)
        result++;
}
else if(y==0 && x!=getLength() && x!=0){
    if (life[x - 1][y + 1] == 1)
        result++;
    else if (life[x][y + 1] == 1)
        result++;
    else if (life[x + 1][y + 1] == 1)
        result++;
    else if (life[x + 1][y] == 1)
        result++;
    else if (life[x - 1][y] == 1)
        result++;
}
    return result;
}

As you can see, I have tried to address all exceptions but the program keeps giving me the following error:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 30
at LifeGrid.neighbours(LifeGrid.java:165)
at LifeGrid.run(LifeGrid.java:179)
at TestLife.main(TestLife.java:15)

Do you guys have any idea why the out of bounds exception still happens when I treated all possible exceptions.

You need to check < getHeight/Width(), not ==. If it's == getHeight or getWidth it will be out of bounds.

Your checks for y == getHeight() need to be y == getHeight() - 1 , because that's the value you'll have when you're on the bottom edge of the board. Also, your checks for x == getLength() need to be x == getLength() - 1 , because that's the value you'll have when you're on the right hand edge of the board.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM