Beispiel #1
0
        public bool ContainsWord(string word)
        {
            DigitalTreeNode currNode = RootNode;

            foreach (char ch in word)
            {
                if (!currNode.NextNodes.ContainsKey(ch))
                {
                    return(false);
                }

                currNode = currNode.NextNodes[ch];
            }

            return((currNode == null) ? false : currNode.IsCompleteWord);
        }
Beispiel #2
0
        public List <List <WordPathNode> > FindWords(int currRow, int currCol,
                                                     List <List <WordPathNode> > results, DigitalTreeNode currNode, List <WordPathNode> currPath, HashSet <int> visitorTracker, HashSet <DigitalTreeNode> foundWords)
        {
            if (currNode.IsCompleteWord && !foundWords.Contains(currNode))
            {
                if (currPath.Count >= MinimumWordLength)
                {
                    AddPathToList(results, currPath);
                }

                // Keep track of found words so we don't add duplicates
                foundWords.Add(currNode);
            }

            foreach (int nextIndex in GetNeighbors(currRow, currCol))
            {
                if (visitorTracker.Contains(nextIndex))
                {
                    continue; // We've already visited this node
                }
                int             nextRow  = GetRow(nextIndex);
                int             nextCol  = GetColumn(nextIndex);
                char            nextChar = BoggleBoard.GetCharacter(nextRow, nextCol);
                DigitalTreeNode nextNode = null;

                if (!currNode.NextNodes.ContainsKey(nextChar))
                {
                    continue;
                }

                nextNode = currNode.NextNodes[nextChar];
                // TODO: Account for the "U" that follows "Q" on the "QU" dices

                currPath.Add(new WordPathNode(nextRow, nextCol, nextChar));
                visitorTracker.Add(nextIndex);

                // Recurse into neighbors with viable prefixes
                FindWords(GetRow(nextIndex), GetColumn(nextIndex), results, nextNode, currPath, visitorTracker, foundWords);

                visitorTracker.Remove(nextIndex);
                currPath.RemoveAt(currPath.Count - 1);
            }

            return(results);
        }
Beispiel #3
0
        private IEnumerable <string> InnerEnumerator(DigitalTreeNode currNode, StringBuilder prefix)
        {
            if (currNode.IsCompleteWord)
            {
                yield return(prefix.ToString());
            }

            foreach (char ch in currNode.NextNodes.Keys)
            {
                prefix.Append(ch);

                foreach (string word in InnerEnumerator(currNode.NextNodes[ch], prefix))
                {
                    yield return(word);
                }

                prefix.Remove(prefix.Length - 1, 1);
            }
        }
Beispiel #4
0
        private void LoadWord(string word)
        {
            DigitalTreeNode currNode, newNode;

            var upperWord = word.ToUpper();

            currNode = RootNode;

            foreach (char ch in upperWord)
            {
                if (currNode.NextNodes.ContainsKey(ch))
                {
                    currNode = currNode.NextNodes[ch];
                }
                else
                {
                    newNode = new DigitalTreeNode();
                    currNode.NextNodes.Add(ch, newNode);
                    currNode = newNode;
                }
            }

            currNode.IsCompleteWord = true;
        }
Beispiel #5
0
 public DigitalTree()
 {
     RootNode = new DigitalTreeNode();
 }