Night 24: Move around
你已使用 Python 或伪代码完成过这个夜间练习,两种语言均计入完成记录。你可以再次练习。
此页面需较新浏览器(支持 SharedArrayBuffer)。请升级 Chrome、Edge、Firefox 或 Safari 至最新版本。
English
Tonight: move around
- Your pet has a map. Tonight it walks around on it!
- Nothing new to learn: you use
INPUT,WHILEandIFon a map.
Words for tonight
| Word | Meaning |
|---|---|
| move | go from one place to another |
| position | where something is: X and Y |
| command | a word that tells the program what to do |
English
Where is the pet?
- Two numbers say where the pet is:
X(left–right) andY(up–down). Y = 1is the top row. So "up" makesYsmaller.
| Move | Change |
|---|---|
| up | Y ← Y - 1 |
| down | Y ← Y + 1 |
| left | X ← X - 1 |
| right | X ← X + 1 |

English
Choose with CASE OF
- One variable, many choices:
CASE OFis neat for commands.
DECLARE X, Y : INTEGER
DECLARE Move : STRING
X ← 3
Y ← 3
Move ← "up"
CASE OF Move
"up" : Y ← Y - 1
"down" : Y ← Y + 1
"left" : X ← X - 1
"right" : X ← X + 1
ENDCASE
OUTPUT "Now at ", X, " ", Y
- In the tasks, the moves come from
INPUTuntil the player typesstop.
English
Stay on the map
- Check the edge before you move.
DECLARE X : INTEGER
DECLARE Move : STRING
X ← 1
Move ← "left"
IF Move = "left" AND X > 1 THEN
X ← X - 1
ENDIF
OUTPUT X
Xstays 1: the pet is already at the left wall.- Now take your pet for a walk!
难度 2/5 · 今日首次完成可获 140 金币及少量额外奖励
Your pet is at X ← 3, Y ← 3. Read one move: up, down, left 或 right. Change X 或 Y, then output them. left gives 2 3. up makes Y smaller.
点击“运行”查看此处输出。
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
Now read moves again and again, until the move is stop. Change X and Y for each move. At the end, output X and Y.
点击“运行”查看此处输出。
难度 3/5 · 今日首次完成可获 200 金币及少量额外奖励
The map is 5 × 5: X and Y go from 1 到 5. Do not let the pet walk off the map. A move into the wall does nothing.
点击“运行”查看此处输出。
难度 4/5 · 今日首次完成可获 280 金币及少量额外奖励
After the moves, draw the 5 × 5 map: P where the pet is, . everywhere else. Row Y and column X hold the P.
点击“运行”查看此处输出。