Beispiel #1
0
        /// <summary>
        /// MorphChain method creates a List of Chains that holds a Lists of string.
        /// dynamically creating a new List of strings for every unique morph word and building it
        /// on top of the previous List; adding it to our List of Chains to find the end chain word
        /// WARNING: Takes forever to compile long chains because it searches through all the lists of chains until it finds the end word.
        /// These lists are dynamically added to the List of chains and has an expontential growth rate.
        /// </summary>
        /// <param name="start"></param>
        /// <param name="end"></param>
        /// <param name="max"></param>
        public List <string> MorphChain(string start, string end, int max)
        {
            if (!Lines.Contains(start) || !Lines.Contains(end) || start.Length != end.Length)
            {
                return(null);
            }
            Console.WriteLine("Solution Chain");
            Words morphSet = new Words();
            List <List <string> > totalChains = new List <List <string> >().Distinct().ToList(); //List of string arrays
            List <string>         setOne      = new List <string>(morphSet.MorphWord(start));    //starting word {e.g told: {bold, cold, fold, gold...}
            List <string>         wordsFound  = new List <string>();
            List <string>         foundMorph  = new List <string>();

            for (int i = 0; i < setOne.Count; i++)
            {
                //temp list to stores the start word and the current indexed morph word
                List <string> temp = new List <string>();
                temp.Add(start);                //add start word
                temp.Add(setOne[i]);            //add that specific index word
                totalChains.Add(temp);          //totalChains gets e.g 0: {told, bold}, 1: {told, cold}, 2: {told, sold}...
                wordsFound.Add(setOne[i]);
            }
            int x = 0;

            //while x is less than the number of elements inside totalchains...
            while (x < totalChains.Count)
            {
                List <string> chain = totalChains[x]; //assign chain to that specfic x index
                if (chain.Count <= max)               // while the number of elements in chain is less than the max length provided
                {
                    if (chain.Contains(end))
                    {
                        foundMorph = chain;
                        break;
                    }
                    if (foundMorph.Contains(end)) //if the end word/word we're looking for is inside chain
                    {
                        //found the morph list; gets that chain; breaks out of loop
                        break;
                    }
                    List <string> nextChain = new List <string>(morphSet.MorphWord(chain.Last())); //nextChain gets the morphed word of the last word in chain

                    //iterate through the morph word
                    for (int i = 0; i < nextChain.Count; i++)
                    {
                        if (!wordsFound.Contains(nextChain[i]))
                        {
                            //temp gets every element inside chain; copies all of its contents
                            List <string> temp = new List <string>();
                            foreach (string element in chain)
                            {
                                temp.Add(element);
                            }
                            temp.Add(nextChain[i]); //temps adds the last index of next chain
                            if (nextChain[i].Equals(end))
                            {
                                foundMorph = temp;
                                break;
                            }
                            totalChains.Add(temp); //total chains adds new list temp
                            wordsFound.Add(nextChain[i]);
                        }
                    }
                }
                else if (chain.Count >= max)
                {
                    break;
                }

                x++; //x will increment until it hits the max count in total chains
            }
            return(foundMorph);
        }