我在玩康威的人生游戏。我很确定我快完成了,但是当我运行它时,我得到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. 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];
}
}
}
如果有人能帮助我,我将不胜感激。
当r
为0
时,此为无效: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;
}
正如我所看到的,你基本上有两个选择:
-
应用有限边界,也就是说,对于第一列和最后一列和行中的单元格,在计算"活的"邻居的数量时,您实现了额外的检查。
-
应用周期边界,即最左边列的单元格和最右边列的单元格被认为是邻居。在模块化算法的帮助下,这些单元不需要与其他单元分开处理。