私と私の研究パートナーは、ConwayのGame of Lifeに関する課題を抱えています。Game of Life malfunctioning java
私たちのプログラムは、誤動作と私は我々のコードと間違って何も見えないコードを見ていると、エラーメッセージ、さらにはデバッグ機能を見たとき、それは誤動作だなぜ私はまだ見つけることができません。
次のようにエラーメッセージがある:nl.ru.ai.exercise6.tryout.nextGeneration(tryout.java:84)で 36:スレッド "メイン" java.lang.ArrayIndexOutOfBoundsExceptionで
例外 nl.ru.ai.exercise6.tryout.main(tryout.java:27)
で nl.ru.ai.exercise6.tryout.evolve(tryout.java:138)でデバッグを使用する場合それは84行目に問題があると言います。nextUniverse[i][j] = Cell.DEAD;
誰かが私たちのコードと仕様を見てください。なぜ私たちのコードがその特定のポイントで動作しないのか
universe = nextGeneration(universe, fileName, row , col);
:あなたはこれらの値はそこnextGeneration()
に渡されrow = 0
とcol = 1
で初めてevolve()
を呼び出すここで私は、私たちのコードは非常に長いですけど、我々はまだ
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);
}
}
、プログラミングの天才? – TDG