我在做一個Java掃雷程序。到目前爲止,除了級聯和揭示外,一切都進展順利。我一直在得到StackOverFlowError。有人可以看看並幫助我嗎?謝謝!級聯和顯示的Java
public static char[][]revealCell(int xbox, int ybox, char blankboard [] [], char RevealedBoard [][]){
if(RevealedBoard[ybox][xbox]== 'B'){
blankboard[ybox][xbox]= 'B';
return blankboard;
}else if(RevealedBoard[ybox][xbox]== '1'){
blankboard[ybox][xbox]= '1';
return blankboard;
}
else if(RevealedBoard[ybox][xbox]== '2'){
blankboard[ybox][xbox]= '2';
return blankboard;
}else{
blankboard[ybox][xbox]= '-';
if(xbox== 0){ // left of the grid
if(ybox==0){
blankboard= revealCell(xbox+1, ybox, blankboard, RevealedBoard);
blankboard= revealCell(xbox, ybox+1, blankboard, RevealedBoard);
blankboard= revealCell(xbox+1, ybox+1, blankboard, RevealedBoard);
return blankboard;
}else if(ybox== 7){
blankboard= revealCell(xbox+1, ybox, blankboard, RevealedBoard);
blankboard= revealCell(xbox, ybox-1, blankboard, RevealedBoard);
blankboard= revealCell(xbox+1, ybox-1, blankboard, RevealedBoard);
return blankboard;
}else{
blankboard= revealCell(xbox+1, ybox, blankboard, RevealedBoard);
blankboard= revealCell(xbox, ybox-1, blankboard, RevealedBoard);
blankboard= revealCell(xbox +1, ybox-1, blankboard, RevealedBoard);
blankboard= revealCell(xbox, ybox+1, blankboard, RevealedBoard);
blankboard= revealCell(xbox+1, ybox+1, blankboard, RevealedBoard);
return blankboard;
}
}else if(xbox== 7){// right of the grid
blankboard= revealCell(xbox-1, ybox, blankboard, RevealedBoard);
if(ybox== 7){
blankboard= revealCell(xbox, ybox-1, blankboard, RevealedBoard);
blankboard= revealCell(xbox-1, ybox-1, blankboard, RevealedBoard);
return blankboard;
}else if(ybox==0){
blankboard= revealCell(xbox, ybox+1, blankboard, RevealedBoard);
blankboard= revealCell(xbox-1, ybox+1, blankboard, RevealedBoard);
return blankboard;
}else{
blankboard= revealCell(xbox-1, ybox-1, blankboard, RevealedBoard);
blankboard= revealCell(xbox-1, ybox+1, blankboard, RevealedBoard);
blankboard= revealCell(xbox, ybox-1, blankboard, RevealedBoard);
blankboard= revealCell(xbox, ybox+1, blankboard, RevealedBoard);
return blankboard;
}
}else{
if(ybox== 0){
blankboard= revealCell(xbox-1, ybox, blankboard, RevealedBoard);
blankboard= revealCell(xbox, ybox+1, blankboard, RevealedBoard);
blankboard= revealCell(xbox-1, ybox+1, blankboard, RevealedBoard);
return blankboard;
}else if(ybox==7){
blankboard= revealCell(xbox-1, ybox, blankboard, RevealedBoard);
blankboard= revealCell(xbox, ybox-1, blankboard, RevealedBoard);
blankboard= revealCell(xbox-1, ybox-1, blankboard, RevealedBoard);
return blankboard;
}else{
blankboard= revealCell(xbox, ybox-1, blankboard, RevealedBoard);
blankboard= revealCell(xbox-1, ybox-1, blankboard, RevealedBoard);
blankboard= revealCell(xbox+1, ybox-1, blankboard, RevealedBoard);
blankboard= revealCell(xbox-1, ybox+1, blankboard, RevealedBoard);
blankboard= revealCell(xbox, ybox+1, blankboard, RevealedBoard);
blankboard= revealCell(xbox-1, ybox, blankboard, RevealedBoard);
blankboard= revealCell(xbox+1, ybox, blankboard, RevealedBoard);
blankboard= revealCell(xbox+1, ybox+1, blankboard, RevealedBoard);
}
}
}
return blankboard;
}
}
revealCell是我的方法,對於我在學校的實際班級,我們必須使用遞歸。我認爲這是我們應該做的,但我得到錯誤。
手工指定每一個方向在多個地方檢查是醜陋的。使用像這樣的枚舉解決方案:http://pastebin.com/06yNW2TD然後編寫一個方法來檢查訪問是否超出限制,如果不允許則不做任何事情。然後在每個地方,您都可以在同一個'方向'枚舉循環中嘗試所有方向。 – Patashu 2013-04-09 23:56:23