Help with randomization

  • Thread starter Thread starter Shannon Reynolds
  • Start date Start date
S

Shannon Reynolds

Guest
I am writing a application that plays a game. It works, my problem is it only asks one question out of 79. I have a matching game that plays first, if you get a match then it goes to the question part. The question part is where I am having a problem. I have it set up to show a word or set of words and then four statements that one is the true answer. I have it in a database. I am doing this for a project for school, but it is not for a grade, I am doing it for fun, and seeing if it is possible. I know it is possible I just don't understand where I went wrong in the code. Any help is appreciated.

Imports System.Data.OleDb
Public Class Form2
Dim ConnectString As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & " Data Source= E:\Questions.accdb"

Dim dr As OleDbDataReader
Dim cm As New OleDbCommand
Dim cn As New OleDbConnection
Dim provider As String
Dim dataFile As String


Dim quesnum As Integer
Dim answer As String
Dim selected As String
Dim score As Integer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load



lblWords.Show()
radStatement1.Show()
radStatement2.Show()
radStatement3.Show()
radStatement4.Show()
btnNext.Show()
btnExit.Show()
btnCheckAnswer.Show()
btnReturn.Show()

For i = 1 To 79
asking()
Next

End Sub

Private Sub asking()
cn.ConnectionString = ConnectString
cn.Open()



cm.CommandText = "SELECT Words, Statements1, Statements2, Statements3, CorrectStatement FROM Game"
cm.Connection = cn

dr = cm.ExecuteReader
Dim questionList As New List(Of String)
Dim answerList As New List(Of String)


If dr.HasRows Then
dr.Read()
questionList.Add(dr.Item("Words"))
questionList = RandomizeQuestionOrder(questionList)

lblWords.Text = questionList(0)

answerList.Add(dr.Item("Statements1"))
answerList.Add(dr.Item("Statements2"))
answerList.Add(dr.Item("Statements3"))
answerList.Add(dr.Item("CorrectStatement"))
answerList = RandomizeListOrder(answerList)
radStatement1.Text = answerList(0)
radStatement2.Text = answerList(1)
radStatement3.Text = answerList(2)
radStatement4.Text = answerList(3)
dr.Close()
End If
cn.Close()

End Sub

Private Sub mark()
cn.ConnectionString = ConnectString
cn.Open()
cm.CommandText = "SELECT CorrectStatement FROM Game"
cm.Connection = cn
dr = cm.ExecuteReader
If dr.HasRows Then
dr.Read()
answer = dr.Item("CorrectStatement")
End If
cn.Close()

If selected = answer Then
score = score + 1
MsgBox("score is " & score,, "CORRECT")

Else
Dim Form1 As New Form1
Hide()
Form1.ShowDialog()
End If
End Sub
Private Function RandomizeQuestionOrder(question As List(Of String))
Dim question2 As Integer
Dim rnd As New Random

'loop through the words in the table
For question1 As Integer = 0 To question.Count - 1
'pick a random word
question2 = rnd.Next(0, question.Count - 1)
If question1 = question2 Then
Continue For
End If
Dim temp As String
temp = question(question1)
question(question1) = question(question2)
question(question2) = temp
Next
Return question
End Function

Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click

asking()


End Sub



Private Function RandomizeListOrder(answers As List(Of String))
Dim answer2 As Integer
Dim rnd As New Random
'loop though each item in the list
For answer1 As Integer = 0 To answers.Count - 1
'pick a random answer
answer2 = rnd.Next(0, answers.Count - 1)
'If answer1 and answer2 are the same then skip the rest of the code
'in the loop and go onto the next question
If answer1 = answer2 Then
Continue For
End If
'swap the questions over
Dim temp As String
temp = answers(answer1)
answers(answer1) = answers(answer2)
answers(answer2) = temp
Next
'return the list of answers
Return answers
End Function

Private Sub btnCheckAnswer_Click(sender As Object, e As EventArgs) Handles btnCheckAnswer.Click
If radStatement1.Checked = True Then
selected = radStatement1.Text
ElseIf radStatement2.Checked = True Then
selected = radStatement2.Text
ElseIf radStatement3.Checked = True Then
selected = radStatement3.Text
ElseIf radStatement4.Checked = True Then
selected = radStatement4.Text
End If
mark()
End Sub

Private Sub btnReturn_Click(sender As Object, e As EventArgs) Handles btnReturn.Click
Dim Form1 As New Form1
Hide()
Form1.ShowDialog()
End Sub

Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Application.Exit()
End Sub
End Class

Continue reading...
 
Back
Top