-4

我正在尝试写康威的人生游戏。现在我用加号表示活细胞,用负号表示死细胞。我的第一代看起来不错,然后出现越界错误。此外,我想在打印用户时为用户添加生成计数器。这是错误代码

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 10
at Life.Neighbors(Life.java:56)
at Life.nextGen(Life.java:35)
at Life.main(Life.java:18)

这是到目前为止的代码

import java.util.Scanner;

public class Life {

    public static boolean[][] gen(){
        boolean[][] matrix = new boolean[10][10];
        for(int i = 0; i < matrix.length; i++)
            for(int j = 0; j < matrix.length; j++)
                if( Math.random() > 0.7 )
                    matrix[i][j] = true;
        return matrix;
    }

    public static void main(String[] args){
        boolean[][] world = gen();
        show(world);
        System.out.println();
        world = nextGen(world);
        show(world);
        Scanner s = new Scanner(System.in);
        while(s.nextLine().length() == 0){
            System.out.println();
            world = nextGen(world);
            show(world);

        }
    }

    public static boolean[][] nextGen(boolean[][] world){
        boolean[][] newWorld 
            = new boolean[world.length][world[0].length];
        int num;
        for(int i = 0; i < world.length; i++){
            for(int j = 0; j < world[0].length; j++){
                num = Neighbors(world, i, j);
                if( occupiedNext(num, world[i][j]) )
                    newWorld[i][j] = true;
            }
        }
        return newWorld;
    }

    public static boolean occupiedNext(int Neighbors, boolean occupied){
        if( occupied && (Neighbors == 2 || Neighbors == 3))
            return true;
        else if (!occupied && Neighbors == 3)
            return true;
        else
            return false;
    }

    public static int Neighbors(boolean[][] world, int row, int col) {
        int num = world[row][col] ? -1 : 0;
        for(int i = row - 1; i <= row + 1; i++)
            for(int j = col - 1; j <= col + 1; j++)
                if( inbounds(world, i, j) && world[i][j] )
                    num++;

        return num;
    }

    public static boolean inbounds(boolean[][] world, int i, int j) {
        return i >= 0 && i < world.length && j >= 0 &&
        i < world[0].length;
    }
    public static void show(boolean[][] matrix){
        String s = "";
        for(boolean[] row : matrix){
            for(boolean val : row)
                if(val)
                    s += "+";
                else
                    s += "-";
            s += "\n";
        }
        System.out.println(s);

        }
    }
4

2 回答 2

3

inbounds您用来确保坐标 (i,j) 没有超出范围的方法是错误的:

public static boolean inbounds(boolean[][] world, int i, int j) {
    return i >= 0 && i < world.length && j >= 0 && i < world[0].length;
}

事实上,在最后一次检查中,您正在与 比较iworld[0].length当它应该是j

public static boolean inbounds(boolean[][] world, int i, int j) {
    return i >= 0 && i < world.length && j >= 0 && j < world[0].length;
}
于 2013-03-06T17:27:46.767 回答
2

快速观察,您写道:

public static boolean inbounds(boolean[][] world, int i, int j) {
        return i >= 0 && i < world.length && j >= 0 &&
        i < world[0].length;

我认为也许最终的比较应该是:

... && j < world[0].length;

而不是我。

于 2013-03-06T17:30:50.823 回答