Search code examples
javakarel

How to create an overarching if statement? As in, "if this happens at any point in the program..."


my assignment is to create an algorithm for the karel robot to use to exit a maze and stop once it reaches a beeper. I have successfully created this algorithm except for getting karel to stop when it reaches the beeper. This is only a portion of my code, but you'll see that I'm basically inserting a beeper checkpoint at every step. I can't help but feel like there's an easier way, plus, when I tried executing with my newly inserted beeper checks, it gave me this error: Exception in thread "main" java.lang.StackOverflowError

    while(!arg.rightIsClear() && arg.frontIsClear() && !arg.nextToABeeper())
    {
        arg.move();
    }
    if(arg.rightIsClear() && !arg.nextToABeeper())
    {
        arg.turnRight();
        arg.move();

so, I would like to simply have an if statement that is checked at every interval throughout the program, if that is possible. thanks.


Solution

  • I think this: Create a custom event in Java is what you are looking for.

    A stackoverflow usually occurs when you accidentally call the same method in a loop. Is this piece of code located in either of the methods turnRight() or move()?