none
lock combo breaker program RRS feed

  • Question

  • Hi, I am writing a code for breaking a 3 digit combo. I cannot get the decisions to display the right response. it would only say correct even when it does not match any of the numbers in the combo. Appreciate your suggestions. 
    Public Class Form1
    
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Dim number1 As Integer
            Dim number2 As Integer
            Dim number3 As Integer
            Dim guess1 As Integer
            Dim guess2 As Integer
            Dim guess3 As Integer
    
            Randomize()
    
            number1 = Int(Rnd() * 9) + 1
            number2 = Int(Rnd() * 9) + 1
            number3 = Int(Rnd() * 9) + 1
    
            guess1 = Val(txtguess1.Text)
            guess2 = Val(txtguess2.Text)
            guess3 = Val(txtguess3.Text)
    
            Label1.Text = number1
            Label6.Text = number2
            Label7.Text = number3
    
        End Sub
    
        Private Sub btnopenlock_Click(sender As Object, e As EventArgs) Handles btnopenlock.Click
            Dim guess1 As Integer
            Dim number1 As Integer
            Dim guess2 As Integer
            Dim number2 As Integer
            Dim guess3 As Integer
            Dim number3 As Integer
    
            lblanswer.Text = Val(lblanswer.Text) + 1
    
            If guess1 = number1 Then
                lblanswer1.Text = ("Correct")
            ElseIf guess1 <> number1 Then
                lblanswer1.Text = ("Wrong")
            End If
    
            If guess2 = number2 Then
                lblanswer2.Text = ("Correct")
            ElseIf guess2 <> number2 Then
                lblanswer2.Text = ("Wrong")
    
            End If
    
            If guess3 = number3 Then
                lblanswer3.Text = ("Correct")
            ElseIf guess3 <> number3 Then
                lblanswer3.Text = ("Wrong")
            End If
    
        End Sub
    
        Private Sub btnplayagain_Click(sender As Object, e As EventArgs) Handles btnplayagain.Click
            lblanswer1.Text = ""
            lblanswer2.Text = ""
            lblanswer3.Text = ""
            txtguess1.Text = ""
            txtguess2.Text = ""
            txtguess3.Text = ""
    
        End Sub
    End Class
    

    Friday, June 15, 2018 12:46 AM

