private IEnumerable <string> EnumerateWords(IEnumerable <Letter> letters, WordBuilder builder) { foreach (Letter letter in letters) { // Only continue with this letter if the prefix exists if (Trie.Matcher.HasNext(letter.C)) { // Add the current letter to the prefix Trie.Matcher.Next(letter.C); // If the current prefix is a word, capture it if (Trie.Matcher.IsMatch()) { string word = Trie.Matcher.PrefixMatched; yield return(word); } // Create a new word builder to descend with WordBuilder nextBuilder = new WordBuilder(builder); nextBuilder.AddLetter(letter); // Get adjacent letters that haven't already been used in this word IEnumerable <Letter> nextLetters = letter.AdjacentLetters.Where(x => !nextBuilder.UsedLetters.Contains(x)); // Descend into the adjacent letters foreach (string word in EnumerateWords(nextLetters, nextBuilder)) { yield return(word); } // Remove the current letter from the prefix Trie.Matcher.Back(); } } }
private IEnumerable <string> EnumerateWords(BoggleBoard boggleBoard) { // Create structure to manage adjacent letters Board board = CreateBoard(boggleBoard); // Find all the words WordBuilder builder = new WordBuilder(); foreach (string word in EnumerateWords(board.Letters, builder)) { yield return(word); } }
public WordBuilder(WordBuilder builder) { // Copy the items, but not a deep copy UsedLetters = new HashSet <Letter>(builder.UsedLetters); }