生命游戏ArrayIndexOutofBounds

时间:2014-03-22 23:07:05

标签: java arrays methods multidimensional-array

我正在做Conway的生活游戏。我很确定我已接近完成,但当我运行它时,我得到Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: -1 at game.of.life.GameOfLife.generation(GameOfLife.java:77) at game.of.life.GameOfLife.main(GameOfLife.java:32) Java Result: 1

我假设当检查阵列边缘的邻居的方法时,那里什么都没有,所以它就会死亡或什么的。我只是不知道如何做到这一点并没有发生。有人有想法吗?代码如下。

package game.of.life;
import java.util.Scanner;
public class GameOfLife {
static boolean[][] current = new boolean[10][10];
static boolean[][] old = new boolean[10][10];
static int population = 10;
public static void main(String[] args) {
    String a = " @ ";
    String b = " ' ";
    int choice = 9;
    int gencount = 0;
    Scanner input = new Scanner(System.in);
    System.out.print("Choose population density. i.e. 10 = 10%: ");
    population = input.nextInt();
    populate();
    copy();
    for(int r = 0; r < current.length; r++){
        for(int c = 0; c < current[r].length; c++){
            if(current[r][c] == true){
                System.out.print(a);
            }
            else
                System.out.print(b);
        }
        System.out.println();
    }
    System.out.print("Generation " + gencount + ".");
    while(choice != 0){
        System.out.print("Make a selection: 1 - Advance Generation 0 - Exit");
        choice = input.nextInt();
        if(choice == 1){
            generation();
            for(int r = 0; r < current.length; r++){
                for(int c = 0; c < current[r].length; c++){
                    if(current[r][c] == true){
                        System.out.print(a);
                    }
                    else
                        System.out.print(b);
                }
                System.out.println();
            }
            copy();
            gencount += 1;
            System.out.println("Generation" + gencount + ".");
        }
    }
}
private static void generation(){
    for(int r = 0; r < old.length; r++){
        for(int c = 0; c < old[r].length; c++){
            if (old[r][c] == true){
                int neighbors = 0;
                if(old[r + 1][c] == true)
                    neighbors += 1;
                if(old[r - 1][c] == true)
                    neighbors += 1;
                if(old[r][c + 1] == true)
                    neighbors += 1;
                if(old[r][c - 1] == true)
                    neighbors += 1;
                if(old[r + 1][c + 1] == true)
                    neighbors += 1;
                if(old[r + 1][c - 1] == true)
                    neighbors += 1;
                if(old[r - 1][c - 1] == true)
                    neighbors += 1;
                if(old[r - 1][c + 1] == true)
                    neighbors += 1;
                if(neighbors != 3 || neighbors != 2)
                    current[r][c] = false;
            }
            else if(old[r][c] == false){
                int neighbors = 0;
                if(old[r + 1][c] == true)
                    neighbors += 1;
                if(old[r - 1][c] == true)
                    neighbors += 1;
                if(old[r][c + 1] == true)
                    neighbors += 1;
                if(old[r][c - 1] == true)
                    neighbors += 1;
                if(old[r + 1][c + 1] == true)
                    neighbors += 1;
                if(old[r + 1][c - 1] == true)
                    neighbors += 1;
                if(old[r - 1][c - 1] == true)
                    neighbors += 1;
                if(old[r - 1][c + 1] == true)
                    neighbors += 1;
                if(neighbors == 3)
                    current[r][c] = true;
            }
        }
    }
}
private static void populate(){
    for(int r = 0; r < current.length; r++){
        for(int c = 0; c < current[r].length; c++){
            int q = (int)(Math.random() * 100);
            if(q < population){
                current[r][c] = true;
            }
            else{
                current[r][c] = false;
            }
        }
    }
}
private static void copy(){
    for(int r = 0; r < old.length; r++){
        for(int c = 0; c < old[r].length; c++)
            old[r][c] = current[r][c];
    }
}
}

如果有人能帮助我,我将不胜感激。

2 个答案:

答案 0 :(得分:3)

r0时,此项无效:old[r - 1][c] 这样你就得到了你发布的例外。

我建议你这样简化它。

    boolean isValidPosition(int r, int c){
        return 
            0 <= r && r < N &&
            0 <= c && c < M;

    }

    int getNeighboursCount(boolean[][] old, int r, int c){
        int neighbors = 0;
        for (int i=-1; i<=1; i++){
            for (int j=-1; j<=1; j++){
                if (i!=0 || j!=0){
                    if (isValidPosition(r + i, c + j)){
                        if(old[r + i][c + j])
                        {
                            neighbors++;
                        }
                    }
                }
            }
        }
        return neighbors;
    }

答案 1 :(得分:1)

我可以看到,你基本上有两个选择:

  1. 应用有限边界,即对于第一列和最后一列和第一行中的单元格,在计算“生活”邻居的数量时执行额外的检查。

  2. 应用周期性边界,即最左侧列上的单元格和最右侧列上的单元格被视为邻居。在模块化算术的帮助下,这些单元格不需要与其他单元格分开处理。