All replies

  • it would only say correct even when it does not match any of the numbers in the combo. Appreciate your suggestions.

    Use the debugger.  Insert a breakpoint at the start of the method, and step the code through a line at a time. Watch the values of the important variables and check the value before each If statement.
    https://vbdotnetblog.wordpress.com/overview/debugging/

    That will show you that the values you are testing are not what you expect.  The reason is explained here:
    https://docs.microsoft.com/en-us/dotnet/visual-basic/programming-guide/language-features/declared-elements/scope

    You are using the Rnd() method when you should be using the Random object.
    https://msdn.microsoft.com/en-us/library/system.random(v=vs.110).aspx

    The Val function will fail or return unexpected results if the text in the text box is not a valid number.  For a better mthod, see
    https://msdn.microsoft.com/en-us/library/f02979c7(v=vs.110).aspx

    Friday, June 15, 2018 12:55 AM
  • To help yourself set break-points, step through the code and examine the values of variables in the IDE local window.

    If you still can't figure it out come back and indicate what you learned and what is the current problem.


    Please remember to mark the replies as answers if they help and unmark them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.
    VB Forums - moderator
    profile for Karen Payne on Stack Exchange, a network of free, community-driven Q&A sites

    Friday, June 15, 2018 1:00 AM
    Moderator
  • Appreciate your responses. I have tried moving the random number generator in the scope of the button open lock and the response is correct. I tried this before but every time i click on the button it will generate new numbers which I don't want. The program will work this way if I can stop the random generator after the first button click. This is why I prefer to have the numbers generated when the form loads and just add the decisions to control the answers. I still have to debug as suggested thank you.
    Friday, June 15, 2018 2:00 AM
  • Appreciate your responses. I have tried moving the random number generator in the scope of the button open lock and the response is correct. I tried this before but every time i click on the button it will generate new numbers which I don't want.

    If you are referring to the Randomize statement, that wasn't the problem - it shouldn't even be there.  The Random object doesn't use it.  You are correct that the number to guess should be set once at the start of the program, and not changed (at least in this version of the program).

    Did you use the debugger to trace the program flow while you were watching the values that were being tested - such as guess1 and number1?   Did you notice that they were not the values that you expected to see? There are two different reasons for that.   Did you look at the article I quoted re scope?  If so, do you see how that was relevant to the problem with the values?  If not, you need to indicate what result you expected for those values, and why.

    Friday, June 15, 2018 3:04 AM
  • Hi Jazee123,

    There are two places you need to change in your code:

    1, Please do not define the members(number1, number2...guess1...) twice, you can extract them as global variables instead:

    Public Class Form1
        Dim number1 As Integer
        Dim number2 As Integer
        Dim number3 As Integer
        Dim guess1 As Integer
        Dim guess2 As Integer
        Dim guess3 As Integer
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            '...
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            '...
        End Sub
    End Class

    2, You assigned the values of the textboxes to guess1/guess2/guess3 in the Form_Load event, however, when the form loaded, these values are 0 because you did not input them, so please move the following section from Form_Load event to button_Click event:

        guess1 = Val(txtguess1.Text)
        guess2 = Val(txtguess2.Text)
        guess3 = Val(txtguess3.Text)

    So the whole project:

    Public Class Form1
        Dim number1 As Integer
        Dim number2 As Integer
        Dim number3 As Integer
        Dim guess1 As Integer
        Dim guess2 As Integer
        Dim guess3 As Integer
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Randomize()
            number1 = Int(Rnd() * 9) + 1
            number2 = Int(Rnd() * 9) + 1
            number3 = Int(Rnd() * 9) + 1
            Label1.Text = number1
            Label2.Text = number2
            Label3.Text = number3
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            guess1 = Val(txtguess1.Text)
            guess2 = Val(txtguess2.Text)
            guess3 = Val(txtguess3.Text)
            If guess1 = number1.ToString() Then
                lblanswer1.Text = ("Correct")
            ElseIf guess1 <> number1 Then
                lblanswer1.Text = ("Wrong")
            End If
    
            If guess2 = number2.ToString() Then
                lblanswer2.Text = ("Correct")
            ElseIf guess2 <> number2 Then
                lblanswer2.Text = ("Wrong")
            End If
    
            If guess3 = number3 Then
                lblanswer3.Text = ("Correct")
            ElseIf guess3 <> number3.ToString() Then
                lblanswer3.Text = ("Wrong")
            End If
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            lblanswer1.Text = ""
            lblanswer2.Text = ""
            lblanswer3.Text = ""
            txtguess1.Text = ""
            txtguess2.Text = ""
            txtguess3.Text = ""
        End Sub
    End Class

    Regards,

    Frankie


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Friday, June 15, 2018 6:46 AM
  • Hi Jazee123,

    There are two places you need to change in your code:

    1, Please do not define the members(number1, number2...guess1...) twice, you can extract them as global variables instead:

    Public Class Form1
        Dim number1 As Integer
        Dim number2 As Integer
        Dim number3 As Integer
        Dim guess1 As Integer
        Dim guess2 As Integer
        Dim guess3 As Integer
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            '...
        End Sub
    
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            '...
        End Sub
    End Class

    2, You assigned the values of the textboxes to guess1/guess2/guess3 in the Form_Load event, however, when the form loaded, these values are 0 because you did not input them, so please move the following section from Form_Load event to button_Click event:

        guess1 = Val(txtguess1.Text)
        guess2 = Val(txtguess2.Text)
        guess3 = Val(txtguess3.Text)

    So the whole project:

    Public Class Form1
        Dim number1 As Integer
        Dim number2 As Integer
        Dim number3 As Integer
        Dim guess1 As Integer
        Dim guess2 As Integer
        Dim guess3 As Integer
        Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
            Randomize()
            number1 = Int(Rnd() * 9) + 1
            number2 = Int(Rnd() * 9) + 1
            number3 = Int(Rnd() * 9) + 1
            Label1.Text = number1
            Label2.Text = number2
            Label3.Text = number3
        End Sub
        Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
            guess1 = Val(txtguess1.Text)
            guess2 = Val(txtguess2.Text)
            guess3 = Val(txtguess3.Text)
            If guess1 = number1.ToString() Then
                lblanswer1.Text = ("Correct")
            ElseIf guess1 <> number1 Then
                lblanswer1.Text = ("Wrong")
            End If
    
            If guess2 = number2.ToString() Then
                lblanswer2.Text = ("Correct")
            ElseIf guess2 <> number2 Then
                lblanswer2.Text = ("Wrong")
            End If
    
            If guess3 = number3 Then
                lblanswer3.Text = ("Correct")
            ElseIf guess3 <> number3.ToString() Then
                lblanswer3.Text = ("Wrong")
            End If
        End Sub
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            lblanswer1.Text = ""
            lblanswer2.Text = ""
            lblanswer3.Text = ""
            txtguess1.Text = ""
            txtguess2.Text = ""
            txtguess3.Text = ""
        End Sub
    End Class

    Regards,

    Frankie


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/81590b4f-0466-4255-adb2-00f0a6242831/please-avoid-doing-other-peoples-homework-for-them?forum=vbgeneral

    Friday, June 15, 2018 9:15 AM