[Home] [Puzzles & Projects] [Delphi Techniques] [Math topics] [Library] [Utilities]
|
|
Problem DescriptionHere is version 3 of the Towers of Hanoi program. We finally get around to adding graphic images of the disks and pegs and to allow user to drag disks to solve the problem. The computer will also show moving disks as it solves the problem. Background & TechniquesMost of the theory has been presented in the discussion section of Version 1 and Version 2. In summary, we can solve the problem recursively for N pegs by letting the solve routine for N disks call itself for N-1 pegs (the top N-1 disks) , then moving the bottom disk to the empty peg, then calling the solve itself again to move the N-1 disks back on top of the bottom disk. The primary addition in this version is the graphics capabilities. The TDisk object is now a descendent of TShape (a rounded rectangle shape). The TTower object is now descended from TPanel to provide a doubled buffered canvas for the peg and disk images. TTower uses Dragover event exit to decide where the disk can be dropped (close to a peg with no disk or a peg topped by a disk larger than the disk being dragged). It also has a DragDrop event exit to actually move the disk when the user drops it. The Moveone routine animates the movement. Since the disks are descended from TShape, that component handles repainting as necessary. The pegs however are drawn directly on the canvas and must be drawn in a Paint event exit to ensure that they stay visible. I've arbitrarily limited the graphics display to 10 disks which takes 1023 moves to solve. That seemed like all anyone would want to solve manually or watch the computer solve. Over 500 lines of code here so we'll put it in the Advanced category - but it's quite straightforward. Like most programming, just a lot of details.. Running/Exploring the Program
Suggestions for Further Explorations
|
[Feedback] [Newsletters (subscribe/view)] [About me]Copyright © 2000-2018, Gary Darby All rights reserved. |