Project 2g
Animation through Threads
150 points
Must be done alone (not in pairs)

The Towers of Hanoi


Create a program that animates the movement of discs for the Towers of Hanoi.  Recall that the Towers of Hanoi is a puzzle where you have three pegs (A, B, C).  On peg A are a set of discs, all of different sizes, with smaller discs on top of larger discs as a rule.  The problem is to move the stack of discs from peg A to peg C with the following rules:

  1. You may only move the top disc of a stack of discs (this also implies that you can only move one disc at a time).

  2. A larger disc can never be placed on top of a smaller disc.


The basic algorithm (designed for Console output) for Hanoi is as follows:


void Hanoi(int numDisks, char source, char destination, char intermediate)


if (numDisks == 1)

    Console.WriteLn("Move a disk from " + source + " to " + destination);



Hanoi(numDisks-1, source, intermediate, destination);

Console.WriteLn("Move a disk from " + source + " to " + destination);

Hanoi(numDisks-1, intermediate, destination, source);

}//end else

}//end Hanoi


Allow the user to choose the number of discs for the simulation.  You can enforce a maximum of 8 if you wish (since that will result in 255 moves).


The discs can be drawn as rectangles.  Make each disc a different color.  When a disc is moved, animate the movement from peg to peg.  The animation can be achieved via a thread.  The sample code given through THIS LINK will give you some idea of what to do.  The animation could be performed using a Timer object, but the idea is for you to get some experience with threads...


In addition actually moving the discs, you should write to a textbox/listbox the moves made (ala the code given above in the Hanoi method).


Provide buttons to allow the user to:



To turn in