### 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.

Mensa® Daily Puzzlers

For over 15 years Mensa Page-A-Day calendars have provided several puzzles a year for my programming pleasure.  Coding "solvers" is most fun, but many programs also allow user solving, convenient for "fill in the blanks" type.  Below are Amazon  links to the two most recent years.

(Hint: If you can wait, current year calendars are usually on sale in January.)

### Contact

 Search DelphiForFun.org only

### Problem Description

Here's some brain exercise for the kiddies.

Find the combination that will open the safe by clicking  the buttons in the proper sequence.  The final button is marked "Last", but it's up to you to discover the starting point.  Each button except the Last has instructions for finding the next, the number of squares to move and a abbreviation of the direction to move (Up, Down, Left, or Right)..

### Background & Techniques

This is another puzzle adapted from a type in the Mensa Number Puzzles Book (Harold Gale, Carlton Books).    The computer version generates random "combinations" and allows users to control problem size from 2X2 to 10X10.

There are at least four approaches to solving this problem:

1. Click on a random square and follow the movement directions from there to Last.  If you do not pass through all of the buttons, the true starting point will be one of the unvisited buttons.
2. The starting button is the only one that cannot be reached from another button.  Examining each button in turn will eventually turn up the proper one.  The "link-from" button, if it exists, will be one of the buttons in the sharing the same row or column as the button being checked.
3. You can start at Last and work backwards to the starting button.
4. Finally,  you can click the Auto-Unlock button.  The program uses the "search backward" technique to find and display the correct. path.

Non-programmers are welcome to read-on but may want to jump the the bottom of the page now to download the executable version of the program.

#### Notes for programmers

A two dimensional dynamic array of strings, Grid, is used to hold the text to be displayed on the board.     Procedure MakeBtnClick generates a valid set of values by trial and error.   It calls MakeValidMove to  define the next cell.   MakeValidMove tries 1000 times generating random directions & distances and returns false if no valid move can be found.

Dynamic array Path contains the cells coordinates ofcells in the current path, whether a full path generated by the computer or a partial path generated by the user.  Procedure DrawBoard handles drawing the board with current Grid values.  Procedure DrawPath highlights the cells in the path, placing small sequence numbers in the upper left corner of selected cells and coloring them green for the first cell, red for the last cell and silver for the intermediate cells.  Drawpath takes a Sleepval parameter used to introduce a 1/2 second delay for each cell drawn when displaying the computer solution.

TImage, Image1, contains the board image.  A Mousedown exit for Image1 is used to handle user clicks, adding valid next cell clicks to the path and beeping for invalid clicks..

Function GetNext is passed coordinates of a cell and returns the coordinates of the next cell.  It is used to check validity of user clicks.   It is also used by function GetPrev which  finds the previous cell by passing cells in order to GetNext until the return value is equal to GetPrev's input.      GetPrev is used by the computer solution search procedure, UnlockBtnClick, which starts at cell LAST and builds the solution path by searching backwards using calls to GetPrev.

Enough of the talk - let's go!

### Suggestions for Further Explorations

 An "expert"  level of play could more closely emulate a real combination lock by forcing a complete restart after any wrong selection. Scoring could be added, scores based either on error counts,  time to solve, or both. The original version had a search backward mode allowing uses to find the solution by searching backward from last call to first. A more elaborate reward for correct solution could be added.  I have a Rewards unit floating around somewhere, but was too lazy to implement it here.

 Original Date: June 26, 2002 Modified: May 15, 2018