[Home] [Puzzles & Projects] [Delphi Techniques] [Math topics] [Library] [Utilities]
Arrange the 10 tiles shown into a 5 X 5 array of numbers in such a way that the 5-digit horizontal numbers match the 5-digit vertical numbers.
Background & Techniques
This puzzle is from the book, Mensa Number Puzzles by Harold Gale, Carlton Books, 1993. There are 200 puzzles in the book including about a dozen of this type, surely enough to justify writing a program to solve them.
Not much usage description needed so non-programmers can jump to bottom the page now to download an executable version of the program.
Notes for programmers
This is another exhaustive search program, we'll start with the first tile and place it every possible position on a 5 X 5 grid, then for that position try every legal position for the second tile. For each valid placement of two tiles, try all positions for the third tile, etc. until we have placed all, or cannot place any more. This descending search method suggests using a recursive tile placement routine. Tryfits is it. Pass it a tile number, N, to place and a board with earlier tiles in place and it will systematically check all 25 grid locations to see if the tile N would fit there. When it finds a location, place tile N and call Tryfits with tile N+1. When we return from Tryfits, if no solution has been found, remove tile N and continue searching for the next location. When all the tiles have been placed, we must have a solution so set the SolutionFound flag and stop removing tiles.
The criteria for placing a tile:
Tryfits calls the Tilefits function to test if a tile fits starting at a given grid position, and procedures AddTile and RemoveTile as appropriate. These are all non-graphic routines and the most fun to write.
The display part is much messier, especially allowing user play. I used drag and drop logic and spent much time on the DragOverProc and DragDropProc procedures. It seemed like a good idea at the time, but I never did it working as I had originally planned. DragoverProc calls occur when the dragged object is dragged over something and must set an Accept variable indicating whether the object can be dropped there. So when we drag over the playing grid, we need to figure which grid squares would be occupied and whether it will fit there. A tile should not be eligible to be dropped on another tile, except ... (I hate those excepts) that it must be able to be dropped on itself if, for example the user wants to relocate a horizontal tile one position to the left. The user doesn't really think that he is dropping a tile on a tile, but the drop/drop logic does. That's the essence of the code you will find in those drag handling procedures.
The tiles are represented as TTileObj objects which are derived from TStringGrid controls. They are kept in a Tiles array. In addition to the stringgrid stuff, (size, location, and values), each tile also contains the tile number (its index in the Tiles array), and a MoveTo point, (its column/row coordinates on the playing grid).
Lots of other stuff that you can check out for yourself, enough to put this program in the "Advanced" category with 700+ lines of code. For the benefit of search engines, features that may be of interest include:
Running/Exploring the Program
Suggestions for Further Explorations
Modified: May 15, 2018
[Feedback] [Newsletters (subscribe/view)] [About me]
Copyright © 2000-2018, Gary Darby All rights reserved.