There have been a number of requests for help with creating quiz based applications. To begin with I have created an example program which demonstrates a bare bones structure of a quiz application.
Run the following program and answer the questions relating to good quiz programming principles.
`Quiz Example
By Chris Tate
More resources at www.binarymodular.com
`======================
` What is a player? Define it in a UDT
Type PlayerType
Name$
Score
Endtype
` What specifications should all quiz entries have in common?
` Define that in a default entry
Global DefaultPlayer as PlayerType
` In this quiz, all players names start off as 'Player'
DefaultPlayer.Name$ = "Player"
` Create a list of X amount of players
` Usually there is only one player, but usual doesn't always stay usual
Dim Players( 1 ) as PlayerType
` Who am I? I am player 1
Global Me = 1
` What is my name?
Input "What is your name? Please enter: ", Players( Me ).Name$
` What is a quiz? Define it as a UDT
Type QuizEntryType
Question$
CorrectAnswer
Answer1$
Answer2$
Answer3$
Answer4$
Score
Endtype
` Create a list of X amount of quiz entries;
Dim QuizEntry( 0 ) as QuizEntryType
` What specifications should all quiz entries have in common?
` Define that in a default entry
Global DefaultQuizEntry as QuizEntryType
` In this quiz, all entries have a score of 1
DefaultQuizEntry.Score = 1
` Get questions and feedback number of quiz entries
Global NumberOfQuestions = 0
NumberOfQuestions = GetQuestions()
` What is a result? Define it as a UDT
Type ResultType
Text$
Message$
Sound
BackgroundColour
ContinueQuiz
Endtype
` There are two results; correct and incorrect; but we will start from index zero as a boolean switch
` Zero is false, one is true
Dim Result( 1 ) as ResultType
` Set their information
Result( 0 ).Text$ = "Game Over"
Result( 0 ).Message$ = "Press any key"
Result( 0 ).BackgroundColour = rgb (255,0,0)
Result( 0 ).Sound = 3
Result( 0 ).ContinueQuiz = 0 ` In this quiz, the game will not answer any more questions when the answer is wrong
Result( 1 ).Text$ = "Correct" ` We will display each message line by line
Result( 1 ).Message$ = "Press any key to continue"
Result( 1 ).BackgroundColour = rgb (0,255,0)
Result( 1 ).Sound = 3
Result( 1 ).ContinueQuiz = 1
Do
Randomize Timer() ` Shuffle random questions in random mode
Question = 1
` Call GetQuestions() again if we are using random mode or removing questions from the list
Repeat
Continue = 0 ` Do not continue if the question is wrong
Cls rgb (0,0,255)
Print "Question "; Question
Print
Print QuizEntry( Question ).Question$
Print "-----------------------------"
Print "A: "; QuizEntry( Question ).Answer1$
Print "B: "; QuizEntry( Question ).Answer2$
Print "C: "; QuizEntry( Question ).Answer3$
Print "D: "; QuizEntry( Question ).Answer4$
Print
Wait 1000 ` Wait a little before listening for the next keystroke (Matrix1 users use Nice Wait / Nice Sleep )
` Wait for an answer in a CPU friendly fashion
Repeat
k$ = MyCPUFriendlySuspendForKeyFunction$()
` Select the result via lowercase (small letters)
` Set to continue if the answer is correct
ValidKeystroke = 0 ` Assume the keystroke is invalid if not a,b,c or d
Select Lower$(k$) ` Matrix1 users use Fast Lower$(k$)
Case "a"
If QuizEntry( Question ).CorrectAnswer = 1
Continue = 1
Endif
ValidKeystroke = 1
Endcase
Case "b"
If QuizEntry( Question ).CorrectAnswer = 2
Continue = 1
Endif
ValidKeystroke = 1
Endcase
Case "c"
If QuizEntry( Question ).CorrectAnswer = 3
Continue = 1
Endif
ValidKeystroke = 1
Endcase
Case "d"
If QuizEntry( Question ).CorrectAnswer = 4
Continue = 1
Endif
ValidKeystroke = 1
Endcase
Endselect
Until ValidKeystroke
` If print the result
Cls Result( Continue ).BackgroundColour
Print Result( Continue ).Text$
Print Result( Continue ).Message$
` 1 multiplied by something equals the same thing
` 0 multplied by something equals 0
` Therefore the correct answer multiplied by score awarded will award the score
Inc Players(Me).Score, Continue * QuizEntry( Question ).Score
MyCPUFriendlySuspendForKeyFunction()
` Is this a test or an elimination? Continue asking questions if it is a test
Continue = Result( Continue ).ContinueQuiz
` Assume next question
Inc Question
` Or select a random question by using the following lines instead of the previous
` Remove From Queue QuizEntry()
` Question = Rnd( Array Count( QuizEntry() ) )
Until Continue = 0 Or Question > Array Count( QuizEntry() ) Or Array Count( QuizEntry() ) = 0
Question = 1
If EscapeKey()
End ` Will occur anyway if you do not use: Disable EscapeKey
Endif
CLS
Print Players(Me).Name$; " your final score was "; Players(Me).Score
Print "Press any key to continue, or escape to exit"
` Next player
Inc Me
If Me > Array Count( Players() )
For p = 1 to Array Count( Players() )
Players(p).Score = 0
Next p
Me = 1
Endif
MyCPUFriendlySuspendForKeyFunction()
Loop
End
//===================================================
Function MyCPUFriendlySuspendForKeyFunction$()
Clear Entry Buffer ` Wipe off the last key pressed
Repeat
Sync Sleep 10
Sleep 1
` Matrix1 users; use the more effecient Nice Sleep 1 or Nice Wait 1 - no difference
k$ = Inkey$()
Until k$ <> ""
Endfunction k$
//===================================================
Function MyCPUFriendlySuspendForKeyFunction()
Wait 200
Repeat
Sync Sleep 10
Sleep 1
` Matrix1 users; use the more effecient Nice Sleep 1 or Nice Wait 1 - no difference
Until Scancode()
Endfunction
//===================================================
Function GetQuestions()
` Now we could load an XML file from MS Excel or connect to a MS Access or
` load the questions from the internet, or from an INI file; I've left that
` up to you; in such a case you would do something along the lines of
` this function: LoadQuestions() See next function which demonstrates
` loading a simple text file
Array Insert At Bottom QuizEntry()
Index = Array Count( QuizEntry() )
QuizEntry( Index ) = DefaultQuizEntry
QuizEntry( Index ).Question$ = "What is an array?"
QuizEntry( Index ).CorrectAnswer = 2
QuizEntry( Index ).Answer1$ = "A box of items"
QuizEntry( Index ).Answer2$ = "A list of items"
QuizEntry( Index ).Answer3$ = "A single item"
QuizEntry( Index ).Answer4$ = "All of the above"
` QuizEntry( Index ).Score = 1
Inc Index ` Advance to the next question
Array Insert At Bottom QuizEntry()
QuizEntry( Index ) = DefaultQuizEntry
QuizEntry( Index ).Question$ = "Why use arrays?"
QuizEntry( Index ).CorrectAnswer = 4
QuizEntry( Index ).Answer1$ = "Because they look cool"
QuizEntry( Index ).Answer2$ = "Because everyone uses them"
QuizEntry( Index ).Answer3$ = "Because they seperate similar things from each other in a list"
QuizEntry( Index ).Answer4$ = "Because they organize related things into a list"
` QuizEntry( Index ).Score = 1
Inc Index ` Advance to the next question
Array Insert At Bottom QuizEntry()
QuizEntry( Index ) = DefaultQuizEntry
QuizEntry( Index ).Question$ = "Why would it be better to store questions on disk?"
QuizEntry( Index ).CorrectAnswer = 3
QuizEntry( Index ).Answer1$ = "Because it will save disk space"
QuizEntry( Index ).Answer2$ = "Because it makes the programming easier"
QuizEntry( Index ).Answer3$ = "Because it makes the questions easier to update and translate"
QuizEntry( Index ).Answer4$ = "Because using memory is bad practice"
` QuizEntry( Index ).Score = 2
Inc Index ` Advance to the next question
Array Insert At Bottom QuizEntry()
QuizEntry( Index ) = DefaultQuizEntry
QuizEntry( Index ).Question$ = "Why would it be good to use XML to store questions?"
QuizEntry( Index ).CorrectAnswer = 1
QuizEntry( Index ).Answer1$ = "Because many document or text editors like MS Excel can export XML"
QuizEntry( Index ).Answer2$ = "Because XML is the main standard"
QuizEntry( Index ).Answer3$ = "Because XML is the easiest to interpret"
QuizEntry( Index ).Answer4$ = "Because XML files are better than INI files"
` QuizEntry( Index ).Score = 3
Inc Index ` Advance to the next question
Array Insert At Bottom QuizEntry()
QuizEntry( Index ) = DefaultQuizEntry
QuizEntry( Index ).Question$ = "Why would it be good encrypt question lists?"
QuizEntry( Index ).CorrectAnswer = 1
QuizEntry( Index ).Answer1$ = "Because the answers could be read and the questions could be tampered with"
QuizEntry( Index ).Answer2$ = "Because the answers could be read and the a virus could be created in the text file"
QuizEntry( Index ).Answer3$ = "Because the answers could be read and the text file could be deleted from disk"
QuizEntry( Index ).Answer4$ = "Because encryption improves data loading times"
` QuizEntry( Index ).Score = 3
Endfunction Index
//===================================================
Function LoadQuestions()
Remstart
` Decrypt("QuestionFile.Txt") ` Use encrpytion if necessary, check TGC for encryption examples
Open To Read 1, "QuestionFile.Txt"
Index = 1
While File End(1) = 0
Array Insert At Bottom
Read String 1, data$ : QuizEntry( Index ).Question$ = data$
Read String 1, data$ : QuizEntry( Index ).CorrectAnswer = Val( data$ ) ` Matrix1 users use IntVal()
Read String 1, data$ : QuizEntry( Index ).Answer1$ = data$
` And so on....
Inc Index
EndWhile
Close File 1
NumberOfQuestions = Index
` Encrypt("QuestionFile.Txt") ` Use encrpytion if necessary
RemEnd
Endfunction
Now look at the comments in the code to learn how it worked. The structure is flexible, so with a few changes here and there, inclusion of images and sounds and you have a nice quiz which can also feature more than one player, questions with alternative scores and a test structure which continues to answer questions for you to evaluate at the end.
Need more info on arrays? View my
arrays guide.
Struggling with confidence with DBP? Need some inspiration? See my tips for
DBP in less than 10 words thread.