The Towers of Hanoi

The Towers of Hanoi is a simple yet classic puzzle.

You are given a puzzle with three pegs. Stacked on one peg are
n disks of different sizes. Each disk in the stack is smaller in radius than the disk beneath it. The problem is to move the disks, one at a time, from one peg to another, while never placing a larger disk on top of a smaller disk. It's trivial for two or three disks, but gets trickier with more.

This applet solves such a puzzle. It's not a hard thing to do for a computer. Thus, this applet also serves as an excellent vehicle for teaching some important topics about computer programming, as well as providing a nice way to exploit Java. That is the purpose of this applet and this web site.

The software solution uses
recursion to solve the the puzzle. Recursion is a technique where a program is divided into a smaller problem of the same nature as the original problem.

The applet is designed using the
Model/View design pattern, also called the Observer design pattern. In Model/View design, you separate the objects in the problem domain into models, which represent state, information, data, or computation, and one or more views, which observe the model.

This Java applet, like most modern graphical users interfaces and many Java applets and applications, uses event driven design. That is, much of the processing is driven by events, such as button clicks.

This particular applet also uses separate
threads to do its work. Each thread is sort of like a separate person on a team, each working together at the same time to solve a problem.

Since the purpose of this applet is to educate, the best thing I can do is to provide the
source code. From the source, you can see how these various design elements work. You can also view the documentation for the Java classes. Finally, I'm not fool enough to think I was the first or best person to write a Towers of Hanoi applet. Use the links page to find some others.

Next section: The Applet Applet

David J. Biesack