How do I check if an array contains some words?

0 favourites
  • 5 posts
From the Asset Store
Supports 1D, 2D, 3D arrays. Import and export arrays in JSON format
  • Hi there,

    I'm not too good with arrays (yet... ), so I hope someone can help me out here...

    I'm working on a project in which a player inputs some words; these words are then added to an array.

    Then a second player adds new words, but he has to use all the words that are already added to the array, therefore I want to check if all of the words in this array are used in some textfield.

    The textfield may contain other words, but ALL of the words in the array should be used in the textfield.

    I hope I'm making sense here...

    Thanks in advance!

  • https://copy.com/DKdeYAd8omHJzITq

    add adds the words to the array

    finish and compare will finish the "sentence" and compare if all words from the last finish and compare were present

    note: you might want to add a lowercase conversion to not differentiate between "word" and "Word"

  • Hi mindfaQ,

    thanks a lot for the work you've put into this example!!

    I've been able to reproduce the project (simply by typing what YOU typed )

    Now I'll have to try to figure out what's really happening here...as I mentioned in my post: I'm not too good at arrays...

    Any tutorials or 'reading stuff' you could suggest on arrays?

    Thanks again!!

  • Try Construct 3

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

    Try Now Construct 3 users don't see these ads
  • In this example the array is used just like a spread sheet. So imagine one.

    First column = word

    Second column = number of times it was present in the previous sentence

    Third column = number of times it is present in the current sentence

    (remember: first column has the Y-index 0)

    Each word has its own row

    Function add_words

    * event 2: checks if the word can be found in any cell in the spread sheet (was already present in the previous sentences, so to say)

    * event 3: if it does, it loops through all rows (X) and looks in the first column, if the word matches (this is to find out the row's index, which contains the word)

    then it adds 1 to the word count in the third column

    * event 4: if the word is not contained, the word gets a new row for its own (that's what the push does); since it is a new word, it was not contained in the previous sentence, so the word count in column 2 is 0 and in column 3 is 1

    * event 5-6: just loops through the textbox and runs the add_words function for each word; use lowercase(tokenat...) to prevent discerning between Word, word and WORD.

    * event 7 and following will do the check, if all words from the previous sentence are contained in the new sentence

    * event 8: loops through each row (= each word)

    * event 9: if the word was contained at least as many times in the new sentence as in the old one -> everything is fine, the values are set in a way, that the word count of the new sentence becomes the word count for the old sentence

    * event 10: if a word was missing one or more times, it adds the amount of times it was missing to the variable wrong

    * event 11: checks if no word was missing and throws a success message

    * event 12: if at least 1 word was missing, it throws an error message

    check the expressions for arrays, if you don't understand one of the actions: https://www.scirra.com/manual/108/array

    most important are imo array.at(x,y,z) to read values from the array and, when looping, array.curx, array.cury and array.curz. Width, heigth, depth can help too, but were not really needed in this case.

  • Hi mindfaQ,

    many thanks again for your reply!

    At the moment I'm working on an other project.

    As soon as I revert to this project again, I'll try to follow/understand what you're doing in attached project.

    THANKS!

Jump to:
Active Users
There are 1 visitors browsing this topic (0 users and 1 guests)