康威生命游戏

时间:2013-03-06 17:21:37

标签: java arrays boolean

我正在努力撰写康威的生活游戏。现在我代表带有加号的活细胞和带有负号的死亡细胞。我的第一代看起来很好然后我得到了一个越​​界错误。另外,我想在打印时为用户添加生成计数器。这是错误代码

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

        }
    }

2 个答案:

答案 0 :(得分:3)

用于确保坐标(i,j)不在界限范围内的inbounds方法是错误的:

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

答案 1 :(得分:2)

快速观察,你写道:

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

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

我想也许最后的比较应该是:

而不是i。