Table Of Contents

Previous topic

24. Refinements: part 3

Next topic

27. Rule number 4

This Page

25. Refinements: part 4

As you most likely found out, we were too hasty in asking Reeborg to move forward after putting down a token. We need to check if there is a wall preventing him from moving first. Here’s a solution to the problem:

put("token");
if ( !front_is_clear() ) {
    turn_left();
}
move();
while ( !token_here() ){
    if (right_is_clear()){
        turn_right();
        move();
    } else if (front_is_clear()){
        move();
    } else {
        turn_left();
    }
}

Try it!

Test it now and see that it works. Can you imagine situations where it might not work?

26. Refinements: part 5

Consider world Around 4

Try it!

Does the program written previously work with it?

As you probably guessed, if you didn’t try it (you should, really!), it does not. To make it work, we need to replace the if we just added by a while. Try it!