update_game function.
Step 7 replaces the old if/elif direction chain with a dictionary lookup, and replaces wall-death with modulo wrap-around. Fill in each blank — use exact Python syntax.
Three common mistakes students make when implementing wrap-around and the MOVES dictionary. Fix each broken line — type the corrected Python line exactly.
🐞 Bug 1 — Wrong wrap operator
The student used division instead of modulo. The snake exits the grid instead of wrapping.
Hint: one character is wrong — which operator causes wrap-around?
🐞 Bug 2 — Hardcoded dictionary key
The student hardcoded a lowercase string instead of using the direction variable. This always causes a KeyError.
Hint: use the variable that holds the current direction string.
🐞 Bug 3 — Missing head index
The student tries to unpack the entire snake list instead of just its head. This causes a too many values to unpack error.
Hint: access only the first element of the snake list.
One strength of the dictionary approach is how easy it is to extend. Add a "STOP" key to MOVES so the snake stays in place when it receives that command — then update update_game so it handles "STOP" correctly (the snake should not move or grow).
update_game from scratch
Apply everything: MOVES dictionary + modulo wrap-around + self-collision check.
Without referring to the code above, write the complete update_game function. It must: (1) look up the direction vector using MOVES, (2) apply wrap-around using %, (3) check for self-collision only (no wall-death), and (4) handle food/score correctly.
Test your understanding of modulo arithmetic and dictionary-based dispatch.
1. What does 20 % 20 evaluate to?
2. Given hx = -1 and GRID_COLS = 20, what is hx % GRID_COLS?
3. Why is using a MOVES dictionary better than an if/elif chain for direction handling?
4. Given MOVES = {"LEFT": (-1, 0)}, what does dx, dy = MOVES["LEFT"] give?
5. After adding % wrap-around, which check from the old code can be removed?