Towers of Hanoi #3

[Home]   [Puzzles & Projects]    [Delphi Techniques]   [Math topics]   [Library]   [Utilities]

 

Search

Search WWW

Search DelphiForFun.org

As of October, 2016, Embarcadero is offering a free release of Delphi (Delphi 10.1 Berlin Starter Edition ).     There are a few restrictions, but it is a welcome step toward making more programmers aware of the joys of Delphi.  They do say "Offer may be withdrawn at any time", so don't delay if you want to check it out.  Please use the feedback link to let me know if the link stops working.

 

Support DFF - Shop

 If you shop at Amazon anyway,  consider using this link. We receive a few cents from each purchase.   Thanks.

 

Support DFF - Donate

 If you benefit from the website,  in terms of knowledge, entertainment value, or something otherwise useful, consider making a donation via PayPal  to help defray the costs.  (No PayPal account necessary to donate via credit card.)  Transaction is secure.

Contact

Feedback:  Send an e-mail with your comments about this program (or anything else).

Search DelphiForFun.org only

 

 

 

Problem Description

Here 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 & Techniques

Most 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

The program should recognize when the user has solved it successfully and give a reward (ta-dahh sound or a nice image or something).    
The default drag image displayed displayed while user is dragging disks.  A disk shaped drag image  would be nice.   (The Sliding Coins puzzle provides a good example of using drag images.)
  
  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright 2000-2017, Gary Darby    All rights reserved.