gpt4 book ai didi

java - 生命游戏数组索引越界

转载 作者:行者123 更新时间:2023-12-01 18:34:39 28 4
gpt4 key购买 nike

我正在玩康威的生命游戏。我很确定我已经接近完成了,但是当我运行它时,我在线程“main”java.lang.ArrayIndexOutOfBoundsException中得到异常:-1
在game.of.life.GameOfLife. Generation(GameOfLife.java:77)
在 game.of.life.GameOfLife.main(GameOfLife.java:32)
Java 结果: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];
}
}
}

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

最佳答案

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

关于java - 生命游戏数组索引越界,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22584851/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com