Multiple-Choice Problems...

Jason Rippetoe jason at rippetoe.com
Tue Nov 11 04:02:44 EST 2003


The fateful moment has arrived- when I reveal to the list actual 
scripts I have written, showing the true depth of my coding abilities. 
Despite the shallowness of these snippets and the possibility that some 
of you more educated programmers might snicker a bit at my naiveté, I'm 
quite proud of the fact that I was able to cobble this together on my 
own and that it actually WORKS!

Well, make that "works up to a point." I'm not so proud of myself I 
won't ask for help.

Here's the background-

This is a foreign language vocabulary drill program. It uses audio bits 
and images in a multiple-choice format- the user clicks a button to 
hear the audio of the vocab word, then selects an answer from one of 
four images. If the image corresponds with the audio, it displays an 
affirmative graphic, if it doesn't a negative graphic appears.

I'll present the scripts I use to get to this point before I give you 
the problem I'm facing right now. For development purposes, I have this 
in three buttons, but button one would likely be in an openStack 
handler in a finished product, since it only needs to occur once. 
Button two could not be a button at all or combined with button three, 
since it never actually gets pressed by the user. I had them separate 
to keep actions clear in my mind while developing. Hope it doesn't 
confuse the issue.

Button one starts off the show, taking the comma-delimited data from a 
field ("SourceData"), then putting it into a variable. Each line in the 
field is two paths- one to the audio file and the other to the 
corresponding graphic. Forgive me if there are global variables that 
aren't used in every handler... I've changed them so many times as I've 
worked on this that there are a few unused, I think.

global gSourceData, gRandomLines
on mouseUp
   put field "SourceData" into gSourceData
   put empty into gRandomLines
send mouseUp to button "Button 2"
end MouseUp

The next button figures out how many lines are in the gSourceData, and 
puts it into a variable. Then grabs a random one of those lines as the 
correct answer, then three other lines to represent incorrect answers. 
Since the lines are deleted from the gSourceData in the process, the 
"wrong" answers are added back to gSourceData so that they can be used 
as answers again. The "correct" answer is removed so that it does not 
appear as a correct answer again, however, it cannot be used as an 
incorrect answer either, which is a shortcoming of my script.


global gSourceData, gSourceLines, gRandomLines, gTempSource, 
gSelection, gSolution, gAltSolutions,
on mouseUp
   put empty into gAltSolutions
   put the number of lines of gSourceData into gSourceLines -- If there 
are 10 lines, gSourceLines will be "10"
   put random(gSourceLines) into gSelection
   put line gSelection of gSourceData into line 1 of gRandomLines
   delete line gSelection of gSourceData
   put gRandomLines into gSolution
   put empty into gRandomLines
   repeat with x = 1 to 3 times
     put the number of lines of gSourceData into gSourceLines -- If 
there are 10 lines, gSourceLines will be "10"
     put random(gSourceLines) into gSelection
     put line gSelection of gSourceData into line x of gRandomLines
     delete line gSelection of gSourceData
   end repeat
   put gRandomLines into gAltSolutions
   put gRandomLines&return&gSourceData into gSourceData
end mouseUp


The final button takes the correct and incorrect answers and assigns 
them to one of four positions on the screen. It is pressed by the user 
to get a new correct answer to advance the program.

global gSourceData, gSourceLines, gRandomLines, gTempSource, 
gSelection, gSolution, gAltSolutions, gRandomImage1, gRandomImage2, 
gRandomImage3, gRandomImage4

on mouseUp
   put field "gImage Source" into gImageSource
   put empty into gRandomImage1
   put empty into gRandomImage2
   put empty into gRandomImage3
   put empty into gRandomImage4
   repeat with x = 1 to 4 times
     put the number of lines of gImageSource into gImageSourceLines -- 
If there are 10 lines, gSourceLines will be "10"
     put random(gImageSourceLines) into gImageSelection
     put line gImageSelection of gImageSource into line x of gImageLines
     delete line gImageSelection of gImageSource
   end repeat
    -- Set the correct answer
   set the fileName of player "Question" to item 1 of line 1 of gSolution
   put line 1 of gImageLines into gRandomImage1
   set the fileName of gRandomImage1 to item 2 of line 1 of gSolution
    -- Set the Alt Selection 1
     set the fileName of player "Alt1" to item 1 of line 1 of 
gAltSolutions
   put line 2 of gImageLines into gRandomImage2
   set the fileName of gRandomImage2 to item 2 of line 1 of gAltSolutions
    -- Set the Alt Selection 2
     set the fileName of player "Alt2" to item 1 of line 2 of 
gAltSolutions
   put line 3 of gImageLines into gRandomImage3
   set the fileName of gRandomImage3 to item 2 of line 2 of gAltSolutions
    -- Set the Alt Selection 3
     set the fileName of player "Alt3" to item 1 of line 3 of 
gAltSolutions
   put line 4 of gImageLines into gRandomImage4
   set the fileName of gRandomImage4 to item 2 of line 3 of gAltSolutions
   send mouseUp to button "Button 2"
end mouseUp

Finally, each image has a script as well that functions as a button to 
reveal whether the user has selected the correct answer when clicked.

global gRandomImage1
on mouseUp
   if the name of me is gRandomImage1
   then show field "CorrectDisplay"
else
   show field "FalseDisplay"
   wait 1 sec
   hide field "FalseDisplay"
end if
wait 1 sec
hide field "CorrectDisplay"
end mouseUp

That's pretty much it, though  there is a problem, which appears as I 
get to the end of the listings in gSourceData. Once there are less than 
four questions left, a line is left blank, meaning that there is no 
path for the image in the following questions. So instead of a pretty 
vocabulary image, there's a blank spot, which repeats until the last 
item in the list happens to be the randomly-selected "correct" answer.

I haven't been able to figure out a simple way to run through the 
complete list without getting this blank spot. Actually, I did think of 
one way, but have no idea how to do the code- I'm afraid I do fine with 
small steps, but trying to implement something large gets the better of 
me. I'm sure there is a much more elegant solution to than what I've 
already created- unfortunately, I haven't any idea how to proceed!

Sorry for the long post. I've rambled enough- any ideas from anyone out 
there in Rev land?

Thanks,

-Jason


More information about the use-livecode mailing list