Arrays

In this lesson we will see how to use LiveCode to build a small Word Finder application. This application was originally developed by one of the Development Team to 'assist' him in his Scrabble playing, it's handy but perhaps not strictly within the rules of the game..

The LiveCode Word Finder has two options; an Anagram Finder and a Scrabble Solver. The Anagram Finder takes a list of letters and returns all the valid anagrams which can be created using them relative to a fixed list of words. Each anagram is checked for validity against the SOWPODS word list which is the word list used in current International Scrabble Tournaments.

The Scrabble Solver extends the functionality of the Anagram Finder by filtering its results to find only the anagrams which match the user entered pattern. This pattern represents an opening on the Scrabble board where, for example, the pattern 'b??' means find all three letter words starting with 'b'. Question marks must be used to represent wildcards. Only anagrams which contain the same number of characters as those in the pattern will be returned.

Interface

Firstly we need to create our interface, we need options for what task we want to perform(1), fields to enter the pattern to match(2) and available letters(3), a search button(4) and a results field (5).

We group the radio buttons for Anagram Finder and Scrabble Solver, this means only one can be selected at a time without you having to implement that behavior manually.

Implementing the Anagram Finder - Stage 1 Finding all Possible Permutations

The Word Finder is implemented using one recursive function, which makes up the backbone of the application, and a handful of smaller, simpler functions which perform various checks and preparation work.

We begin by generating a list of all possible permutations using the letters which were entered in the Available Letters field. This is done using a recursive function which continually splits the string of available letters down into smaller substrings, and calls itself again with a substring as the parameter. The recursion stops when the parameter string contains only one character and therefore can't be split any further. As the recursion is taking place lists of permutations are generated by placing the characters which have been taken off the start of the parameter string, in turn, between each of the remaining characters of the string. More details can be found by looking at the permuteSequence function in the card script.

Implementing the Anagram Finder - Stage 2 Checking against the Dictionary

Once a list of permutations has been found they are checked to see if they are valid words (relative to our dictionary). As the dictionary is quite large, we store it compressed (using the built-in compress function) in the custom property uAllWords of the stack. To check if a permutation is valid we could have used

``tString is among the lines of sDictionary``

but the time required for this check would be dependent on the size of the dictionary. As a result the dictionary is instead placed into an array using the code below

``````repeat for each line tWord in tWords
put true into sDictionary[tWord]
end repeat``````

This allows us to use the check

``sDictionary[tString]``

This will be true if tString is within the word list, and empty (i.e. false) otherwise. This check is much more efficient than checking the lines of the custom property as it can be carried out in constant time. Any permutations which are contained within the dictionary are added to a list of valid words. The final piece of processing needed is to remove any duplicates from this list of words. A duplicate can occur if there are multiple instances of a given letter in the Available Letters field since each letter is treated uniquely in terms of producing permutations.

ABC Example

Given the letters a,b,c the possible permutations are

abc,bac,bca,acb,cab,cba,ab,ba,ac,ca,a,bc,cb,b,c,

but the only valid words are

ab, ba, bac, cab

At this point the remaining words will be returned if the Anagram Finder option has been selected.

Scrabble Solver

If you selected the Scrabble Solver option one more step is needed before returning the results. This step further filters the list of anagrams to cut out any which do not match the pattern which the user entered. It is implemented using the filter command and takes the contents of the Pattern field to use as the regular expression.

Question marks must be used to represent wildcards, for example the pattern 'a?' returns all valid 2 letter words starting with a. In the example above this is aa and ab. This is the list of valid 2 letter words beginning with a which can be made with the given letters, in this case aa is possible because we assume that the a given in the pattern is already on the board and the second a comes from the list of tiles we have in our hand.

Using the Anagram Finder

If you are using the Anagram Finder at least one character must be placed in the available letters field. The returned anagrams will contain only the characters which you enter. Clicking Search will start the application and cause any results to appear in the field below the button.

Using the Scrabble Solver

When using the Scrabble Solver both a pattern and list of available characters are required. The characters represent the tiles you currently hold and the pattern an opening on the board( i.e. t *blank slot* *blank slot* *blank slot*). Blank slots (i.e. wildcards) must be entered as question marks and will be interpreted as spaces which need to be filled by one of your tiles. Once you have entered both a pattern and your tiles click Search to retrieve word suggestions. An example of a valid search is shown in the screenshot.