2012-10-06 36 views
0

我目前正在介紹Java類的編程,而且我遇到了一兩個問題。生命的遊戲算法/其他語句的結構

首先關閉我目前的程序是「康威的生命遊戲」我已經得到了一切工作,除了算法檢查鄰近的細胞。

我已經檢查了大約7或8個不同的帖子在Stackoverflow和一對夫婦在其他網站,到目前爲止我找不到任何人採取了我的方法。

現在我需要的是有人來查看我的代碼,看看它應該工作,如果不是爲什麼不呢?目前我遇到一個運行時錯誤,說:

「ArrayIndexOutOfBoundsException:5 at Life.checkold(Life.java:151)」

我已經在代碼中標出了這個地方,我可以做得最好。

我輸入到這兩個方法都由5

首先尺寸5的陣列,感謝您至少閱讀本。其次,如果我需要其他人包括什麼請你只留下評論或回覆(新來的抱歉)

建設下一代航空運輸系統:

public static boolean[][] buildnext(boolean[][] lastgen) 
{ 
    boolean[][] nextgen = new boolean[lastgen.length][lastgen[0].length]; 

    for(int r = 0; r < lastgen[0].length; r++) 
    { 
     for(int c = 0; c < lastgen.length; c++) 
     { 
      nextgen[c][r] = checkold(lastgen, c, r); 
     } 
    } 
    return nextgen; 

} 

我的檢查方法:

public static boolean checkold(boolean[][] lastgen, int col, int row) 
    { 
     int acount = 0; 
     boolean alive = lastgen[col][row]; 

     if(col == 0 && row == 0) //Top Left Corner 
     {  
      if(lastgen[col][row + 1] == true) acount++; //Below 
      if(lastgen[col + 1][row] == true) acount++; //Right 
      if(lastgen[col + 1][row + 1] == true) acount++; //Below Right 
     } 

     else if(col == lastgen.length && row == 0)//Top Right Corner 
     { 
      if(lastgen[col][row + 1] == true) acount++; //Below 
      if(lastgen[col - 1][row] == true) acount++; //Left 
      if(lastgen[col - 1][row + 1] == true) acount++; //Below Left 
     } 
     else if(col == 0 && row == lastgen[0].length)//Bottom Left Corner 
     { 
      if(lastgen[col][row - 1] == true) acount++; //Above 
      if(lastgen[col + 1][row] == true) acount++; //Right 
      if(lastgen[col + 1][row - 1] == true) acount++; //Above Right 

     } 
     else if(col == lastgen.length && row == lastgen[0].length) //Bottom Right Corner 
     { 
      if(lastgen[col][row - 1] == true) acount++; //Above 
      if(lastgen[col - 1][row] == true) acount++; //Left 
      if(lastgen[col - 1][row - 1] == true) acount++; //Above Left 
     } 
     else if(col == 0 && row > 0 && row < lastgen[0].length) //Left Col 
     { 
      if(lastgen[col][row - 1] == true) acount++; //Above 
          if(lastgen[col][row + 1] == true) acount++; //Below (This is the code that the runtime error is about) 
      if(lastgen[col + 1][row] == true) acount++; //Right 
      if(lastgen[col + 1][row - 1] == true) acount++; //Above Right 
      if(lastgen[col + 1][row + 1] == true) acount++; //Below Right 

     } 
     else if(col == lastgen.length && row > 0 && row < lastgen[0].length) //Right Col 
     { 
      if(lastgen[col - 1][row] == true) acount++; //Left 
      if(lastgen[col][row - 1] == true) acount++; //Above 
      if(lastgen[col][row + 1] == true) acount++; //Below 
      if(lastgen[col - 1][row - 1] == true) acount++; //Above Left 
      if(lastgen[col - 1][row + 1] == true) acount++; //Below Left 
     } 
     else if(col < 0 && row == 0) //Top Row 
     { 
      if(lastgen[col][row + 1] == true) acount++; //Below 
      if(lastgen[col - 1][row] == true) acount++; //Left 
      if(lastgen[col + 1][row] == true) acount++; //Right 
      if(lastgen[col - 1][row + 1] == true) acount++; //Below Left 
      if(lastgen[col + 1][row + 1] == true) acount++; //Below Right 
     } 
     else if(col < 0 && row == lastgen[0].length) //Bottom Row 
     { 
      if(lastgen[col][row - 1] == true) acount++; //Above 
      if(lastgen[col - 1][row] == true) acount++; //Left 
      if(lastgen[col + 1][row] == true) acount++; //Right 
      if(lastgen[col - 1][row - 1] == true) acount++; //Above Left 
      if(lastgen[col + 1][row - 1] == true) acount++; //Above Right 
     } 
     else if(col < 0 && row < 0) //Middle Cells 
     { 
      if(lastgen[col][row + 1] == true) acount++; //Below 
      if(lastgen[col][row - 1] == true) acount++; //Above 
      if(lastgen[col - 1][row] == true) acount++; //Left 
      if(lastgen[col + 1][row] == true) acount++; //Right 

      if(lastgen[col - 1][row - 1] == true) acount++; //Above Left 
      if(lastgen[col + 1][row - 1] == true) acount++; //Above Right 
      if(lastgen[col - 1][row + 1] == true) acount++; //Below Left 
      if(lastgen[col + 1][row + 1] == true) acount++; //Below Right 
     } 

     if(acount == 3 && alive == false) alive = true; 
     if(acount == 1) alive = false; 
     if(acount == 3 && alive == true) alive = false; 

     return alive; 
    } 
