[Home] [Puzzles & Projects] [Delphi Techniques] [Math topics] [Library] [Utilities]
Place 3 dimes and 2 quarters in a row like this The baseline is an extension of line formed by the bottoms of these coins. The objective is to rearrange the coins into an arrangement that looks like this . You can only move a pair of adjacent coins of different types (i.e. Dime/Quarter or Quarter/Dime) by sliding the pair left or right to a new unoccupied location anywhere on the baseline. (If you are using real coins, you'll have to slide them vertically first, then left or right, then back in line.) Coins may not be rotated to change orientation while moving
Reference: The Master Book of Mathematical Recreations, Fred Schuh, Dover Publications
This puzzle was presented as an example of a puzzle that can most easily be solved in reverse. From the original position there are 4 coin pairs to consider as first move candidates. In the solution there is only one coin pair eligible for the first move. Since all moves are reversible, we can consider DDDQQ to be the initial state and DQDQD to be the goal state. Reversing the order and direction of the moves to solve this puzzle is a solution to the original puzzle. This does indeed reduce the search tree significantly.
This program however has been implemented more as an example of graphics and drag drop processing than as a search program. The AutoSolve button simply selects randomly from a few predefined solutions embedded in the program. The program used to generate these solutions is (or will be) posted to Math Topics as Adjacency List Graph Searching.
Non-programmers can skip to the bottom of the page to download an executable version of the program
This is the first program in this series to show drag images (an image of the pair of coins being dragged) while dragging. Dragging images seems unnecessarily complicated - I'm sure there are reasons beyond my feeble understanding why this is so.
Dragging without images is straightforward. You need to specify either dmAutomatic for the control to be dragged, and, in the receiving control, a DragOver exit (to say that its OK to drop it here) and DragDrop exit (to actually move the dropped object).
Image dragging is more complex. Whenever a control drag is started, a call is made to GetDragImages which, unless you have overridden it, returns nil, telling dragging that no images are being displayed. If you return a Timagelist (or a TDragImageList), drag images will be used. To display other than the first image, a call to Imagelist.SetDragImage is required to specify which image to use.
In summary, the elements are:
This application is complicated by the fact that we are dragging pairs of objects (big/small coin pairs) and the drop area must only be able to fit the pair, ignoring the fact that the quarter may be dropping into a dime size slot.
Anyway, it mostly works if you want to study the code for hints.
Addendum: January 17, 2002: I posted an improved version today with better dragging features, specifically,
Suggestions for Further Explorations
Coin drawing is still done directly on the form's canvas. It would be cleaner to to draw on the canvas of a Timage to simplify the calculation of offsets when converting points to screen coordinates..
Copyright © 2000-2018, Gary Darby All rights reserved.