Everyone Can Learn Program
中文 Start learning
Course 1 · Zero to codingLesson 3 of 10Free to read

Collecting gems · Robot Quest

How do you plan a longer program that has more than one goal?

Course contents

Why 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 lesson

Bit walks to the gem, picks it up, then continues to the flag.

move()
move()
pick()
move()
move()
Step by step
  1. L1Part 1: walk to the gem (first square).
  2. L2Second square: Bit is on the gem.
  3. L3Part 2: pick it up. The gem counter goes to 1/1.
  4. L4Part 3: walk to the flag.
  5. L5On the flag with every gem collected: solved.

Picking too early

Run it in the lesson

Here pick() comes one line too early. Run it and see the error.

move()
pick()
move()
move()
move()
Step by step
  1. L1Bit moves one square. The gem is still one square ahead.
  2. L2pick() 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()
  1. ASolved
  2. BError on line 3: no gem here
  3. CBit reaches the flag but the gem is left
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?

  1. AYes
  2. BNo
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.

  1. PredictCount the gems

    Predict the result.

  2. ModifyA second gem

    This program solved the level when there was one gem. A second gem has appeared. Add what's missing.

  3. 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

TermChineseDefinition
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.