Page 1 of 1

Random questions in quiz

Posted: Tue Jul 14, 2009 1:53 pm
by Stormy1
Hi There

I have a quiz with a button called 'Question'. When the user clicks on it, I need it to show a question on screen. There are 36 questions in total but appear on screen one at a time in the same location. However, the order in which the questions come up need to be random.

I have looked through the documentation and don't know how to achieve this. I don't know where to put the 36 questions and how to get the button to bring them up randomly into a field on screen.

Any help or pointers would be really appreciated!!

Thank you in advance

Posted: Tue Jul 14, 2009 2:11 pm
by Klaus
Hi Stormy1,

question, how and where did you store your questions?

Posted: Tue Jul 14, 2009 2:42 pm
by SparkOut
There's lots of ways you could do this. As Klaus says, the method of storing your questions (and the corresponding answers) makes a difference.

One way is to put the questions and answers into custom property sets of the stack or card, etc. Something like this is a quick and easy way, given a card with two fields ("fldQuestion" and "fldAnswer") and two buttons "Reset" and "Question":

Code: Select all

for the button Reset:

on mouseUp
   put empty into field "fldQuestion"
   put empty into field "fldAnswer"
   repeat with i = 1 to 36
      set the QuizQuestions[(i)] of this stack to "Question" && i
      set the QuizAnswers[(i)] of this stack to "Answer" && i
      put i & cr after tQList
   end repeat
   delete char -1 of tQList
   set the cQList of this stack to tQList
end mouseUp
This will create new Custom Property Sets "QuizQuestions" and "QuizAnswers" with a numeric key for each element in the custom property set. Each key of QuizQuestions indexes the question (which I have dumped in automatically to insert "Question 1", "Question 2", etc. and similarly for QuizAnswers. You can populate the custom property sets (both QuizQuestions and QuizAnswers) according to your list of questions and answers - reading line by line from a file say (although if you already have them written in a textfile, maybe the random selection could/should just read that file directly, rather than use the custom property set approach. See... lots of ways to think about it.)
It also creates/resets a custom property for the list of available question numbers that have not already been used in this session.

Code: Select all

for the Question button:

on mouseUp
   put the cQList of this stack into tQList
   if the number of lines of tQList is zero then
      answer "Out of questions"
   else
      put any line of tQList into tQLine
      delete line lineOffset (tQLine, tQList) of tQList
      set the cQList of this stack to tQList
      put the QuizQuestions[(tQLine)] of this stack into field "fldQuestion"
      put the QuizAnswers[(tQLine)] of this stack into field "fldAnswer"
   end if
end mouseUp
First the available question numbers list is read from the custom property cQList. If there are no more questions that have not already been asked then it bypasses the questions, otherwise it will pick "any" line of that list and then delete it, and then put that newly shortened list back into the custom property ready to read next time around. The random question number that was picked is then read in from the QuizQuestions property set. For example, if line 20 was picked, then the value in that line is put into tQLine. First time around it will be 20. Line 20 is then deleted and the shortened list stored again. QuizQuestions[20] is then put into the question field. QuizAnswers[20] is also put into the answer field. (More realistically you would have the user type in the answer and then do something like check that field contains the same value as QuizAnswer[20] but how you set out your interface and the actions is up to you.)
Next time around, there will be 35 lines in the list of available questions. For argument's sake, line 20 could be randomly chosen again, but this time the value on that line will be 21 (as the value 20 was deleted last time). Again it will be shortened and stored, so that you don't use the same question another time until the list is reset.

Anyway, this is all basic rubbish dumped into the custom properties, but I hope you see how you can tailor that to your needs and methods. Again, there's no definitive way to do this - other people may come up with different solutions.
HTH anyway.

Posted: Tue Jul 14, 2009 4:27 pm
by Stormy1
Hi SparkOut and Klaus

Thank you SO much for your responses. I will work through what you've put SparkOut and get back to you.

One thing I should have mentioned is that the answers are not required. Its purely to promote discussion amongst a group of people. Its purely a method of bring up a question.

Thanks again

Posted: Tue Jul 14, 2009 4:49 pm
by Stormy1
Hi Guys

I'm a bit confused by this SparkOut.
I only have 36 questions which I would prefer to store within the card itself if possible rather than refer to any text files.

Based on your email, I don't understand where I would put the questions for the script to find?

Apologies but I'm fairly new to RevStudio

Thanks

Posted: Tue Jul 14, 2009 6:52 pm
by Klaus
Hi Stormy1,

well SparkOut posted a solution that is maybe not really suited for beginners,
took a looong while until I understood custom properties :)