+0

可能重複:http://stackoverflow.com/questions/12743748/find-elements-surrounding-and-元素在一個數組/ 12743834#12743834和http://stackoverflow.com/questions/652106/finding-neighbours-in-a-two-dimensional-array/652123#652123 –

+0

@RohitJain謝謝,這些將幫助我。對不起,創建一個類似的問題。 –

+0

@AndrewPearson ..它沒有問題..而那些更好的方法來找到'neighbours'。你應該完全改變你的代碼..不需要太多的if-else .. –

回答

1

您的例外是相當明顯,你正試圖訪問一個沒有約束的數組索引。

else if(col == 0 && row > 0 && row < lastgen[0].length) //Left Col 
     { 
      if(lastgen[col][row - 1] == true) acount++; //Above 
          if(lastgen[col][row + 1] == true) acount++; //Below (This is the code that the runtime error is about) 

你的代碼在這條線失敗:

if(lastgen[col][row + 1] == true) 

說lastgen陣列是2X3陣列和行= 2,否則,如果部分是真實的,並且考慮是否也是如此,現在在這裏

if(lastgen[col][row + 1] == true) 

你試圖訪問3索引, 記住:數組索引從零開始。如果你的數組長度是3,你的數組索引將是0,1,2。 2將是最後一個索引。

+0

啊,我看到我做了什麼。感謝您的解釋。所以糾正這個問題的一種可能的方法是用lastgen [0] .length-1替換lastgen [0] .length。 –

+0

@AndrewPearson是的,我認爲應該工作.. – PermGenError

1

這些情況是否正確?

 col < 0 

在我看來,你應該評估「山坳> 0」

+1

你是對的,謝謝你的發現。那會讓我接下來。 (我想這是我在午夜編寫代碼時得到的結果) –

0

錯誤,因爲在同一塊

1. if(lastgen[col][row + 1] == true) acount++; 
2. if(lastgen[col + 1][row + 1] == true) acount++; 

兩行假設你lastgen元素大小爲4,你正嘗試訪問[row+1]這明顯是5

編輯

else if(col == 0 && row > 0 && row < lastgen[0].length-1)

,而不是else if(col == 0 && row > 0 && row < lastgen[0].length)

array.length-1