Alexchandriyaa P
Alexchandriyaa P

Reputation: 11

unity slow down and freeze,became unplayable

unity stopped working when i hit play in my project am trying to list possible combinations(all type of combinations like 3 letter word, 4 letter word, 5 letter word, upto 10 letter) of randomly generated 10 letter word for that am using nested for loops and i call functions in start function

public void CheckingFiveLetter()
{

    for (int i = 0; i < convertStoredLetters.Length; i++) 
             //convertStoredLetters is consonants and vowels which i get at 
    runtime
    {
        for (int j = 0; j < convertStoredLetters.Length; j++) 
        {
            for (int k = 0; k < convertStoredLetters.Length; k++) 
            {
                for (int l = 0; l < convertStoredLetters.Length; l++)
                {
                    for (int m = 0; m < convertStoredLetters.Length; m++) 
                    {

                        if (i != j && i != k && i != l && i != m  && j != i && j != k && j != l && j != m  && k != i && k != j && k != l && k != m  && l != i && l != j && l != k && l != m  && m != i && m != j && m != k && m != l) 
                        {
                            letterword5.Add (convertStoredLetters.Substring (i, 1) + convertStoredLetters.Substring (j, 1) + convertStoredLetters.Substring (k, 1) + convertStoredLetters.Substring (l, 1) + convertStoredLetters.Substring (m, 1));
                            letterword5.Add (convertStoredLetters.Substring (j, 1) + convertStoredLetters.Substring (k, 1) + convertStoredLetters.Substring (l, 1) + convertStoredLetters.Substring (m, 1) + convertStoredLetters.Substring (i, 1));
                            letterword5.Add (convertStoredLetters.Substring (k, 1) + convertStoredLetters.Substring (l, 1) + convertStoredLetters.Substring (m, 1) + convertStoredLetters.Substring (i, 1) + convertStoredLetters.Substring (j, 1));
                            letterword5.Add (convertStoredLetters.Substring (l, 1) + convertStoredLetters.Substring (m, 1) + convertStoredLetters.Substring (i, 1) + convertStoredLetters.Substring (j, 1) + convertStoredLetters.Substring (k, 1));
                            letterword5.Add (convertStoredLetters.Substring (m, 1) + convertStoredLetters.Substring (i, 1) + convertStoredLetters.Substring (j, 1) + convertStoredLetters.Substring (k, 1) + convertStoredLetters.Substring (l, 1));


                            letterword5.Add (convertStoredLetters.Substring (i, 1) + convertStoredLetters.Substring (m, 1) + convertStoredLetters.Substring (l, 1) + convertStoredLetters.Substring (k, 1) + convertStoredLetters.Substring (j, 1));
                            letterword5.Add (convertStoredLetters.Substring (j, 1) + convertStoredLetters.Substring (i, 1) + convertStoredLetters.Substring (m, 1) + convertStoredLetters.Substring (l, 1) + convertStoredLetters.Substring (k, 1));
                            letterword5.Add (convertStoredLetters.Substring (k, 1) + convertStoredLetters.Substring (j, 1) + convertStoredLetters.Substring (i, 1) + convertStoredLetters.Substring (m, 1) + convertStoredLetters.Substring (l, 1));
                            letterword5.Add (convertStoredLetters.Substring (l, 1) + convertStoredLetters.Substring (k, 1) + convertStoredLetters.Substring (j, 1) + convertStoredLetters.Substring (i, 1) + convertStoredLetters.Substring (m, 1));
                            letterword5.Add (convertStoredLetters.Substring (m, 1) + convertStoredLetters.Substring (l, 1) + convertStoredLetters.Substring (k, 1) + convertStoredLetters.Substring (j, 1) + convertStoredLetters.Substring (i, 1));



                        }   
                    }
                }
            }
        }
    }

    //checking if the combination is present in the word dictionary or not 
    if present add count
    for (int m = 0; m < letterword5.Count; m++)
    {
        bool Checkfi = wordRan.CheckWord (letterword5 [m].ToLower ());
        if (Checkfi == true) 
        {
            fiveLetterWord.Add (letterword5 [m].ToLower ());

        }

    }
    fi = fiveLetterWord.Distinct ().ToList ();

    wordRan.storeFiveWordCount = fi.Count;
    five.text = "Five Possible words : " + fi.Count.ToString ();
    letterword5.Clear ();
    fiveLetterWord.Clear ();

}

similarly for length of words

Upvotes: 0

Views: 153

Answers (2)

basklein
basklein

Reputation: 395

Like Vidmantas commented, that many for loops will kill your computer. I'd recommend trying to simplify it a bit. Are you trying to generate just one string or all possible strings for a certain amount of letters?

If you just want one string, the best way to handle it is to have one for loop like this:

public string RandomWord (int stringLength)
{
    string word = "";

    for (int i = 0; i < stringLength; i++)
    {
        int num = random.Range(0, 26);
        char letter = (char)('a' + num);
        word += letter;
    }

    return word;
}

reference: https://www.dotnetperls.com/random-lowercase-letter

If you want to get a list of all possible n-letter strings, I'd suggest doing it like this:

public string[] WordList (int stringLength)
{
    List<string> wordList = new List<string>();
    List<byte> letterCounter = new List<byte>();
    String word = "";

    for (int i = 0; i < stringLength; i++)
    {
        letterCounter.Add(0);
    }

    while (letterCounter[0] < 25)
    {
        for (int i = 0; i < stringLength; i++)
        {
            char letter = (char)('a' + letterCounter[i]);
            word += letter;
        }

        wordList.Add(word);
        letterCounter[stringLength]++;

        for (int i = stringLength; i > 0; i--)
        {
            if (letterCounter[i] >= 26)
                if (i > 0)
                    letterCounter [i - 1]++;
        }
    }

    return wordList.ToArray();
}

If you want to get a list of all dictionary words of a certain length, I'd suggest setting up a dictionary database and getting them from there.

Hope this helped!

Upvotes: 1

basklein
basklein

Reputation: 395

Edit: While testing the code I realized, this is not exactly the code you're looking for because it gives every possibility. Also ones that use the same letter multiple times. I need to think a bit more on how to improve it. If anyone does know how to do it, feel free to show it.

Ok, so if I understand your comments correctly, then this should be what you want.

public List<string> WordList (int stringLength, string inputWord)
{
    List<string> wordList = new List<string>();
    List<byte> letterCounter = new List<byte>();
    String word = "";

    char[] letters = inputWord.ToCharArray();

    for (int i = 0; i < stringLength; i++)
    {
        letterCounter.Add(0);
    }

    while (letterCounter[0] < 10)
    {
        for (int i = 0; i < stringLength; i++)
        {
            char letter = letters[letterCounter[i]];
            word += letter;
        }

        wordList.Add(word);
        letterCounter[stringLength]++;

        for (int i = stringLength; i > 0; i--)
        {
            if (letterCounter[i] >= 10)
                if (i > 0)
                    letterCounter [i - 1]++;
        }
    }

    return wordList;
}

Now you just have to compare the resulting list to your dictionary and throw out the not-words.

Upvotes: 0

Related Questions