I recently started learning more about recursion in Python and quickly got myself into the Tower of Hanoi problem.
I already have a recursive solution in Python, which prints the moves I should play to solve the problem, but I would like to visualize it and see the pieces moving.
What would be a possible approach to it?
If you model your pegs as a list of lists with the larger discs at the lower indices, it should be relatively simple to implement a printing function and a movement function (that also prints). You can then feed your movements to that function.
Output:
This will work for any tower height and, if your algorithm makes illegal moves, it will illustrate them as well:
If you build your Hanoi solver as a generator, you will be able to use it in a for-loop that simply calls the moveHanoi() function: