生命游戏ArrayIndexOutofBounds

我在做康威的生活游戏。 我很确定我已接近完成,但是当我运行它时,我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

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

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

如果有人可以帮助我,我将不胜感激。

r0 ,这个无效: old[r - 1][c]
这样你就得到了你发布的例外。

我建议你这样简化它。

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

我可以看到,你基本上有两个选择:

  1. 应用有限边界,即对于第一个和最后一个列和行中的单元格,在计算“活动”邻居的数量时,您会实施额外的检查。

  2. 应用周期性边界,即最左侧列上的单元格和最右侧列上的单元格被视为邻居。 在模块化算法的帮助下,这些单元不需要与其他单元分开处理。