Make your own free website on Tripod.com

There are compile scripts under the scripts folder (but you might need to set up your java paths first). compile.sh.bat would compile, then archive (ie jar) all the class files before deleting the .class files under the bin directory. The doc directory contains some files which may help in further understanding the algorithm. The source files are all in the src folder.

Program listing:

  1. Stack.java - stack interface
  2. MyStack.java - linked list implemented stack
  3. StackFactory.java
  4. Peg.java - peg class definition. Contains stack methods.
  5. Disk.java - disk class definition
  6. MyThreads.java - contains several thread classes that animates the program, the algorithms for the solution can be found in SolveThread which is a class in this file
  7. HanoiCanv.java - the drawing board
  8. App.java - the main applet
  9. Constants.java - program wide constants

Quick hint:

You might like to look at SolveThread class in file MyThreads.java. In its run method, you will see the following snippet-
--------------------------
		if(!hanoiCanv.isRandomized) {

			if (isConstrained && isSpread)
				hanoiCanv.askForHelp = true;							/**/

			else if (isConstrained)
				constrainedMove(hanoiCanv.numOfDisks, sandy, dandy);

			else if (isSpread)
				hanoiCanv.askForHelp = true;							/**/

			else
				originalMove(hanoiCanv.numOfDisks, sandy, dandy, andy);
		}

		else {

			if (isConstrained && isSpread)
				hanoiCanv.askForHelp = true;							/**/

			else if (isConstrained)
				hanoiCanv.askForHelp = true;							/**/

			else if (isSpread)
				hanoiCanv.askForHelp = true;							/**/

			else
				hanoiCanv.askForHelp = true;							/**/

		}
--------------------------
The original case (that is, in the applet, randomized, constrained, spread checkboxes are not ticked) is already done. Here is the snippet of code that does the trick-
--------------------------
	private void originalMove(int numOfDisks, Peg source, Peg destination,
			Peg alternate) {

		if (isInterrupted)
			return;

		if(numOfDisks > 0)
		{
			originalMove(numOfDisks - 1, source, alternate, destination);
			moveADisk(source, destination);
			originalMove(numOfDisks - 1, alternate, destination, source);
		}
	}
--------------------------
Basically, if you have 4 disks (initially at the source peg- the first one), what you have to do is to move the top three disks to the alternate peg (the middle peg) and move the remaining disk number 4 into the destination peg. Then after, we move the three disks in the alternate peg to the destination.





Happy hacking! =)