예제 #1
0
        private List <ScrabbleNode> SearchForScrabblePath(ScrabbleNode node, string word)
        {
            var path = new List <ScrabbleNode>();

            if (!string.IsNullOrWhiteSpace(word))
            {
                if (node.Value == word[0])
                {
                    path.Add(node);
                    if (node.Children != null && word.Length > 1)
                    {  // keep going and search children for the remainder of the word.
                        foreach (var child in node.Children)
                        {
                            if (child.Value == word[1])
                            {
                                path.AddRange(SearchForScrabblePath(child, Chop(word)));
                                break; // stop processing this level.
                            }
                        }
                    }
                }
            }

            return(path);
        }
예제 #2
0
        private void AddImpl(ScrabbleNode head, string path)
        {
            if (!string.IsNullOrWhiteSpace(path))
            {
                ScrabbleNode pathNode = null;
                if (head.Children == null)
                { // first child
                    pathNode      = new ScrabbleNode(path[0]);
                    head.Children = new List <ScrabbleNode>();
                    head.Children.Add(pathNode);
                }
                else
                {
                    // Look for existing child path.
                    foreach (var child in head.Children)
                    {
                        if (child.Value == path[0])
                        {
                            pathNode = child;
                            break; // stop searching.
                        }
                    }

                    if (pathNode == null)
                    { // new child path.
                        pathNode = new ScrabbleNode(path[0]);
                        head.Children.Add(pathNode);
                    }
                }

                AddImpl(pathNode, Chop(path));
            }
            else
            {
                head.IsEnd = true; // mark this node as the end of a word.
            }
        }