Public Class frmMain
Private Questions As New List(Of List(Of String))
Private RandomIndex As List(Of Integer)
Private RunningIndex As Integer = 0
Private totalCorrect As Integer
Private totalLife As Integer = 3
Private Sub btnStart_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnStart.Click
GenerateQuestion(RandomIndex(RunningIndex))
shuffle()
btnStart.Enabled = False
btnRetry.Enabled = True
End Sub
Private Sub shuffle()
Dim nListOfLocation As New List(Of Point)
With nListOfLocation
.Add(btnCorrect.Location)
.Add(btnWrong1.Location)
.Add(btnWrong2.Location)
.Add(btnWrong3.Location)
End With
Dim ShuffledList As List(Of Integer) = GenerateUnique(4, 1, 4)
btnCorrect.Location = nListOfLocation(ShuffledList(0) - 1)
btnWrong1.Location = nListOfLocation(ShuffledList(1) - 1)
btnWrong2.Location = nListOfLocation(ShuffledList(2) - 1)
btnWrong3.Location = nListOfLocation(ShuffledList(3) - 1)
End Sub
Private Sub GenerateQuestion(ByVal nRandomIndex As Integer)
Dim myQuestions As List(Of String) = Questions(nRandomIndex - 1)
lblQuestion.Text = myQuestions(0)
btnCorrect.Text = myQuestions(1)
btnWrong1.Text = myQuestions(2)
btnWrong2.Text = myQuestions(3)
btnWrong3.Text = myQuestions(4)
RunningIndex += 1
End Sub
Private Sub frmMain_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
AddQuestionChoice("Question1", "Correct", "Wrong1", "Wrong2", "Wrong3")
AddQuestionChoice("Question2", "Correct", "Wrong1", "Wrong2", "Wrong3")
AddQuestionChoice("Question3", "Correct", "Wrong1", "Wrong2", "Wrong3")
AddQuestionChoice("Question4", "Correct", "Wrong1", "Wrong2", "Wrong3")
AddQuestionChoice("Question5", "Correct", "Wrong1", "Wrong2", "Wrong3")
retry()
btnRetry.Enabled = False
End Sub
Private Sub AddQuestionChoice(ByVal nQuestion As String, ByVal nCorrect As String, ByVal nWrong1 As String, ByVal nWrong2 As String, ByVal nWrong3 As String)
Dim QuestionChoice As New List(Of String)
With QuestionChoice
.Add(nQuestion)
.Add(nCorrect)
.Add(nWrong1)
.Add(nWrong2)
.Add(nWrong3)
End With
Questions.Add(QuestionChoice)
End Sub
Public Function GenerateUnique(ByVal LoopCount As Integer, ByVal rMin As Integer, ByVal rMax As Integer) As List(Of Integer)
Dim holder As New HashSet(Of Integer)
Dim rand As New Random
Do
holder.Add(rand.Next(rMin, rMax + 1))
Loop Until holder.Count = LoopCount
Return holder.ToList
End Function
Private Sub btnRetry_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRetry.Click
retry()
End Sub
Private Sub retry()
Panel1.Enabled = True
btnStart.Enabled = True
btnRetry.Enabled = False
lblQuestion.Text = Nothing
RunningIndex = 0
totalCorrect = 0
totalLife = 3
txtCorrect.Text = totalCorrect
txtLives.Text = totalLife
RandomIndex = GenerateUnique(Questions.Count, 1, Questions.Count)
End Sub
Private Sub btnCorrect_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCorrect.Click
If RunningIndex < Questions.Count Then
GenerateQuestion(RandomIndex(RunningIndex))
shuffle()
totalCorrect += 1
txtCorrect.Text = totalCorrect
ElseIf RunningIndex = Questions.Count Then
totalCorrect += 1
txtCorrect.Text = totalCorrect
RunningIndex += 1 ' append running index ' this is the last question
End If
End Sub
Private Sub btnWrong_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnWrong1.Click, btnWrong2.Click, btnWrong3.Click
If RunningIndex < Questions.Count Then
If totalLife <= 1 Then
totalLife = totalLife - 1
Panel1.Enabled = False
MsgBox("game over")
Else
GenerateQuestion(RandomIndex(RunningIndex))
shuffle()
totalLife = totalLife - 1
End If
ElseIf RunningIndex = Questions.Count Then
totalLife = totalLife - 1
RunningIndex += 1 ' append running index ' this is the last question
End If
txtLives.Text = totalLife
End Sub
End Class