I would suggest to simply use (hidden) fields, as long your questions are one-liners.


Best

Klaus

Posted: Tue Jul 14, 2009 7:51 pm
by SparkOut
I agree that custom properties and custom property sets are not explained terribly clearly for a beginner to easily grasp, but they really are blissfully simple if you understand a variable, especially an array.

Try this:

Make a stack with one card.
Drag out a field on the card and call it "fldQuestions" (or whatever you like, as long as you refer to it the same later on).
Drag out a button on the card and give it the caption "Reset"
Edit the script of the reset button and put in:

Code: Select all

on mouseUp 
   put empty into field "fldQuestion" 
   repeat with i = 1 to 36 
      put i & cr after tQList 
   end repeat 
   delete char -1 of tQList 
   set the cQList of this stack to tQList 
end mouseUp
This will merely clear the question field and reset the list of available question choices back to 1 through 36.
The stack now has a custom property, which you can think of as just a glorified cross between a variable and a field, called cQList. It contains that list you just made of 1 through 36.

Now you need to populate your stack with questions. To do this you can add them manually in the Property Inspector, or by script. By script, something like:
Drag out a button and give it the script:

Code: Select all

on mouseUp 
   -- Make a list of questions to store as an array of custom properties
   put "What colour is my cat?" & cr into theQuestions
   put "What colour is Klaus's cat?" & cr after theQuestions
   put "What colour is SparkOut's cat?" & cr after theQuestions
   put "Does SparkOut have a cat?" & cr after theQuestions
   put "Does Klaus prefer chips or chicken?" & cr after theQuestions
   put "Does SparkOut drive a Ferrari?" & cr after theQuestions
   put "Would SparkOut like to drive a Ferrari?" & cr after theQuestions
   put "Does Klaus have a Bugatti Veyron?" & cr after theQuestions
   put "This isn't question number 36, but don't you think that's enough for now?" after theQuestions
   -- don't put "& cr" after the last question, so as not
   -- to get an empty line at the end of the list
   -- You can do this more programmatically but it's probably clearer this way, 
   -- I've obviously not made it simple enough above.

   repeat with i = 1 to 36 
      set the QuizQuestions[(i)] of this stack to line i of theQuestions 
   end repeat 
end mouseUp
Now (if you save the stack) you have stored the question list as a custom property set of the stack. Each question can be addressed by getting "the QuizQuestion[1] of this stack" (or the QuizQuestion[2] or [3] etc etc.)

So now add your Question button to say:

Code: Select all

on mouseUp
   -- retrieve the list of available questions that haven't been asked
   -- from the custom property
   put the cQList of this stack into tQList
   
   if the number of lines of tQList is zero then 
      answer "Out of questions" 
   else 
      -- get a random value from the list of available questions
      put any line of tQList into tQLine 
      -- delete the line which contains this value
      -- so that it can't be asked again this session
      delete line lineOffset (tQLine, tQList) of tQList 
      -- put the now shortened list back into the custom property
      set the cQList of this stack to tQList 

      -- tQLine now contains a random value from 1 through 36 that has
      -- NOT already been used this session, so get that question number and display it
      put the QuizQuestions[(tQLine)] of this stack into field "fldQuestion" 
   end if 
end mouseUp
Hope that helps a bit more.

Posted: Wed Jul 15, 2009 11:31 am
by Stormy1
Thank you so much Sparky.

I had to adapt it a little but it works perfectly now.
I managed to get it to work as a custom property as I didn't want to have too many buttons: not even a 'reset' button so I managed to put all the code into the 'Question' button.

Anyway, you've been a great help and much for me to learn from the script.

Thanks again.