[Advanced] Arrays and Double For Loops

0 favourites
  • 3 posts
From the Asset Store
SynthWave Loop Pack
$3.50 USD
50% off
SynthWave Loop Pack includes 68 seamless loops, founded on 11 original melodies.
  • Hi, I am creating a "Letter Peak" or "Wordscapes" type game where the user is given SIX letters, and they have to make words that will be checked with an array of English dictionary.

    Well, that part is done. Works great.

    However, when the random letters are assigned, I want an Array to store all possible valid words that can be made from these letters. This is where I'm getting stuck. I tried a method with double-for-loop that iterates each permutation of these random 6 letter words with each word of english dictionary, it does work but it gets stuck for 10 seconds to do so. That's because my Dictionary array is 14k words, and combined permutations of the 6 letters are also 1000+. So, some millions of iterations.

    Summary:

    I want to reduce these iterations to extract and store all valid English words from the combinations of randomly given 6 letters.

    Any help will be appreciated!

  • Try Construct 3

    Develop games in your browser. Powerful, performant & highly capable.

    Try Now Construct 3 users don't see these ads
  • The first thing that comes to mind - don't search for the whole 6 letter permutation.

    Say, your letters are D F G A U N.

    Select the first letter - D.

    Select the second letter - F

    Check if array contains any words starting with "DF". There are no such words, so don't process this combination further, stop the loop for "F" and go to next letter "G".

    Check if array contains any words starting with "DG".

    and so on...

  • In the same order of idea as above, you may use the find(src,text) expression in your search.

    You will have to test against all 14k of your array, which will take long nonetheless, but hopefully, this could reduce the used time.

    For each stored word, check if the random letters are part of it or not.

    In pseudo code

    Var NBLetter
    
    Main loop through the array (XWords)
    => NBLetter = 0
    ..Second loop through your array containing the picked letters (XLetters)
    ....If find(SecondArray.at(XLetters), FirstArray.at(XWords)) = -1
    => Next XWords
    => Reset XLetters
    ....Else
    => NBLetter+1
    ......If NBLetter > SecondArray.Width
    => Save the current FirstArray.At(XWords) as a possible word
    ......Else
    => Next XLetters
    [/code:ypi2w8xq]
    
    This is supposed to check if the random picked letter exists in the current word. If it doesn't, go to the next word.
    There is still way to optimize (doesn't check if letters are duplicate or not) but this would be a first way to go.
    
    The iteration will still take time nonetheless.
Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)