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);
        }
Beispiel #2
0
        /// <summary>
        /// One click on the right "Fill" button fills the lbWordSearch listbox with our entire resource "WordList.txt" file.
        /// Depending on what radio button is checked (Rhyme, Scrabble, Morph Word, or Morph Chain), the lbWordSearch list box will be filled with the designated function correlated to the word in the textboxes.
        /// If none of the radio buttons are checked, print out error message box.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnFill2_Click(object sender, EventArgs e)
        {
            if (!(rbRhyme.Checked || rbScrabble.Checked || rbMorphWord.Checked || rbMorphChain.Checked))
            {
                string msgStr = String.Format("Option not selected.");
                MessageBox.Show(msgStr);
            }

            // Finds words that rhyme with the word inside the textbox.
            // Used try/catch to detect error from user input.
            else if (rbRhyme.Checked)
            {
                lbWordSearch.BeginUpdate();
                lbWordSearch.Items.Clear();
                List <string> rhymeWords = word.RhymeWord(txtUser.Text);
                try
                {
                    if (rhymeWords.Count == 0 || string.IsNullOrWhiteSpace(txtUser.Text))
                    {
                        throw new Exception();
                    }
                    labelError.Visible = false;
                    foreach (string words in rhymeWords)
                    {
                        lbWordSearch.Items.Add(words);
                    }
                }
                catch (Exception)
                {
                    labelError.Text    = "Invalid Input";
                    labelError.Visible = true;
                }
                // If no words are found, print out message
                lbWordSearch.EndUpdate();
            }

            // Finds words that make up of letters inside the textbox.
            // Used try/catch to detect error from user input.
            else if (rbScrabble.Checked)
            {
                lbWordSearch.BeginUpdate();
                lbWordSearch.Items.Clear();
                List <string> wordScrabble = word.ScrabbleWord(txtUser.Text);
                try
                {
                    if (wordScrabble.Count == 0 || wordScrabble.Contains("") || string.IsNullOrWhiteSpace(txtUser.Text))
                    {
                        throw new Exception();
                    }
                    labelError.Visible = false;
                    foreach (string scrabble in wordScrabble)
                    {
                        if (scrabble != wordScrabble[0])
                        {
                            lbWordSearch.Items.Add(scrabble);
                        }
                    }
                }
                catch (Exception)
                {
                    labelError.Text    = "Invalid Input";
                    labelError.Visible = true;
                }
                // If no words are found, print out message
                lbWordSearch.EndUpdate();
            }

            // Find morph words of the word inside the textbox.
            // Used try/catch to detect error from user input.
            else if (rbMorphWord.Checked)
            {
                lbWordSearch.BeginUpdate();
                lbWordSearch.Items.Clear();
                string[] wordMorph = word.MorphWord(txtUser.Text);
                try
                {
                    // If no words are found, print out message
                    if (wordMorph.Length == 0 || string.IsNullOrEmpty(txtUser.Text))
                    {
                        throw new Exception();
                    }
                    labelError.Visible = false;
                    foreach (string morph in wordMorph)
                    {
                        lbWordSearch.Items.Add(morph);
                    }
                }
                catch (Exception)
                {
                    labelError.Text    = "Invalid Input";
                    labelError.Visible = true;
                }
                lbWordSearch.EndUpdate();
            }

            // Finds morph chain of start and end word with maximum chain length.
            // Used try/catch to detect error from user input.
            else if (rbMorphChain.Checked)
            {
                lbWordSearch.BeginUpdate();
                lbWordSearch.Items.Clear();
                try
                {
                    int           maxLength = Int32.Parse(tbMax.Text);
                    List <string> wordChain = word.MorphChain(txtUser.Text, tbEnd.Text, maxLength);
                    if (wordChain.Count == 0)
                    {
                        labelError.Text    = "No Solution";
                        labelError.Visible = true;
                    }
                    if (string.IsNullOrEmpty(txtUser.Text))
                    {
                        throw new Exception();
                    }
                    labelError.Visible = false;
                    foreach (string chain in wordChain)
                    {
                        lbWordSearch.Items.Add(chain);
                    }
                }
                catch
                {
                    labelError.Text    = "Invalid Input";
                    labelError.Visible = true;
                }
                lbWordSearch.EndUpdate();
            }
        }