Word Search

[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

Here's a "word search" puzzle solver motivated by a Mensa Puzzle Calendar entry which asks viewers to find five 7-letter words in the given grid by following "crooked" paths. The search rules require moving in a "crooked path" from each letter to an adjacent letter horizontally, vertically, or diagonally to the next letter and no letter may be revisited within a single word.  The puzzle is one of several included in the program downloads.

A start at finding word for "dart-shooting" tube

Background & Techniques

This is the only puzzle of the "crooked path" type I know of, but I generalized program features to  include:

1. Traditional word search "straight path" type puzzles requiring that words lie in a straight line horizontally, vertically, or diagonally.
2: Inclusion of a "Target word" list for the given grid.
3. Setting a range of word lengths for words to be found.
4. Allowing users to identify words by clicking in the grid.
5. Letting the program find words solving the puzzle according to the given parameters.
6. Clicking on found words to replay them on the grid.
7. Allowing users to modify or define puzzles by specifying grid size (up to 18x18), entering grid letters and other parameters.
8. Support puzzle saving to and reloading from text files.

Since additional puzzles must be manually entered, the program is probably more useful as a demonstration of coding techniques than as a puzzle playing game.    Two 16x16 more traditional word-search puzzles, "cats.txt" and "weather.txt", are included with the downloads.   Our Crossword Generator program has a feature allowing traditional word search puzzles be created from a user supplied list of Target words.  A handy tool for those teaching vocabulary related topics.      

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:

This program turned out to be more complicated that I had anticipated and took three weeks and  1000 lines of code to get it into its current stage.  Here's a brief summary of the functions and feature the I learned or relearned in the process.

  • Users can interact with the search grid in two ways:
    • Click to define the letters of a word during the search phase and
    • Enter letter into the grid to modify or create a puzzle.  

    An OnDrawCell event exit is used to highlight letters of words being defined, but with editing turned on (necessary for modifying letters), Delphi overrides user drawing in an unacceptable way.  Solution was to add a "User Mode" radio group to specify "playing" or "changing" and then remove or add  StringGrid editing options depending on the current mode.

  • Cells clicked in finding a word must be retained so that revisiting letters within a word can be denied, erroneous clicks can be undone,  and  found words can be replayed later.   There are two parts to this problem. 
    • Highlighting clicked letters and preventing revisiting is handled by using an extra character position in the text of each cell.  In addition to the letter to be displayed, the extra character contains a space character by default but an exclamation point character (!) if the cell is part of the current word. The marked cells cannot be revisited and are drawn in red font color. 
    • Knowing the path of the current word  as it is built is important if the user wants to retract a letter selection. A separate stringlist, of 5 character strings, CurrentWordList,  has an entry for each letter.  Each entry has the letter plus 2 character string versions of the column and row of the letter. Clicking a selected cell will "unselect" that partial word from the clicked letter to the end of the selected letter for that word.  A copy of this path stringlist for each word is saved in the "FoundWordList" listbox as the object associated with the word string. 
  • Two recursive search functions, GetNextCrookedPath and GetNextStraightPath, find words when the program is asked to search for words. Each is passed a pointer to the last letter found and the partial word string built so far.  In addition the straight path search function gets an indicator of which direction we are currently checking.   The crooked path function has to check for a valid next letter all 7 unused directions.  Checking is performed in a separate Path array of integers which has two extra rows and columns used to surround the valid grid sized array.  These extra "guard" cells are preloaded with large negative numbers to speed searching.  The position for each letter of a word being formed has a positive value and all the valid next letter cells have zeros.  When a zero position is found, that letter from the grid is added to the PartialWord string, the position is added to the CurrentWordList and checks are made to see if we have a valid word yet.  If Target words were provided, we check for existence of our word in that list, if no target words, we check against a dictionary.        
  • Scaling the grid's cell size and font size based on the number of rows and columns in the current puzzle required dividing maximum grid width by the number of row and columns allowing for the space occupied by grid lines.  Once the DefaultColWidth and DefaultRowHeight properties have been calculated the font size to use must be calculated and the font assigned to the grid's Canvas property. 
  • Checking for modifications to the current puzzle before loading a different puzzle or closing the program invokes a technique used in many programs on DFF.  A Modified Boolean flag is set to False when a puzzle is loaded or after it is saved and set to True by any routine than changes puzzle content.  The CheckModified function is called before overwriting  the current puzzle or closing the program.  If it finds the Modified flag set, it uses a MessageDialog to ask the user if she wants to save current puzzle first.  If  reply is Yes, the save procedure is called.  Either a Yes or No reply will return a True result for CheckModified, telling the caller to proceed.  A Cancel reply to the MessageDialog will return False to the caller, usually meaning do not proceed with the load or program exit.    
  • Target words and Found words are displayed  in TListboxes because they support multiple columns and often eliminate the need for scrollbars.  Unlike TMemo controls however, listboxes do not support user editing directly, so entry/change of Target words is performed in a separate dialog.  Existing target words are preloaded into a memo control in the dialog before it is displayed.  After the user completes the editing, an OK button click will return to the caller where the lines (words) are copied to a  Stringlist where a  Sort method call will sort them and we can rebuild the TargetWordList with the sorted results. 
  • Whew again!  Happy coding!


November 27, 2015:  Wordsearch Version 2.1 adds a "Revisit OK" checkbox to allow revisiting grid letters within a word. Included Puzzle "RevisitTest.txt" is a recent Mensa Calendar Puzzle which requires the enhancement for players to solve. The program search option finds all words that can be formed within a given length range when letter revisits are allowed. The two solution words are contained in the words the program finds.

Here's the puzzle: Start on a letter and move horizontally, vertically, or diagonally from letter to adjacent letter to spell out a two-word famous fighting force. You may return to a letter and reuse it. All the letters will be used at least once. (Source:Mensa Brain Puzzlers Calendar for for November 21, 2015)

Running/Exploring the Program 

Suggestions for Further Explorations

Allowing user click on target words to display the first letter would be a nice "Hint"  feature (and save a lot of time for the playerJ). 


Original:  January 25, 2015

Modified:  February 18, 2016

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