我正在制作一个Java Mine Sweeper程序。到目前为止,除了级联和揭示之外,一切都进展顺利。我继续得到StackOverFlowError。有人可以看看并帮助我吗?谢谢!
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是我的方法,对于我在学校的实际课程,我们必须使用递归。我认为这是我们应该做的,但我遇到了错误。
答案 0 :(得分:0)
检查你的递归的停止条件,你确定你只能得到'B','1'或'2',当一个小区有3个地雷时会发生什么?顺便说一下,我认为你的递归可以简化,如果每个单元格显示你检查所有邻居(包括越界),并在你的函数的开头你检查是否是一个有效的单元格((xbox> = 0& ;& xbox< = 7)||(ybox> = 0&& ybox< = 7)),如果它不是有效的单元格,则按原样返回数组。