예제 #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 void MorphChain(string start, string end, int max)
        {
            Console.WriteLine("Solution Chain");
            string alphabet = "abcdefghijklmnopqrstuvwxyz";                                             //used for replacing the index of morph word
            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, alphabet)); //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 (foundMorph.Contains(end))     //if the end word/word we're looking for is inside chain
                    {
                        //foundMorph = chain; //found the morph list; gets that chain; breaks out of loop
                        break;
                    }
                    List <string> nextChain = new List <string>(morphSet.MorphWord(chain.Last(), alphabet)); //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)
                {
                    Console.WriteLine("No Solution within given maximum Length.");
                    break;
                }
                x++; //x will increment until it hits the max count in total chains
            }

            //writes out the contents in the foundmorph chain
            foreach (string count in foundMorph)
            {
                Console.WriteLine(count);
            }
        }
예제 #2
0
        /// <summary>
        /// Interface of the Assignment.  Prints a menu for each option using a Switch statement.
        /// </summary>
        /// <param name="args"></param>
        static void Main(string[] args)
        {
            //create constructor for type Word class
            Words word = new Words();
            bool  exit = false;

            //While loop; only breaks out of the menu if exit = true;
            while (!exit)
            {
                Console.Write("1. All Words\n2. Rhyming words\n3. Scrabble words\n4. Morph words\n5. Morph chain\n6. Quit Program\nYour choice: ");

                //User input for to select a Option from the menu
                int input = int.Parse(Console.ReadLine());
                switch (input)
                {
                //Prints out all the content in the textfile using PrintAll method from Word Class.
                case 1:
                    word.PrintAll();
                    break;

                //Prompts user input and searches for words that end in the same input using the RhymeWord method from Word Class.
                case 2:
                    Console.Write("Let's see your freestyle! Pick a rhyme scheme: ");
                    string rhyme = Console.ReadLine();
                    word.RhymeWord(rhyme);         //Takes a string (user input) for the function.
                    break;

                //Prompts user input and searches for words that contain the same letters in the index using ScrabbleWord method from Word Class
                case 3:
                    Console.WriteLine("You don't know how to rap? Don't worry, I'll search a word that your looking for: ");
                    string scrabble = Console.ReadLine();
                    word.ScrabbleWord(scrabble);      //Takes a string (user input) for the function.
                    break;

                //Prompts user input and searches for words that differ from one letter in the index using MorphWord method from Word Class
                case 4:
                    Console.WriteLine("I'll try to morph your word into something cool.");
                    string   morph    = Console.ReadLine();
                    string   alphabet = "abcdefghijklmnopqrstuvwxyz";    //used for replacing the index of morph word
                    string[] set      = word.MorphWord(morph, alphabet); //Takes a string (user input) for the function.
                    int      index    = 0;
                    Console.WriteLine("Its Morphin' time!");
                    foreach (string s in set)
                    {
                        Console.WriteLine("Morph Word {0}: {1}", index, s);
                        index++;
                    }
                    break;

                //Breaks out of the loop
                case 5:
                    Console.Write("Enter Start Word: ");
                    string start = Console.ReadLine();
                    Console.Write("Enter end word: ");
                    string end = Console.ReadLine();
                    Console.Write("Enter maximum chain length: ");
                    int max = int.Parse(Console.ReadLine());
                    word.MorphChain(start, end, max);
                    break;

                case 6:
                    Console.WriteLine("Awesome job, thanks for playing!");
                    exit = true;
                    break;
                }
            }
            Console.ReadKey();
        }