Reputation: 1185
I am making a quiz for my computer science class and the basic concept is that you have 15 keywords and 15 definitions. All need to be randomly displayed and the correct answer has to appear. The user has to match the correct definition to the keyword twice and then that keyword and definition are not displayed again. When all have been answered twice the quiz is over.
I have stored both my keywords and my definitions in the same file so they don't get out of sync. The text file looks like so:
Keyword1,Definition1
Keyword2,Definition2
Keyword3,Definition3
Keyword4,Definition4
etc (15 lines in total)
Currently I have my dictionary manually created like so:
Const NUMBER_OF_ANSWERS As Integer = 3
Public Class Form1
Dim kv As New Dictionary(Of String, String)
kv.Add("Keyword1", "Definition1")
kv.Add("Keyword2", "Definition2")
kv.Add("Keyword3", "Definition3")
kv.Add("Keyword4", "Definition4")
kv.Add("Keyword5", "Definition5")
kv.Add("Keyword6", "Definition6")
kv.Add("Keyword7", "Definition7")
kv.Add("Keyword8", "Definition8")
kv.Add("Keyword9", "Definition9")
kv.Add("Keyword10", "Definition10")
kv.Add("Keyword11", "Definition11")
kv.Add("Keyword12", "Definition12")
kv.Add("Keyword13", "Definition13")
kv.Add("Keyword14", "Definition14")
kv.Add("Keyword15", "Definition15")
Dim r As New Random
Dim kvRandom As List(Of KeyValuePair(Of String, String)) =
kv.OrderBy(Function() r.Next).ToList
'questions will appear in random order
For Each line As KeyValuePair(Of String, String) In kvRandom
Dim keyword As String = line.Key
Dim correctDefinition As String = line.Value
Dim keywords As New List(Of String)
keywords.Add(keyword)
keywords.AddRange(kv.Keys.Except({keyword}).
OrderBy(Function() r.Next).Take(NUMBER_OF_ANSWERS - 1))
Dim definitionsRandom As List(Of String) =
keywords.Select(Function(x) kv(x)).OrderBy(Function() r.Next).ToList
'TODO: need to write some code here
'display keyword and three possible definitions to the user
'(out of which one is correct)
'answers will also appear in random order
'Check answer against value stored in "correctDefinition"
LabelKeyword.Text = keyword
RadioButtonDef1.Text = definitionsRandom(0)
RadioButtonDef2.Text = definitionsRandom(1)
RadioButtonDef3.Text = definitionsRandom(2)
Next
End Sub
I know that to populate a dictionary from a textfile I do the following:
For Each line As String In IO.File.ReadAllLines("keywords_and_definitions.txt")
Dim parts() As String = line.Split(",")
kv.Add(parts(0), parts(1))
Next
However I am not sure how to change this code to successfully implement this. Thanks to previous help here I was told to do this (by @Neolisk):
set your progress variable to 0 of 14 (number of questions minus 1, indexes are zero based in VB.NET). At first question, display question #0, when user presses Next, increment progress variable. Don't forget to count valid/invalid answers. You may want to store full answer history for a user. If you need mode detail on this one, I think it's worth asking a separate question - provide the functionality you need there.
Again I am not sure how to go about doing this. The above code works wonders it's just I'm not sure how to change the code to populate the dictionary from the text file rather than how I have done it above as it is a requirement that we include our keywords and definitions from a text file.
Upvotes: 0
Views: 1894
Reputation: 19544
Maybe I'm not understanding your question, but just mixing the two together should do the trick, no??
Const NUMBER_OF_ANSWERS As Integer = 3
Public Class Form1
Dim kv As New Dictionary(Of String, String)
For Each line As String In IO.File.ReadAllLines("keywords_and_definitions.txt")
Dim parts() As String = line.Split(",")
kv.Add(parts(0), parts(1))
Next
Dim r As New Random
...
Or am I not understanding your question?
Also, I'll give you the hint, since this is homework, that the Dim parts() As String
could be moved outside of your loop.
Hopw this is what you were asking and it helps set you in the right direction
Upvotes: 1