生命游戏失败的java

时间:2016-10-21 07:36:51

标签: java conways-game-of-life

我和我在学校的研究伙伴在康威生命游戏的任务上遇到了麻烦。

我们的程序出现故障并查看代码我发现我们的代码没有任何问题,在查看错误消息甚至调试功能时,我仍然无法找出它出现故障的原因。

错误消息如下:

  

线程中的异常" main" java.lang.ArrayIndexOutOfBoundsException:   36在nl.ru.ai.exercise6.tryout.nextGeneration(tryout.java:84)at   nl.ru.ai.exercise6.tryout.evolve(tryout.java:138)at   nl.ru.ai.exercise6.tryout.main(tryout.java:27)

使用调试功能时,它表示第84行存在问题,nextUniverse[i][j] = Cell.DEAD;有人可能请查看我们的代码并说明为什么我们的代码在该特定点上不起作用。我知道我们的代码很长,但我们还是

public static void main(String[] args) throws IOException {
    System.out.print("Enter input file:");
    Scanner scanner = new Scanner(System.in);
    String fileName = scanner.nextLine();
    System.out.print("For how many generations do you want your program to run?");
    for (int row = 0; row < 40; row++) {
        for (int col = 1; col < 60; col++) {
    int numberEvolve = scanner.nextInt();
    Cell[][] universe = readUniverseFile(fileName);
    showUniverse(universe);
    evolve(universe, numberEvolve, 2, fileName, row, col);}}
}

static Cell[][] readUniverseFile(String fileName) throws IOException {
    Cell[][] universe = new Cell[40][60];
    try {
        BufferedReader in = new BufferedReader(new FileReader(fileName));

        try {
            for (int i = 0; i <= 39; i++) {
                String line = in.readLine();
                if (line == null) {
                    throw new IllegalArgumentException("Input file does not contain 40 lines");
                }
                if (line.length() != 60) {
                    throw new IllegalArgumentException(
                            "Input file contains a line which doesn't contain 60 characters");
                }
                for (int j = 0; j < 60; j++) {
                    if (line.charAt(j) == '*') {
                        universe[i][j] = Cell.LIVE;
                    } else if (line.charAt(j) == '.') {
                        universe[i][j] = Cell.DEAD;
                    } else if (line.charAt(j) != '.') {
                        throw new IllegalArgumentException("Input file contains invalid character");
                    } else if (line.charAt(j) != '*') {
                        throw new IllegalArgumentException("Input file contains invalid character");
                    }

                }
            }
            in.close();
        } catch (FileNotFoundException e) {
            throw new IllegalArgumentException("Input file could not be found");
        }

    } catch (FileNotFoundException e) {
        throw new IllegalArgumentException("Input file could not be found");
    }
    return universe;
}

private static void showUniverse(Cell[][] universe) {
    for (int row = 0; row <= 39; row++) {
        for (int col = 0; col <= 59; col++) {
            updateScreen(row, col, universe[row][col]);
        }
    }
}

private static Cell[][] nextGeneration(Cell[][] universe, String fileName, int row, int col) throws IOException {   
            Cell[][] nextUniverse = new Cell[row][col];
            for (int i = 1; i <= 38; i++) {
                for (int j = 1; j <= 58; j++) {
                    int livingNeighbours = numberLivingNeighbours(universe);
                    if (universe[i][j] == Cell.LIVE) {
                        if (livingNeighbours < 2 || livingNeighbours > 3) {
                            nextUniverse[i][j] = Cell.DEAD;
                        } else {
                            nextUniverse[i][j] = Cell.LIVE;
                        }
                    } else {
                        if (livingNeighbours == 3) {
                            nextUniverse[i][j] = Cell.LIVE;
                        }
                    }

                }
            }
    return nextUniverse;
}

