我正在寫一篇康威的人生遊戲遊戲,讓我幾周感到非常沮喪。到目前爲止,我已經編寫了大部分代碼,但仍然存在一些問題。我提前爲很多問題道歉。和X - -指數超出界限和多次跑步
我的人生計劃的遊戲包含30×30條線的輸入文本文件開始代表死亡的細胞和X活着。
我的問題現在就是:
- 如何處理arrayoutofbounds例外livesurrounding檢查出數組的時候?我想要做的就是忽略這些價值觀。
- 如何更改我的代碼以運行多次gens變量的值?
- 爲什麼打印copygrid總是打印地址,即使我使用Arrays.toString?
謝謝,任何幫助表示讚賞,因爲我很困惑,並用盡時間。
我的代碼:
package assignment2;
/**
*
* @author jaw209
*/
import java.io.*;
import java.util.*;
import javax.swing.*;
public class Assignment2 {
private static int livecount;
static char[][] copygrid = {{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'},{'-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-','-'}};
public static void main(String[] args) throws FileNotFoundException {
String inputfile;
String output;
String generations;
//Input file = L:\Java 2\Assignment2\Sample input.txt
//Inputs from file to char array
inputfile = JOptionPane.showInputDialog ("Where is the input file? Ex: C:\\users\\public\\desktop\\input.txt ");
Scanner input = new Scanner (new FileReader(inputfile));
char[] chararray = new char[900];
String allvalues = null;
do {
String values = input.next();
allvalues = allvalues + values;
}
while(input.hasNextLine());
chararray = allvalues.toCharArray();
//Reads values in chararray into multi array
char[][] grid1 = new char[30][30];
int i = 4;
for(int row = 0; row < 30; row++){
for(int col = 0; col < 30; col++){
grid1 [row][col] = chararray[i];
i++;
System.out.print(grid1[row][col]);
}
System.out.println();
}
//Finds how many generations should be calculated
generations = JOptionPane.showInputDialog ("How many generations should be calculated?");
int gens = Integer.parseInt(generations);
//Runs the cycle once
for (int row = 0; row < 30; row++){
for (int col = 0; col < 30; col ++){
if (status(grid1[row][col])){
liveSurrounding(grid1, row, col);
moves(row, col);
}
else if (!status(grid1[row][col])){
//run 3 checker
if (liveSurrounding(grid1, row, col) == 3){
copygrid[row][col] = 'X';
}
}
}
}
System.out.println(copygrid);
//Output to File
output = JOptionPane.showInputDialog ("Where is the output file?");
PrintWriter out = new PrintWriter(output);
out.println(Arrays.toString(copygrid));
}
//Check to see if cell is live or dead
public static boolean status(char value){
if (value == 'X'){
return true;
} else {
return false;
}
}
//See if neighbor is alive or dead
public static int liveSurrounding(char [][] grid, int a, int b){
livecount = 0;
if (grid[a-1][b] == 'X'){
livecount++;
}
if (grid[a-1][b+1] == 'X'){
livecount++;
}
if (grid[a][b+1] == 'X'){
livecount++;
}
if (grid[a+1][b+1] == 'X'){
livecount++;
}
if (grid[a+1][b] == 'X'){
livecount++;
}
if (grid[a+1][b-1] == 'X'){
livecount++;
}
if (grid[a][b-1] == 'X'){
livecount++;
}
if (grid[a-1][b-1] == 'X'){
livecount++;
}
return livecount;
}
//Adjust alive cells for each condition
public static char[][] moves(int a, int b){
switch(livecount){
case 0: copygrid[a][b] = 'X'; break;
case 1: copygrid[a--][b] = 'X'; break;
case 2: copygrid[a][b++] = 'X'; break;
case 3: copygrid[a][b--] = 'X'; break;
case 4: copygrid[a++][b] = 'X'; break;
case 5: copygrid[a--][b++] = 'X'; break;
case 6: copygrid[a++][b--] = 'X';
case 7: copygrid[a--][b--] = 'X'; break;
case 8: copygrid[a++][b++] = 'X'; break;
default:
}
return copygrid;
}
}
#1:在'liveSurrounding',你需要決定正確的邏輯是什麼時, 「周圍」單元不在陣列中。假設你想表現得與非X的表現一樣。然後你只需在訪問周圍元素之前檢查索引,例如, if(a> 0 && grid [a-1] [b] =='X')','if(b
ajb