Exemplo n.º 1
0
        private List <ScrabbleTreeNode> FindWordTreeChildren(ScrabbleTreeNode node, List <char> remainingLetters)
        {
            List <ScrabbleTreeNode> children = new List <ScrabbleTreeNode>();

            if (node.children.Any(o => o == (char)sChar.EOW))
            {
                children.Add(new ScrabbleTreeNode((char)sChar.EOW));
            }
            foreach (char letter in remainingLetters)
            {
                if (node.children.Any(o => o == letter))
                {
                    var remaining = new List <char>(remainingLetters);
                    remaining.Remove(letter);
                    List <ScrabbleTreeNode> leaves = new List <ScrabbleTreeNode>();
                    foreach (ScrabbleTreeNode matchingChild in node.children.Where(o => o == letter))
                    {
                        leaves = FindWordTreeChildren(matchingChild, remaining);
                        if (leaves.Count() > 0)
                        {
                            children.Add(new ScrabbleTreeNode(matchingChild.value)
                            {
                                children = leaves, state = new ScrabbleTreeNode.State()
                                {
                                    fromWildCard = (letter == (char)sChar.wildCard)
                                }
                            });
                        }
                    }
                }
            }
            return(children);
        }
Exemplo n.º 2
0
        public void helpOut()
        {
            //var generator = new TreeGenerator(@"E:\Scrabble\Word List.txt");
            //var generator = new TreeGenerator(@"E:\Scrabble\TestList.txt");
            //generator.CreateTreeFile(@"E:\Scrabble\TreeFileTest.txt");

            var a = new ScrabbleTreeNode('A');
            var b = new ScrabbleTreeNode('B');

            Console.WriteLine(a == b);


            var tree = new ScrabbleTree();

            //tree.Read(@"E:\Scrabble\TreeFileTest.txt");
            tree.Read(@"C:\git\ScrabbleGenerator\TreeFile.txt");

            var wordTree = tree.FindWordTree(new char[] { 'P', 'E', 'E', 'K' });
            var wordList = wordTree.ToWordList();

            foreach (var word in wordList)
            {
                Console.WriteLine(word);
            }
        }
Exemplo n.º 3
0
        private int countChildren(ScrabbleTreeNode node)
        {
            int count = 1;

            foreach (ScrabbleTreeNode child in node.children)
            {
                count += countChildren(child);
            }
            return(count);
        }
Exemplo n.º 4
0
        private List <StringBuilder> BuildWordList(ScrabbleTreeNode node)
        {
            List <StringBuilder> wordList = new List <StringBuilder>();

            if (node == (char)sChar.EOW)
            {
                wordList.Add(new StringBuilder());
            }
            foreach (ScrabbleTreeNode child in node.children)
            {
                wordList.AddRange(BuildWordList(child).Select(o => node == (char)sChar.root ? o : o.Append(node.value)));
            }
            return(wordList);
        }
Exemplo n.º 5
0
        private IList <ScrabbleTreeNode> GenerateChildren(SortedSet <string> wordsAlongPath, int depth)
        {
            List <ScrabbleTreeNode> nodes = new List <ScrabbleTreeNode>();

            while (wordsAlongPath.Count > 0)
            {
                if (wordsAlongPath.First().Length == depth)
                {
                    nodes.Add(new ScrabbleTreeNode('#'));
                    wordsAlongPath.Remove(wordsAlongPath.First());
                }
                if (wordsAlongPath.Count > 0)
                {
                    char newPathChar = wordsAlongPath.First()[depth];
                    var  newNode     = new ScrabbleTreeNode(newPathChar);
                    var  nextpath    = new SortedSet <string>(wordsAlongPath.Where(o => o[depth] == newPathChar));
                    wordsAlongPath.RemoveWhere(o => nextpath.Contains(o));
                    newNode.children = GenerateChildren(nextpath, depth + 1);
                    nodes.Add(newNode);
                }
            }

            return(nodes);
        }
Exemplo n.º 6
0
 public ScrabbleTree()
 {
     root = new ScrabbleTreeNode((char)sChar.root);
 }