我正在寫一個掃雷的方法,打開一個單元,如果沒有礦在那裏。如果礦旁沒有相鄰的單元格,則會打開沒有地雷的單元格周圍的單元格。我經常有這樣的錯誤:掃雷stackoverflowerror
異常在線程 「AWT-EventQueue的 - 0」 java.lang.StackOverflowError的 這是我的源代碼:
public void open(int row, int col) {
// row = vertical index of the matrix
// col = horizontal index of matrix
unclicked--;
butt[row][col].setEnabled(false); // disable the called button
if (aray[row][col] !=0) // checks if there are no adjacent cells with an adjacent mine count >0
butt[row][col].setText(Integer.toString(aray[row][col]));
else{
if(row < size-1){
open(row+1, col);
if(col<size-1)
open(row+1, col+1);
if(col>0)
open(row+1, col+1);
}
if(row>0){
if(col>0)
open(row-1, col-1);
if(col< size)
open(row-1, col+1);
}
if(col<size-1)
open(row, col+1);
if(col>0)
open(row, col-1);
return;
}
}
幫助,將不勝感激
您是否嘗試過調試? – Jens
看起來像可能的無限遞歸。 – paisanco
遞歸應該如何結束?可能會插入'if(!butt [row] [col] .isEnabled())return;'作爲此方法的第一行。 – Marco13