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:
- 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.
- 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.
- You can start at Last and work backwards
to the starting button.
- 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!
Running/Exploring the Program