/// <summary> /// Builds up the specified boggle dictionary from a list of words /// </summary> /// <param name="wordList">The source list of words</param> /// <returns>The boggleDictionary head node</returns> public static BoggleDictionaryNode BuildDictionary(string[] wordList) { var RootNode = new BoggleDictionaryNode('\0', false); for (int x = 0; x < wordList.Length; x++) { InsertWord(wordList[x], RootNode); } return(RootNode); }
List <string> IBoggleService.GetBoggleAnswers(BoggleDataContract input) { // NOTE: Here is where there should be a large amount of logic for error handling and other checking // Because this is a code sample, I chose not to include it for the sake of time. In a production service // There would be a large number of checks looking for propper inputs and responding appropriatelyj // This isn't thread safe, I need to look further into how WCF services handle threads and locking if (dictionary == null) { dictionary = BoggleLibraryUtils.BuildDictionary(CodeSampleCoreUtils.ReturnWordList()); } return(BoggleSolver.SolveBoggle(input.Height, input.Width, input.Board, input.MinWordSize, dictionary)); }
/// <summary> /// Determines if a word can be found in the boggle dictionary /// </summary> /// <param name="word">The Word to search for</param> /// <param name="root">The root node of the boggle dictionary</param> /// <returns>True if the word is in the dictionary, false otherwise</returns> public static bool ContainsWord(string word, BoggleDictionaryNode root) { BoggleDictionaryNode current = root; foreach (char n in word.ToLower()) { if (!current.Leaves.ContainsKey(n)) { return(false); } current = current.Leaves[n]; } return(current.isWord); }
/// <summary> /// Inserts a word into the boggle dictionary /// </summary> /// <param name="word">The word to place</param> /// <param name="root">The root node of the boggle dictionary</param> private static void InsertWord(string word, BoggleDictionaryNode root) { BoggleDictionaryNode current = root; foreach (char n in word.ToLower()) { if (!current.Leaves.ContainsKey(n)) { current.Leaves.Add(n, new BoggleDictionaryNode(n, false)); } current = current.Leaves[n]; } current.isWord = true; }