Can this program be made any shorter? http://www.dodaj.rs/f/1K/vW/2gLz63rl/blockly.png
Here's one with same amount of code and optimal logic (for this map) without hard coding. It also ensures that there is a forward step in every iteration of the while loop.
Basically it's just an order of preference:
Turn right > turn left > go straight
while True:
if wall(AHEAD):
turn(LEFT)
else:
move(FORWARD)
turn(RIGHT)
However, the addition of one more if makes the solution much faster and look more sensible, by not turning to the right and immediately back to the left: while True:
if wall(AHEAD):
turn(LEFT)
else:
move(FORWARD)
if not wall(RIGHT):
turn(RIGHT)Edit: I didn't know that an else was possible, so I had to do it this way.
while !wall(AHEAD):
move forward
turn left
while wall(AHEAD):
turn right while true:
turn left
while wall(AHEAD):
turn right
move forward
However, while this algorithm happens to work on this maze, it will not work against other mazes (if you care).Imagine a 5x5 square race track, with spokes coming from the outside of the wheel to the center, where there was a flag. Your algorithm (and my modified one) would just spin clockwise around the track without ever realizing it should take a right towards the center.