2011-06-08 42 views
3

我在學校爲java創建掃雷程序,並且在清除沒有任何地雷的廣場時遇到麻煩,廣場應該是禁用的,所有的周圍正方形顯示,如果有另一個觸摸無炸彈的廣場,它將執行相同的操作。我得到一個堆棧溢出錯誤,我知道這與一個無限循環有關,但我無法找到它在我的代碼中卡住的位置。掃雷計劃遞歸錯誤

我不知道發生了什麼問題,所以任何建議都會有幫助。

這裏是代碼的相對件(的if語句是用於特殊情況下,如果點擊了正方形是在板的邊緣)

private void doClear(int y, int x, JButton[][] bArray2, int gridy,int gridx) 
{ 
    clicked--; 
    bArray2[y][x].setBackground(lighterGray); 
    bArray2[y][x].setEnabled(false); 

    if (x > 0 && x<gridx-1 && y> 0 && y<gridy-1) 
    { 
     clearSquare(y-1, x-1,gridy, gridx, bArray2); 
     clearSquare(y-1, x,gridy, gridx, bArray2); 
     clearSquare(y-1, x+1,gridy, gridx, bArray2); 
     clearSquare(y, x-1,gridy, gridx, bArray2); 
     clearSquare(y, x+1,gridy, gridx, bArray2); 
     clearSquare(y+1, x-1,gridy, gridx, bArray2); 
     clearSquare(y+1, x,gridy, gridx, bArray2); 
     clearSquare(y+1, x+1,gridy, gridx, bArray2); 
    } 
    if(y == 0 && x != 0 && x != gridx-1) // top row check 
         { 
          clearSquare(y, x-1,gridy, gridx, bArray2); 
          clearSquare(y, x+1,gridy, gridx, bArray2); 
          clearSquare(y+1, x-1,gridy, gridx, bArray2); 
          clearSquare(y+1, x,gridy, gridx, bArray2); 
          clearSquare(y+1, x+1,gridy, gridx, bArray2); 

         } // ends top row check 

         if (y == 0 && x == 0) // corner check top left 
         { 
          clearSquare(y, x+1,gridy, gridx, bArray2); 
          clearSquare(y+1, x,gridy, gridx, bArray2); 
          clearSquare(y+1, x+1,gridy, gridx, bArray2); 
         } // ends top left corner check 

         if (y == 0 && x == gridx-1) // corner check top right row 
         {  
          clearSquare(y, x-1,gridy, gridx, bArray2); 
          clearSquare(y+1, x-1,gridy, gridx, bArray2); 
          clearSquare(y+1, x,gridy, gridx, bArray2);        
         } // ends top right corner check 

         if (x == 0 && y != 0 && y != gridy-1) //left column check 
         { 
          clearSquare(y-1, x,gridy, gridx, bArray2); 
          clearSquare(y-1, x+1,gridy, gridx, bArray2); 
          clearSquare(y, x+1,gridy, gridx, bArray2); 
          clearSquare(y+1, x,gridy, gridx, bArray2); 
          clearSquare(y+1, x+1,gridy, gridx, bArray2); 
          } // ends left column check 

          if (x == gridx-1 && y != 0 && y != gridy-1) // right column check 
          { 
          clearSquare(y-1, x-1,gridy, gridx, bArray2); 
          clearSquare(y-1, x,gridy, gridx, bArray2); 
          clearSquare(y, x-1,gridy, gridx, bArray2); 
          clearSquare(y+1, x-1,gridy, gridx, bArray2); 
          clearSquare(y+1, x,gridy, gridx, bArray2); 
          }// ends right column check 

         if(y == gridy-1 && x != 0 && x != gridx-1) 
         { 
          clearSquare(y-1, x-1,gridy, gridx, bArray2); 
          clearSquare(y-1, x,gridy, gridx, bArray2); 
          clearSquare(y-1, x+1,gridy, gridx, bArray2); 
          clearSquare(y, x-1,gridy, gridx, bArray2); 
          clearSquare(y, x+1,gridy, gridx, bArray2); 
         } // ends bottom row check 

         if (y == gridy-1 && x == 0) // left bottom corner check 
         { 
          clearSquare(y-1, x,gridy, gridx, bArray2); 
          clearSquare(y-1, x+1,gridy, gridx, bArray2); 
          clearSquare(y, x+1,gridy, gridx, bArray2); 

         } // ends left bottom corner check 

         if (y == gridy-1 && x == gridx-1) // right bottom corner check 
         { 
          clearSquare(y-1, x-1,gridy, gridx, bArray2); 
          clearSquare(y-1, x,gridy, gridx, bArray2); 
          clearSquare(y, x-1,gridy, gridx, bArray2); 
         }//ends right bottom corner check 
} 

private void clearSquare(int y,int x, int gridy, int gridx, JButton[][] bArray2) 
{ 
     int value = array[y][x]; 
     System.out.println(value); 
     String text = bArray2[y][x].getText(); 
     if (text == "") 
     { 
     if (value == 0) 
     { 
      doClear(y, x, bArray2, gridy, gridx); 
     } 
     else{ 
      clicked--; 
      bArray2[y][x].setText(""+value); 
      } 
     } 

} 
+1

順便說一句,如果你在頂部和底部有一個額外的行和在左側和右側有一個額外的列,你將需要少得多的代碼。保持這些隱藏設置的初始狀態清除。 – ikegami 2011-06-08 00:17:48

+0

我已添加到我的答案中。原來還有另一種簡化代碼的方法,不需要添加額外的行和列。 – ikegami 2011-06-08 00:41:26

回答

4
private void doClear(int y, int x, JButton[][] bArray2, int gridy,int gridx) 
{ 
    if (...already cleared...) { 
     return; 
    } 

    ... 
} 

沒有這種檢查,小區A將清除相鄰小區B,它會清除相鄰小區A,它會清除相鄰小區B,這...

您發佈的代碼可以被替換爲以下幾點:

private void handleClick(JButton[][] bGrid, int gridy, int gridx, int y, int x, bool realClick) { 
    if (x < 0 || x >= gridx || y < 0 || y >= gridx) { 
     return; 
    } 

    JButton button = bGrid[y][x]; 
    if (!button.isEnabled()) { 
     return; 
    } 

    if (realClick) { 
     --clicked; 
    } 

    button.setBackground(lighterGray); 
    button.setEnabled(false); 

    if (...is a mine...) { 
     ... 
    } else { 
     button.setText(array[y][x]); 

     if (value == 0) { 
      handleClick(bGrid, gridy, gridx, y-1, x-1, false); 
      handleClick(bGrid, gridy, gridx, y-1, x, false); 
      handleClick(bGrid, gridy, gridx, y-1, x+1, false); 
      handleClick(bGrid, gridy, gridx, y, x-1, false); 
      handleClick(bGrid, gridy, gridx, y, x+1, false); 
      handleClick(bGrid, gridy, gridx, y+1, x-1, false); 
      handleClick(bGrid, gridy, gridx, y+1, x, false); 
      handleClick(bGrid, gridy, gridx, y+1, x+1, false); 
     } 
    } 
} 

array」需要重新命名爲某種感覺!

更新:增加了簡化代碼。