Word Grid

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



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.


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

Search DelphiForFun.org only



Problem Description

Find the word in this partially filled grid which starts in one corner and circles the board to end in the center square.

Background & Techniques

Here's the puzzle from the January 3 puzzle in "The Brain Game", our daily puzzle calendar for 2013.  (My rule is that I can use any program I have written as a brain extender J!)

"A nine letter word is starts at a corner and spirals into the center. Fill in the missing letters to find out what it is."

For different puzzles, click on the grid to enter edit mode, key in the new changes, then press the "Make templates"  button to exit edit mode and start solving. Click the "Search templates " button to search the dictionary for the matching word.

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

Programmer's Notes:

For future reference, here are few tips and techniques used in this program .

  • The strategy for solving the problem is to generate the 8 possible word templates for words starting each of the 4 corners proceeding clockwise or counterclockwise around the grid before ending with the letter in the center.  Then we'll use our UDict unit to retrieve all 9 letter words in the largest dictionary ( "Full.dic") and check each word against each template looking for those that have matching letters in positions where the letters in the template are known.
  • If we make a string of letters and ? characters starting in the top left corner and concatenate (join together) the top row, the last column, the bottom row from right to left and the first column from bottom to top we have the first 8 letter template.
    • {make a clockwise string around the border from top left corner}
    • The corresponding template in the other direction is simply the first template reversed.  Function "ReverseString" in Delphi's StrUtils unit does exactly what we need.  sccw:= reverseString(scw);
  • To get the template starting form the next corner, all we must do is move the first two characters to the end of the string. (e.g. s:=copy(s,3,6)+copy(s,1,2);)  Repeat that 3 times, add that center character,   and we have all 4 templates for one direction.  Do the same thing on the reversed string and we'll have the 4 counterclockwise templates.
  • A dictionary named PubDic is automatically created by the UDict unit.  Function LoadLargeDic is called to load the large dictionary, prompting you for the location if it is not in the same folder as the program.    The dictionary is remembered from run to run in file Dic.ini.  Delete this file to specify a  dictionary from a different location.
  • A call to Pubdic.Setrange procedure will tell the dictionary that you want to sequentially retrieve all 9 letter words starting with letters "A" though "Z".  Then a "While GetNextWord" loop will retrieve each word and return True until all have been  processed.
  • We pass each word retrieved against all 8 templates, discarding any word with a character which does not have the same letter or a ? in the template.  A solution is any word which pass the matching test for all 9 letters.
  • An OnDrawCell event exit is used to center the character in the center of each grid cell by placing the top left corner at the center of the cell minus half of the character's width and half the character's height like this:  with rect do textout((left + right - textwidth(s)) div 2, (top + bottom -textheight(s)) div 2, s);
  • To allow changes to the grid, the editor features must be enabled but in edit mode the currently selected cell is highlighted and displayed in the default location, distracting for normal usage.  To avoid the distraction when not editing, I added an OnClick event exit with this statement to add the GoAlwaysShowEditor option to the set of grid options when the user clicks on the grid: with stringgrid1 do options:=options + [goAlwaysShowEditor];   The "Make templates" button turns the option off again to restore the normal display by subtracting the option from the set. 

Running/Exploring the Program 

Suggestions for Further Explorations



Original:  January 4, 2013

Modified:  February 18, 2016

  [Feedback]   [Newsletters (subscribe/view)] [About me]
Copyright 2000-2017, Gary Darby    All rights reserved.