0

I receive a stackoverflow error when performing this recursion. There is a pattern, it says this first once:

at MazeGui.move(MazeGui.java:79) which is line if(rigting.goal == true) {

and then it says the following two, and both of them are repeated for a very very long time in the output. The problem is happening here somewhere I'm just not sure what:

at MazeGui.move(MazeGui.java:89) which is line move(rigting.right, pos); //moves right

at MazeGui.move(MazeGui.java:107) which is line move(rigting.left, pos); //moves left

...

...

Am I missing a termination condition or something, is there some infinite recursion happening? I can't wrap my head around it, completely lost. Any help would be appreciated.

The code:

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;
}
1
  • Yes. Stack overflow usually means the stop condition is not met. Commented Sep 30, 2012 at 23:17

2 Answers 2

1

Your "pathing" algorithm has a simple recursive loop.

In this case, your algorithm computes that you must move right. Then once you has done that, it computes that you must move left. Once you've moved left, you're back in the position you were last in. Since you are back in your starting position, the cycle begins anew and continues this way infinitely (or, really, until there is a stack overflow).

A possible solution is to analyze the state of your application and any time the state updates, detect if you were in that state before; if you were, amend your actions accordingly.

Sign up to request clarification or add additional context in comments.

Comments

0

You might want to try something like this,

public boolean move(Maze rigting, int pos)

{
    if (rigting.goal == true)
    {
         return true;
    }

    if (rigting.wallR != true)
    {
         pos += 1;
         showLabel(pos);
         return move(rigting.right, pos); //moves right
    }

    if(rigting.wallD != true) //checks if there is a wall below
    {
        pos += 10;
        showLabel(pos);
        return move(rigting.down, pos); //moves down
    }
    if(rigting.wallL != true) //checks if there is a wall on the left
    {
        pos -= 1;
        showLabel(pos);
        return move(rigting.left, pos); //moves left
    }

    if(rigting.wallU != true) //checks if there is a wall above
    {
        pos -= 10;
        showLabel(pos);
        return move(rigting.up, pos); //moves up
    }

    return false;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.