World 5: While and lists
The last world
- In the last world your code looks at the map itself.
- A WHILE loop walks until something is true. An array keeps many moves.
Words for this world
| Word | Meaning |
|---|---|
| WHILE | repeat as long as something is true |
| array | many values in one variable |
| search | look at every place to find something |
Walk until you find it
DECLARE Road : STRING
DECLARE Pos : INTEGER
Road ← "P..$"
Pos ← 1
WHILE MID(Road, Pos, 1) <> "$"
OUTPUT "right"
Pos ← Pos + 1
ENDWHILE
OUTPUT "dig"
- The loop stops when the pet stands on the treasure.
الصعوبة 3/5 · 200 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
The road is "P...$..H". Use a WHILE loop: walk right while the square is not $. Dig, then walk right while the square is not H.
اضغط تشغيل لرؤية المخرجات هنا.
الصعوبة 3/5 · 200 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
Pack the moves into an array first. Add right و down إلى Moves three times. Then output every move in the array.
اضغط تشغيل لرؤية المخرجات هنا.
الصعوبة 3/5 · 200 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
This route array has words the pet does not know (jump, sing). Output only the real moves: up, down, left, right.
اضغط تشغيل لرؤية المخرجات هنا.
الصعوبة 4/5 · 280 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
Let the code find home on the map. Search every row and column for H, keep its position, then output the moves to get there.
اضغط تشغيل لرؤية المخرجات هنا.
الصعوبة 3/5 · 200 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
Walk right while the next square is not a tree. Then go down and one step right home.
اضغط تشغيل لرؤية المخرجات هنا.
الصعوبة 5/5 · 400 عملة + مكافأة صغيرة عند الإنجاز الأول اليوم
Final level! Let the code find both the treasure $ and home H on the map. Walk to the treasure, dig, then walk home. (Go right first, then down.)
اضغط تشغيل لرؤية المخرجات هنا.