Week 5: Towers of Hanoi
Chris Tralie
Overview / Logistics
We talked about the Towers of Hanoi problem in Module 9, and we came up with a way of computing the optimal number of steps, but now we're going to take it to the next level and actually show what the steps are in an animation.
Click here to download the starter code for this exercise. It represents each peg as a stack, which uses a singly-linked list at the underlying data structure. Fill in the solve_rec
method in hanoi.py to recursively show a solution. Every time you move a peg, you should call self.draw_frame()
. As you go, you will have to change the role of pegs, so you'll have to carefully figure out which pegs to pass along as the "source," "target," and "free" pegs.
Below are a few examples of this working. To run this, an object is initialized at the bottom
This will output a bunch of frames as png images of each step.