de en es fr
Let the machine help
Light teasing, exhibition, BDSM, sissyfication, watersports... with sounds and pictures


Post new topic Reply to topic  [ 3 posts ] 
Author Message
 Comparing a value - can you disregard case?
PostPosted: Mon Jun 15, 2015 5:52 pm 
Offline
Veteran
User avatar
Is it possible to ignore case sensitivity when comparing a user's text input to a value?

For example, I'm using the code below to pick a random question to ask the player, taking the questions and answers from a pair of arrays:

Code:
def limitTest = 3
def questionsTest = ["What colour is an orange?",
   "Who coded SexScripts?",
   "What is the answer to life, the universe and everything?"]
def answersTest = ["orange",
   "Doti",
   "42"]
def questionSelect = getRandom(limitTest)
def questionAsked = questionsTest[questionSelect]
def answerCorrect = answersTest[questionSelect]
def question = getString("The question is:\n"+questionAsked+"\nAnswer?","")
if(question==answerCorrect) {
   show("Correct!")
   wait(3)
   correct = true
} else {
   show("I'm sorry! That's the wrong answer!\n\n The correct answer was '"+answerCorrect+"'.")
   wait(5)
   correct = false
}
questionArray.remove(questionAsked)
answerArray.remove(answerCorrect)
questionLimit --

Is there any way to easily make it so that, (for example) if the "What colour is an orange?" question is asked, the player can input either "orange" or "Orange" and be correct?

Or would this take a lot of extra coding to achieve?

My main problem is that I need to get the questions and answers from arrays, rather than using a simple "if(question=="orange" || question=="Orange")".

I've always assumed an "if x==y" had to be identical, and other cases needed to be specified ("if... else if" etc.) but is there a trick one can pull to make the comparison not care about case sensitivity?


Top
 Profile Send private message 
 
 Re: Comparing a value - can you disregard case?
PostPosted: Mon Jun 15, 2015 6:55 pm 
Offline
Veteran
User avatar
would do it as:
Code:
  if(question.toUpperCase() == answerCorrect.toUpperCase() ) {


but think you can also do:

Code:
if (question.equalsIgnoreCase(answerCorrect)) {

_________________
Liz

You can't take something off the Internet - it's like taking pee out of a pool.
https://play-clan.site profile: Liz


Top
 Profile Send private message 
 
 Re: Comparing a value - can you disregard case?
PostPosted: Tue Jun 16, 2015 7:01 am 
Offline
Veteran
User avatar
Thanks, Liz! Awesome as always!

Used the second one, and it worked perfectly.

EDIT:
After further work, I ended up going with Liz's first example instead, so to allow "wildcards" in the player's answers. For anyone else who wants to do this, I used:

Code:
if(question.toLowerCase().contains(answerCorrect.toLowerCase())) {

This lets me ask (for example) "What was the sequel to the movie Terminator called?" with the correct answer being "Terminator 2"... and the player can answer "Terminator 2" or "Terminator 2: Judgement Day" or "terminator 2 judgement day" and all will be correct.


Top
 Profile Send private message 
 
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 3 posts ] 

All times are UTC + 1 hour [ DST ]


Who is online

Users browsing this forum: No registered users and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Search for:
Jump to:  


Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
Maroon Fusion theme created by Oxydo
Software, theme modifications, phpBB modification by Doti 2010 - 2020
This website uses session cookies only.