Final: any maze · Robot Quest
Can you write one program that solves mazes it has never seen?
Course contentsWhy you need it
This is the final challenge, and it uses everything: functions, while, if, and the sensors. More importantly it introduces the idea of an algorithm: a general method that works for a whole family of problems, not just one.
The intuition
Lost in a maze? Put your right hand on the wall and never take it off. You will eventually walk out (for mazes where all the walls are connected). This is the right-hand rule.
For Bit, "keep the right hand on the wall" becomes one step:
- Turn right (try to follow the wall).
- While the way ahead is blocked, turn left.
- Move forward.
Repeat that step until the flag. Pick up gems along the way.
Definition
Algorithm
An algorithm is a finite, step-by-step method for solving a whole class of problems.
Examples
One step of the rule
Run it in the lessonRun it and watch Bit hug the right wall around the corner.
def step():
turn_right()
while not front_clear():
turn_left()
move()
while not at_goal():
step()- L7Not on the flag yet: take one step.
- L2Try turning right first.
- L3Blocked? Turn left until the way is clear.
- L5Move forward one square.
- L7Repeat until the flag, through both corners.
A whole maze
Run it in the lessonThe same algorithm, plus picking gems. It also solves the mazes in the exercises.
def step():
turn_right()
while not front_clear():
turn_left()
move()
while not at_goal():
step()
if on_gem():
pick()- L8Loop until the flag.
- L9One step of the right-hand rule.
- L10After every step, check for a gem.
Common mistakes
Spinning on the spot
If Bit is completely walled in, while not front_clear(): turn_left() never ends. The maps here always have a way out.
Checking the flag too late
Check at_goal() in the while condition so Bit stops the moment it arrives.
Quick quiz
1. Where does Bit end up?
def step():
turn_right()
while not front_clear():
turn_left()
move()
step()
step()Show the answer
Answer: A
2. Why is the right-hand rule called an algorithm?
Show the answer
Answer: A
An algorithm is a general method, not a solution for one map.
Practice
3 exercises in three steps: predict what a program prints, change a program to meet a new requirement, then write one from scratch. Your code runs in the browser and is checked the moment you submit.
- PredictTrace the rule
Predict where Bit ends up after three steps.
- ModifyDon't forget the gems
This solver reaches the flag but leaves the gems behind. Fix it so it collects every gem in both mazes.
- WriteGraduation maze
Write the full maze solver from scratch: a
step()function for the right-hand rule, awhileloop until the flag, and gem picking. It must solve all 3 mazes.
Practise this lesson
Open the interactive lesson to edit and run these examples and solve the exercises with instant feedback. Free to start, no sign-up.
Interactive lessons open in order. If you have not done the earlier ones yet, you start at the course map.
Terms in this lesson
| Term | Chinese | Definition |
|---|---|---|
| Algorithm | 算法 | A finite, step-by-step method that solves a whole class of problems. |
| Right-hand rule | 右手法则 | A maze algorithm: keep the wall on your right and follow it. |