2014-02-24 30 views
0

我正在寫一篇康威的人生遊戲遊戲,讓我幾周感到非常沮喪。到目前爲止,我已經編寫了大部分代碼,但仍然存在一些問題。我提前爲很多問題道歉。和X - -指數超出界限和多次跑步

我的人生計劃的遊戲包含30×30條線的輸入文本文件開始代表死亡的細胞和X活着。

我的問題現在就是:

  1. 如何處理arrayoutofbounds例外livesurrounding檢查出數組的時候?我想要做的就是忽略這些價值觀。
  2. 如何更改我的代碼以運行多次gens變量的值?
  3. 爲什麼打印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;   
    }  
} 
+0

#1:在'liveSurrounding',你需要決定正確的邏輯是什麼時, 「周圍」單元不在陣列中。假設你想表現得與非X的表現一樣。然後你只需在訪問周圍元素之前檢查索引,例如, if(a> 0 && grid [a-1] [b] =='X')','if(b ajb

回答

0

你的問題實在是不夠嚴謹,我們不打算通過所有的代碼來看看這裏,也許人們在爲CodeReview.SE都願意這樣做,一旦你的代碼工作。 ..

爲了回答關於如何避免IndexOutOfBoundsException您的問題,您可以通過第一避免這種覈查是否要訪問的元素在數組的邊界,然後訪問它,例如:

private void accessArray(final int[] array, final int index) { 
    if (0 <= index && index < array.length) { 
     int whatever = array[index]; 
    } 
} 
0

要回答#3:Arrays.toString通過在每個元素上調用覆蓋的toString來工作。二維數組是數組的數組。因此,數組中的每個元素本身就是一個數組,並且在數組上調用toString會爲您提供一個地址,因爲它看起來像您已經知道。

toString的的documentation說:

如果數組包含其他數組作爲元素,它們是由Object.toString()從對象繼承的方法,其中 描述他們的身份,而不是轉化爲 串他們的內容。

調查Arrays.deepToString我認爲是你想要的。

0

有些錯誤是有...

  1. char[] chararray = new char[900]; - >你是固定的長度?是你的文件的內容超過30個字符,然後IndexOutOfBoundsException
  2. 在這裏,你的聲明int i=4所以後來30×30 + 4 = 904然後IndexOutOfBoundsException

    INT I = 4;對於(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(); }

  3. ALSO if (grid[a - 1][b] == 'X')如果a=00-1=-1所以IndexOutOfBoundsException會.............同樣越來越多....

SO我不這樣說,檢查代碼

0

#2:使用此:

for(int gen=0;gen<gens;gen++){ 
    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'; 
       }  
      } 
     } 
    } 
}