Let`s say we have PIGGY on our Scrabble tablet. To use the algorithm I suggested, we want to get all possible “Key” values from PGGY. In our WordEntry object list, we created the key field by sorting the letters of the word alphabetically. We have to do the same with the letters on our tray. Now we can match the number of letters in the test word with the letters available to me: Knowing that the word began and ended with an “s”, I did the following: Regarding your initial question, you take “piggy” as an input and you want to find all kinds of words that can be made from those letters. The combination extension method for “piggy” gives you a list like this: The problem here is that my list of occurrences (elephant, I misspelled “occurrence” in the code) of each letter may not contain the letter in the test word. If we want to find all the possible words that can be formed from these letters in a series of letters, we must be able to generate all possible combinations of letters. We can test any of them against the “dictionary” (quotes, because there is REALLY no dictionary in the. NET sense is a list (or sequence) of WordEntry objects). To make sure that the keys (from the sequence of letters we “drawn” to Scrabble) are compatible with the key field of the WordEntry object, we must first sort the letters. With the More Words website with the search “8-letter words starting with s”, I got 3480 words that meet these criteria.
But which one is suitable? Essentially, we created a table with the Word and Key columns (based on reading the Word file and calculating the key for each), and then we run a query that links Key to a sequence of key values that we generated (from the letters on our topic). So it`s pretty easy to create the list of words and keys we want to test (or Scrabble`s dictionary of valid words). If we look at our list of WordEntry objects (reading in the list of words with 2, 3, 4 letters), we will probably find that the following combinations are found: An alternative Linq statement, which is “simpler”, combines the parts together without creating the temporary anonymous type wordLetterCounts: This is the essence of the algorithm. The ToWordKey() function is essentially a hash function. All strings with the same number of letters and the same set of letters are hashed to the same value. So create a list of words and their hashes (Key – ToWordKey), and then specify a series of letters that can be used to create words, hash the letters (ToWordKey), and find all entries in the list with the same hash value. To find all the words of any length (for a series of letters), all you have to do is chop the input (send the entire string via ToWordKey), and then find all the combinations of any length. Since the combinations are generated from the hashed set of letters AND since the combination extension method maintains the original order of the letters in each combination, each combination retains the property of having been chopped! That`s pretty cool! Then, for each word list (for example, 3-letter words, 4-letter words, and so on), create a dictionary whose keys are the letters of the word in alphabetical order and whose value is the word. For example, the following word list: I recently found out I had a valid word with 8 letters (my 7 letters plus a letter on the board) – Word With Friends Wordfinder told me I could write the word with 8 letters – but I didn`t know what the word was.
These key values correspond to the following words: With this code and with a proper set of words, you should be able to take any combination of letters and find all the words that can be formed from them. You can control words by finding all words of a certain length or all words of any length. First, find lists of words (you say you`re going to compare with a list of words). Here`s a code that reads these words (I cut and pasted them into a txt file) and creates a list of WordEntry objects: For example (assuming the few words above make up your entire dictionary), if you had the letters `o` `g` `d` on your Scrabble tablet, you could form the words DOG and GOD because both have the GDO key. www.poslarchive.com/math/scrabble/lists/index.html Similarly, we do the same for every possible word (ToList() makes the debugger readable): For a series of letters, find all possible words of the same length as the key: For each combination of length K, check the dictionary if there are words with this key. If so, they are candidates to play. So how exactly do I match the available letters? Well, count them and make sure that the number of each letter in a possible word matches the number of letters available to me. This gives me a count of my letters: the InAlphabeticOrder method allows you to create a list of your input words (Scrabble dictionary), indexed by their “key” (similar to the dictionary, but several words could have the same key). Nothing special here, just read the file, break it up into words, and create a new WordEntry for each word. Perhaps it could be more effective here by reading one line at a time.
The list also gets quite long if you consider the words of 5, 6 and 7 letters. This may or may not be a problem. For a toy or game, it`s probably okay. If you feel like it, you can create a small database with words and keys. Use the GetWords function above and the link to the 2-, 3-, and 4-letter words to create the list of WordEntry objects.