Collecting gems · Robot Quest
How do you plan a longer program that has more than one goal?
Course contentsWhy you need it
Now Bit has a job: pick up every gem and then stand on the flag. Programs often have several things to achieve. Breaking the job into small parts ("get to gem 1, pick it, get to gem 2...") is how programmers keep long programs manageable.
The intuition
Think of a shopping list: walk to aisle 3, take milk, walk to aisle 5, take bread, go to the checkout. Each item is a small goal.
pick() picks up the gem on Bit's square. If there is no gem there, Bit stops with an error, just as you can't take milk from an empty shelf.
A level is solved only when all gems are collected and Bit stands on the flag.
Definition
Decomposition
Decomposition is breaking a problem into smaller parts that are easier to solve one at a time.
Examples
One gem on the way
Run it in the lessonBit walks to the gem, picks it up, then continues to the flag.
move()
move()
pick()
move()
move()- L1Part 1: walk to the gem (first square).
- L2Second square: Bit is on the gem.
- L3Part 2: pick it up. The gem counter goes to 1/1.
- L4Part 3: walk to the flag.
- L5On the flag with every gem collected: solved.
Picking too early
Run it in the lessonHere pick() comes one line too early. Run it and see the error.
move()
pick()
move()
move()
move()- L1Bit moves one square. The gem is still one square ahead.
- L2
pick()on an empty square: error on line 2. Swap lines 2 and 3 to fix it.
Common mistakes
Reaching the flag isn't enough
If a gem is left behind, the level is not solved, even though Bit is on the flag.
pick() where there's no gem
pick() on an empty square is an error. Count squares carefully, or test your program after each part.
Quick quiz
1. What happens?
move()
move()
pick()
move()Show the answer
Answer: B
The gem is on the first square. After two moves Bit has already passed it.
2. Bit stands on the flag but one gem is still on the map. Is the level solved?
Show the answer
Answer: B
Both conditions are needed: all gems collected and Bit on the flag.
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.
- PredictCount the gems
Predict the result.
- ModifyA second gem
This program solved the level when there was one gem. A second gem has appeared. Add what's missing.
- WriteGem corner
Collect both gems and finish on the flag.
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 |
|---|---|---|
| Decomposition | 分解 | Breaking a problem into smaller parts that are easier to solve one at a time. |
| Error | 错误 | A problem that stops a program from running, reported with the line where it happened. |