0 votes
by (140 points)

Hi guys,

I'm creating a game with puzzles. On every passage you'll have clues and need to fill in a textbox with the correct answer to be able to continue forward to the next passage/puzzle.

I allready have the code that works for one specific answer. But I would like to like to have a little change so I can easily ad more correct answer for example.

If the correct answer is ''oakwood va clinic'' I would like that ''oakwood clinic'' and ''oakwood va'' is also the right awnser to proceed.

Hope you guys can help me!
 

this is my current code:

<<set $answer to ''>>\
<<textbox '$answer' '' autofocus>>
<span id='textbox-reply'></span>
​
<span id='textbox-submit'>\
    <<button 'Send Intel'>>
        <<set $answer to $answer.trim().toLowerCase().replace(/\s\s+/g, ' ')>>
        <<if $answer is "oakwood va clinic">>
    
            <<replace '#textbox-reply'>>\
            
                Correct!\
                <</replace>>
            <<replace '#textbox-submit'>>\
                [[Mission 3 Succeeded]]\
                        
            <</replace>>
            <<run $('#textbox-answer').attr('readonly', 'true');>>
            
        <<else>>
            <<replace '#textbox-reply'>>\
                Incorrect.  Please try again.\
            <</replace>>
        <</if>>
    <</button>>\
</span>

 

1 Answer

+1 vote
by (68.6k points)

Aside from attempting to write a grammar parser to decipher the player's answer, which is likely far more work than you're prepared to do, the simplest solution would be to check it against an array of valid answers.

Here's an example of how you might do it with an array and the <Array>.includes() method:

<<if [
	"oakwood va clinic",
	"oakwood clinic",
	"oakwood va"
].includes($answer)>>

 

PS: Alternatively.  You could use a regular expression, but that's easier to screw up, so….

by (140 points)
edited by

Thanks I just missed the obvious fact I could use comma's in my code to put more options as the answer.

I simply changed:

<<if $answer is "oakwood va clinic">>

to:

<<if $answer is "oakwood va clinic","oakwood clinic","oakwood va">>

 

Thanks so much!

 

EDIT: doesn't work afterall. This makes it work with any random answer.

by (63.1k points)
What you just posted doesn't work. The if statement is just always true now. Try typing "blah" in, and it will accept the answer.
by (140 points)
edited by

Crap you're right I didn't try any wrong inputs... :( stuck again

Where do I put the provided code into mine? It keeps giving me errors.

<<if [
	"oakwood va clinic",
	"oakwood clinic",
	"oakwood va"
].includes($answer)>>

 

by (68.6k points)

You replace the opening <<if>> from your code example.  I.e.:

<<if $answer is "oakwood va clinic">>

 

by (140 points)
Thanks! It works!

It's almost 2am here so that might be also part of the problem ;)
...