private static int numberLivingNeighbours(Cell[][] universe) {
    int numberLivingNeighbours = 0;
    for (int row = 1; row < 39; row++) {
        for (int col = 1; col < 59; col++) {
            if (universe[row + 1][col] == Cell.LIVE) {
                numberLivingNeighbours++;
            }
            if (universe[row + 1][col - 1] == Cell.LIVE) {
                numberLivingNeighbours++;
            }
            if (universe[row + 1][col + 1] == Cell.LIVE) {
                numberLivingNeighbours++;
            }
            if (universe[row][col - 1] == Cell.LIVE) {
                numberLivingNeighbours++;
            }
            if (universe[row][col + 1] == Cell.LIVE) {
                numberLivingNeighbours++;
            }
            if (universe[row - 1][col] == Cell.LIVE) {
                numberLivingNeighbours++;
            }
            if (universe[row - 1][col - 1] == Cell.LIVE) {
                numberLivingNeighbours++;
            }
            if (universe[row - 1][col + 1] == Cell.LIVE) {
                numberLivingNeighbours++;
            } else {
                numberLivingNeighbours--;
            }
        }
    }
    return numberLivingNeighbours;
}

private static void evolve(Cell[][] universe, int numberEvolve, int generationTime, String fileName, int row, int col) throws IOException {
    for (int i = 0; i < numberEvolve; i++) {
        showUniverse(universe);
        sleep(generationTime);
        universe = nextGeneration(universe, fileName, row , col);
    }
}

2 个答案:

答案 0 :(得分:0)

for (int row = 0; row < 40; row++) {
    for (int col = 1; col < 60; col++) {
      int numberEvolve = scanner.nextInt();
      Cell[][] universe = readUniverseFile(fileName);
      showUniverse(universe);
      evolve(universe, numberEvolve, 2, fileName, row, col);
    }
}

首次使用evolve()row = 0致电col = 1 这些值传递给nextGeneration()那里:

universe = nextGeneration(universe, fileName, row , col);

你打电话给Cell[][] nextUniverse = new Cell[row][col]; (记住,它是0和1)

然后用固定值i和j开始迭代:

for (int i = 1; i <= 38; i++) {
  for (int j = 1; j <= 58; j++) {

一旦拨打nextUniverse[i][j] = Cell.DEAD; 如果值i / j大于0/1,则会得到ArrayOutOfBoundsException, 因为你的数组大小取决于行/ col输入,但你用i和j的迭代最多可达38/58。

快乐修复:)

答案 1 :(得分:0)

main 方法中for循环的第一次迭代中,row为0且col为1

22 for (int row = 0; row < 40; row++) {
23     for (int col = 1; col < 60; col++) {
24         int numberEvolve = scanner.nextInt();
25         Cell[][] universe = readUniverseFile(fileName);
26         showUniverse(universe);
           // On the first iteration, row is 0 and col is 1 when calling evolve() method
27         evolve(universe, numberEvolve, 2, fileName, row, col);}}

之后,使用变量row = 0和col = 1

调用nextGeneration()
134 private static void evolve(Cell[][] universe, int numberEvolve, int generationTime, String fileName, int row, int col) throws IOException {
135     for (int i = 0; i < numberEvolve; i++) {
136         showUniverse(universe);
137         sleep(generationTime);
            // In the method nextGeneration(), row is 0 and col is 1
138         universe = nextGeneration(universe, fileName, row , col);
139     }
140 }

接下来,创建一个名为nextUniverse的对象,其大小为Cell [0] [1]
但是,您尝试在第84行访问nextUniverse [38] [58],因此抛出了NullPointerException。

  

基本上,你不能访问nextUniverse [38] [58],因为nextUniverse的大小是[0] [1]

77 private static Cell[][] nextGeneration(Cell[][] universe, String fileName, int row, int col) throws IOException {   
       // nextUniverse has a size of [0][1]
78     Cell[][] nextUniverse = new Cell[row][col];
79     for (int i = 1; i <= 38; i++) {
80         for (int j = 1; j <= 58; j++) {
81             int livingNeighbours = numberLivingNeighbours(universe);
82             if (universe[i][j] == Cell.LIVE) {
83                 if (livingNeighbours < 2 || livingNeighbours > 3) {
                       // Attempted to store values into nextUniverse[38][58]
84                     nextUniverse[i][j] = Cell.DEAD;