0

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.

Sam
  • 43
  • 5

2 Answers2

1

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()?

Community
  • 1
  • 1
bas
  • 1,678
  • 12
  • 23
  • Thanks for the response. That custom event link is indeed what I was looking for, but its implementation is a bit lengthier than I thought. I thought you could just have a 1 line long statement that would solve my problem. What I was doing is actually quicker! Anyway, to try to fix the stackoverflow error, I switched the beeper check from inside the while/if parenthesis and moved it before the move() methods, and it worked out. Thanks for you help. – Sam Jul 29 '13 at 14:46
  • 1
    Creating events is the right way to do this though. Once you get familiar with them they are easier to use and easier to maintain. – bas Jul 29 '13 at 14:48
  • 1
    I see. well, I guess i'll check them out. – Sam Jul 29 '13 at 14:51
1

Imagine there was such a thing as "an overarching if statement" in the Karel programming language. For the sake of argument, let's call it "when". Now imagine you wrote the following code:

when (facingEast())
{
    turnLeft();
}

when (facingEast())
{
    turnRight();
}

What should happen if Karel faces east? Should the turn left? Should he turn right? Should he only do one of those things, because after he turns, he is no longer facing east? Or should he do both? And if so, in what order? Note this would make him face east again, and he would get stuck in an infinite loop.

As the above example illustrates, such "an overarching if statement" would very quickly lead to ambiguities and contradictions. It would not make much sense to put that in a programming language.

my assignment is to create an algorithm for the karel robot to use to exit a maze and stop once it reaches a beeper.

Then I would propose the following basic algorithm layout:

while (noBeepersPresent())
{
    turnInTheDesiredDirection();   // you have to write this method
    move();
}

This way, Karel will check for the beeper after each move.

fredoverflow
  • 256,549
  • 94
  • 388
  • 662