生活游戏故障java
我和我在学校的研究伙伴在康威的《生命游戏》的作业上遇到了麻烦 我们的程序出现故障,查看代码时,我看不出代码有任何错误,当查看错误消息,甚至调试函数时,我仍然无法找到它出现故障的原因 错误消息如下所示: 线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常: 36在nl.ru.ai.exercise6.tryout.nextGeneration(tryout.java:84)在 nl.ru.ai.exercise6.tryout.evolve(tryout.java:138)位于 nl.ru.ai.exercise6.tryout.main(tryout.java:27) 当使用调试函数时,它表示第84行有问题,生活游戏故障java,java,conways-game-of-life,Java,Conways Game Of Life,我和我在学校的研究伙伴在康威的《生命游戏》的作业上遇到了麻烦 我们的程序出现故障,查看代码时,我看不出代码有任何错误,当查看错误消息,甚至调试函数时,我仍然无法找到它出现故障的原因 错误消息如下所示: 线程“main”java.lang.ArrayIndexOutOfBoundsException中的异常: 36在nl.ru.ai.exercise6.tryout.nextGeneration(tryout.java:84)在 nl.ru.ai.exercise6.tryout.evolve(t
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);
}
}
publicstaticvoidmain(字符串[]args)引发IOException{
System.out.print(“输入文件:”);
扫描仪=新的扫描仪(System.in);
字符串文件名=scanner.nextLine();
System.out.print(“您希望您的程序运行多少代?”);
用于(int行=0;行<40;行++){
for(int col=1;col<60;col++){
int numberrevolve=scanner.nextInt();
单元格[][]universe=readUniverseFile(文件名);
展示宇宙(宇宙);
进化(宇宙,数字旋转,2,文件名,行,列);}
}
静态单元格[][]readUniverseFile(字符串文件名)引发IOException{
单元格[][]宇宙=新单元格[40][60];
试一试{
BufferedReader in=新的BufferedReader(新文件读取器(文件名));
试一试{
对于(int i=0;i
在这里,您可以调用Cell[][]nextUniverse=newcell[row][col];
(记住,它是0和1)
然后使用固定值i和j开始迭代:
for (int i = 1; i <= 38; i++) {
for (int j = 1; j <= 58; j++) {
for(int i=1;i在主方法中for循环的第一次迭代中,行为0,列为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);}}
22(int行=0;行<40;行++){
23表示(int col=1;col<60;col++){
24 int numberrevolve=scanner.nextInt();
25个单元格[][]universe=readUniverseFile(文件名);
26展示宇宙(宇宙);
//在第一次迭代中,调用evolve()方法时,行为0,列为1
27进化(宇宙,数字旋转,2,文件名,行,列);}
之后,使用变量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 }
134私有静态void evolve(单元格[][]宇宙,整数旋转,整数生成时间,字符串文件名,整数行,整数列)引发IOException{
135表示(int i=0;i
接下来,创建一个名为nextUniverse的对象,其大小为单元格[0][1]
但是,您试图访问第84行中的nextUniverse[38][58],因此引发了NullPointerException。
基本上,您无法访问nextUniverse[38][58],因为nextUniverse的大小为[0][1]
77私有静态单元格[][]下一代(单元格[][]宇宙,字符串文件名,int行,int列)引发IOException{
//nextUniverse的大小为[0][1]
78个单元格[][]nextUniverse=新单元格[行][col];
79(inti=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);}}
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 }
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;