0

Hi Guys! I'm trying to create an addition game with an image to display as the given number (addends)
EX.
instead of just displaying texts like 1+1 = 2
I will be using images to display the given numbers and the answer.

I have created some codes here that was able to display the images randomly using an imagelist however the program isn't displaying the correct sum for the problem. Any advice and help is greatly appreciated.

Public Class Play

    Dim RandomGen As New Random
    Dim RandomNum1 As Integer
    Dim RandomNum2 As Integer
    Dim QuestionNumber As Integer = 0
    Dim Answer As Integer

    Dim NumGuesses As Integer = 5
    Const MaxGuesses As Integer = 2
    Const NumQuestions As Integer = 5

    Private Sub Play_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        lblTryAgain.Visible = False

    End Sub

    Private Sub BtnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnNext.Click
        RandomNum1 = RandomGen.Next(0, 9)
        RandomNum2 = RandomGen.Next(0, 9)

        lblQuestion1.Image = ImageList1.Images(RandomNum1)
        lblQuestion2.Image = ImageList1.Images(RandomNum2)

        If QuestionNumber < 1 Then
            BtnNext.Text = "Answer"
            QuestionNumber = 0
            ShowQuestion()
        Else
            CheckAnswer()
        End If

    End Sub

    Private Sub ShowQuestion()

        Dim addend1 As Integer = RandomGen.Next(9)
        Dim addend2 As Integer = RandomGen.Next(9)

        NumGuesses = 0
        lblTryAgain.Visible = False

        Answer = addend1 + addend2

        Me.Text = "Question " & _
            QuestionNumber.ToString() & _
            " of " & NumQuestions.ToString()

        txtAnswer.Text = ""
        txtAnswer.Focus()

        'Answer = RandomNum1 + RandomNum2
    End Sub

    Private Sub CheckAnswer()
        Dim guess As Integer = Val(txtAnswer.Text)
        txtAnswer.Text = ""
        txtAnswer.Focus()

        If Math.Abs(guess - Answer) < 0.01 Then
            QuestionNumber += 1

            If QuestionNumber > NumQuestions Then
                lblQuestion1.Text = ""
                lblQuestion2.Text = ""
                txtAnswer.Text = ""

            Else
                ' Show another question.
                ShowQuestion()

            End If

        Else
            If NumGuesses >= MaxGuesses Then
                ' Show a new question.
                ShowQuestion()

            End If
        End If



    End Sub
2
Contributors
1
Reply
15
Views
2 Years
Discussion Span
Last Post by tinstaafl
0

Just from a perusal of your code it looks like you're assigning random pictures to the labels, but you're generating two more random numbers to get the answer. Which stands to reason, that they probably won't match the images you've used.

Edited by tinstaafl

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.