The main topic of this worksheet is recursion with backtracking, building on the maze-solving example from lecture.
These things might be helpful while working on the problems. Remember that for worksheets, we don't strictly limit what resources you can consult, so these are only suggestions.
solvemaze.py
working¶Get plane.py
, maze.py
, and solvemaze.py
from the course sample code repository
Test out solvemaze.py
and make sure you can run it in the terminal and find/open the image files it creates.
Note that when run as a script, solvemaze.py
now takes command line arguments (which are optional). If given:
For example,
python3 solvemaze.py 13 mazeimage
will create two files: mazeimage.svg
(which you can open in a browser) and mazeimage.png
(which you can open in VS code, a browser, or various other programs for viewing images). Each image will show a 13x13 maze and its solution.
Add a new function depth_first_maze_solution_history()
to solvemaze.py
that works similarly to depth_first_maze_solution()
but which returns a list of every path considered (rather than just a single path that is a solution). This means the returned value will be a list of lists of Point2
objects.
Thus the last item in the returned list will be a solution, if a solution exists.
Then, write a script that uses depth_first_maze_solution_history
to do the following:
n
n
by n
mazedfs001.png
, dfs002.png
, etc., that highlight all paths considered by the solverRun this on a maze of moderate size (e.g. 9x9) and flip through the resulting images in your platform's image viewer to make a crude animation of the recursive process.
Remember project 1 is due on Fri Feb 9 at 11:59pm central. If you have questions, this is the perfect time to discuss them with your TA. The worksheet was made a little shorter than usual to allow time for this.
Work on these if you have extra time and have no need to ask questions about the project.
Instead of using the PrimRandomMaze
class to generate a maze, write your own subclass of Maze
that creates a maze in which start
and goal
are set, the border is fully blocked, and so that it is possible to get from start
to goal
. The constructor should create some obstacles between the start and goal, to make the maze more interesting.
For example, you might make a class that simply places a large rectangle of blocked squares in the middle of the maze, so that a solution must either go along the top and right, or bottom and left. Or you might make some walls coming out of the sides, so that a solution needs to turn back and forth several times. Or you could make the start at the center, and surround it by walls that force any solution to take a spiral path out to a goal on the edge.
The key characteristics you are looking for are:
It's OK for the constructor to decide a size is too small or is otherwise unacceptable, and raise an exception. But to keep things interesting, it should allow arbitrarily large mazes.
Write a script that instantiates your class and then uses solvemaze
to find a solution. Save the solved maze to a SVG file.