I'm writing a trivia type game, and I would like to know what's the best way to define your questions and multiple-choice answers without having to use a long list of Select Case statements.
Printable View
I'm writing a trivia type game, and I would like to know what's the best way to define your questions and multiple-choice answers without having to use a long list of Select Case statements.
Standardize the format so you can use one function for all questions(or series of functions, maybe even a class).
I'd suggest UDTs. You don't necessarily need a Select Case structure. I don't see any problems with an If Else structure.
You could either make an array for the questions or load them as needed, loading them as needed would be less memory consuming.
The If/Else would be a good idea, but I plan to have over 150 questions total by the time I'm through, that's why I wanted to avoid that.
What do you mean by loading them as needed? Do you mean defining the question in code as needed?
No... I mean you're going to have to store these questions somewhere. I suppose you could do it intrinsically. But I would suggest an external file so you could supplement questions.
Then have a UDT:
VB Code:
Private Type tQuestion Question as String Choices() as String Answer as Byte ' relate this to Choices index 'anything else? End Type Public cQ as tQuestion ' this could be an array of the questions 'Then simply assign those values to what you want them as Private Function Query(Decision as Byte) As Boolean If tQuestion.Answer = Decision Then Query = True End Function
Hmm, I think I get it now. I could do something like this:
VB Code:
Dim CurrentQuestion(149) As tQuestion Dim Mixed(149) As Integer Private Type tQuestion Question as String Choices() as String Answer as Byte End Type Public Function SetupQuestions() Dim intFN As Integer For intFN = 0 To 39 CurrentQuestion(intFN) = intFN Next intFN MixQuestions CurrentQuestion(), Mixed() End Function Private Function MixQuestions(ByRef CurrentQuestion() As tQuestion, ByRef intReturnArray() As Integer) As IntegerDim intExclude() As Integer Dim intUBound, intFN, intAssigningValue, intIndex As Integer intUBound = UBound(CurrentQuestion) ReDim intRetVal(intUBound) ReDim intExclude(intUBound) For intFN = 0 To intUBound intExclude(intFN) = -1 Next intFN For intFN = 0 To intUBound intAssigningValue = CurrentQuestion(intFN) intIndex = RandomInteger(0, intUBound + 1, intExclude()) intReturnArray(intIndex) = intAssigningValue intExclude(intFN) = intIndex Next intFN End Function
Then I would use your Query function to check if the answer is right, and it should behave like any "normal" trivia program.
One question comes to my mind: How do you recall the questions and answers from the file?
You can us VB's intrinisc functions that write to records, and parse themselves automatically.