
Bug hunt
Walking Through Walls
A good move function asks two questions about the new square, before Pip goes there:
- Is it inside the map?
- Is it free, or is a rock (
#) in the way?
If either answer is bad, Pip stays where they are. The function returns the old position.
The bug
Pip starts at (0, 0) and tries to step south. Row 1, column 0
is a rock. But the program prints:
Pip is at (1, 0)
Pip walked straight into the rock, and no warning appeared!
There are two bugs in try_move. One checks the wrong
square. The other forgets to stop Pip after the warning.
Assignment
Fix try_move(world, pos, delta) so that:
- It checks the tile at the new position for a rock.
- When there is a rock, it prints
A rock blocks the way.and returns the old position. - Edges and open moves keep working as before.
Fix try_move(world, pos, delta) so that:
- It checks the tile at the new position for a rock.
- When there is a rock, it prints
A rock blocks the way.and returns the old position. - Edges and open moves keep working as before.
main.py Python sleeping
Press Run to try your code, then Check to test it.