この再帰を実行すると、stackoverflow エラーが発生します。パターンがあり、最初に次のように言います。
行である MazeGui.move(MazeGui.java:79) で if(rigting.goal == true) {
そして、次の2つが表示され、出力では両方が非常に長い間繰り返されます。ここで問題が発生しています。どこかわかりません。
行移動(rigting.right、pos)であるMazeGui.move(MazeGui.java:89)で。//右に移動
行移動(rigting.left、pos)であるMazeGui.move(MazeGui.java:107)で。//左に移動
...
...
終了条件か何かが欠けていますか、無限再帰が発生していますか? 私はそれに頭を包むことができず、完全に失われました。どんな助けでも大歓迎です。
コード:
public boolean move(Maze rigting, int pos)
{
if (rigting.goal == true)
{
return true;
}
if (rigting.wallR != true)
{
pos += 1;
move(rigting.right, pos); //moves right
showLabel(pos);
return true;
}
if(rigting.wallD != true) //checks if there is a wall below
{
pos += 10;
move(rigting.down, pos); //moves down
showLabel(pos);
return true;
}
if(rigting.wallL != true) //checks if there is a wall on the left
{
pos -= 1;
move(rigting.left, pos); //moves left
showLabel(pos);
return true;
}
if(rigting.wallU != true) //checks if there is a wall above
{
pos -= 10;
move(rigting.up, pos); //moves up
showLabel(pos);
return true;
}
return false;
}