Exemplo n.º 1
0
        /// <summary>
        /// Returns all the words contained in this graph that meet the criteria.
        /// Uses Queue to perform Breadth First Search.
        /// This is an expensive operation that should only be performed once during the
        /// creation of the WordList and only if a computer player exists.
        /// </summary>
        /// <param name="diff">The difficulty level.</param>
        /// <param name="lexicon">The wordList to use.</param>
        /// <returns>A list of strings.</returns>
        public static List <string> GetAllWords(Difficulty diff, Book lexicon)
        {
            List <string> wordList = new List <string>();
            var           Q        = new Queue <Tuple <DawgNode, string, int> >();

            Q.Enqueue(new Tuple <DawgNode, string, int>(dictionary[0], string.Empty, 0));

            while (Q.Count > 0)
            {
                var T = Q.Dequeue();

                for (int i = T.Item3; i < T.Item3 + T.Item1.TransitionSetSize; i++)
                {
                    DawgNode node = dictionary[i];
                    string   word = T.Item2 + node.Letter;

                    if (node.TestAcceptNode(diff, lexicon))
                    {
                        wordList.Add(word.ToString());
                    }

                    if (node.TransitionSetSize >= 1)
                    {
                        Q.Enqueue(new Tuple <DawgNode, string, int>(node, word, node.TransitionSetBeginIndex));
                    }
                }
            }

            wordList.Sort();
            return(wordList);
        }
Exemplo n.º 2
0
        /// <summary>
        /// Tests to see it the string can be found in the dictionary.
        /// </summary>
        /// <param name="str">The word to search for.</param>
        /// <param name="diff"></param>
        /// <param name="lexicon"></param>
        /// <returns>True if word is found, otherwise false.</returns>
        public static bool Contains(string str, Difficulty diff, Book lexicon)
        {
            // For the first letter in the string point to the correct node.
            DawgNode node = dictionary[str[0] - 65];

            // For each subsequent letter transition to the next node.
            foreach (char ch in str.Substring(1))
            {
                node = Transition(node, ch);
                if (node == null)
                {
                    return(false);
                }
            }

            // Check to see if the tranisiton path end a valid AcceptNode
            return(node.TestAcceptNode(diff, lexicon));
        }