简体   繁体   English

生命游戏ArrayIndexOutofBounds

[英]Game of Life ArrayIndexOutofBounds

I'm doing the Conway's game of life. 我在做Conway的生活游戏。 I'm pretty sure I'm close to finished, but when I run it, I get 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 我敢肯定我快要完成了,但是当我运行它时,我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得到了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

I'm assuming when the method that checks neighbors at the edges of the array, there's nothing there so it dies or something. 我假设当检查数组边缘的邻居的方法时,那里什么都没有,所以它死了。 I just don't know how to make it so that doesn't happen. 我只是不知道如何做到这一点,所以不会发生。 Does anyone have any thoughts? 有人有想法吗? Code below. 下面的代码。

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

If anyone can help me out it would be much appreciated. 如果有人可以帮助我,将不胜感激。

When r is 0 , this one is not valid: old[r - 1][c] . r0 ,此无效: old[r - 1][c]
Thus you get the exception you posted. 这样就得到了发布的异常。

I suggest you simplify it like this. 我建议您像这样简化它。

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

As I can see, you basically have two choices: 如我所见,您基本上有两个选择:

  1. apply finite bounds, that is, for the cells in the first and last columns and rows, you implement an additional check when counting the number of 'living' neighbours. 应用有限范围,即对于第一列和最后一列和最后一行中的单元格,在计算“活着的”邻居的数量时,您将执行附加检查。

  2. apply periodic bounds, that is, the cells on the leftmost column and the cells on the rightmost column are considered as neighbours. 应用周期边界,即,最左边一列的单元格和最右边一列的单元格被视为邻居。 With the help of modular arithmetic, these cells don't need to be handled separately from others. 借助模块化算法,这些单元无需与其他单​​元分开处理。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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