生命游戏ArrayIndexOutofBounds [英] Game of Life ArrayIndexOutofBounds

查看:112
本文介绍了生命游戏ArrayIndexOutofBounds的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在做康威的生活游戏。我很确定我已接近完成,但是当我运行它时,我得到线程中的异常mainjava.lang.ArrayIndexOutOfBoundsException:-1
at game.of.life .GameOfLife.generation(GameOfLife.java:77)
at game.of.life.GameOfLife.main(GameOfLife.java:32)
Java结果:1

I'm doing the Conway's game of life. 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

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

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.

推荐答案

r <$ c时$ c> 0 ,这一个无效:旧[r - 1] [c]

因此你得到你发布的例外。

When r is 0, this one is not valid: old[r - 1][c].
Thus you get the exception you posted.

我建议你这样简化。

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

这篇关于生命游戏ArrayIndexOutofBounds的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