Results 1 to 11 of 11

Thread: VB.net Slots game bug not easily apparent

  1. #1

    Thread Starter
    New Member
    Join Date
    Mar 2024
    Posts
    2

    VB.net Slots game bug not easily apparent

    I created a simple slot game with vb.net. It is behaving very oddly. Bot the rules for the game and the source code are provided in the game. Any help in explaining why its behaving the way it is, is appreciated. Zip file containing release executable and source code project files are provided below.

    For example, how did go to $49,225 in total winnings in 6 spins.

    Name:  Untitled.jpg
Views: 274
Size:  18.7 KB

    https://file.io/WjfrjfFixgco

  2. #2
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,525

    Re: VB.net Slots game bug not easily apparent

    You would have been informed when you posted that, as a new member, your posts were subject to moderation. Please pay attention to these notices and don't post the same question multiple times. Your duplicate threads have been deleted.

  3. #3
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,525

    Re: VB.net Slots game bug not easily apparent

    As for the issue, you need to do a far better job of explaining it. Just providing us with all the source code and expecting us to work out what the problem is before we can even think about resolving it is not acceptable. You need to provide a FULL and CLEAR explanation of the SPECIFIC issue you're having. You need to explain exactly what you're trying to achieve, how you're trying to achieve it, what happens when you try and how that differs from your expectations. Providing the entire project should be a last resort. You should post only the relevant code directly, as text and formatted as code. In order to work out what's relevant, you need to have already debugged your code. If you don't know how to debug, stop what you're doing and learn that first. By the looks of things, you've almost certainly done far too much work already without having properly debugged the code you wrote along the way. By doing that, you can easily end uup with multiple issues that all interact and make diagnosing any of them very difficult. You need to break the overall problem down into smaller and smaller parts and ensure that each part works before going on to something else. If you have an issue, you only need to describe that one part to us.

  4. #4
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,222

    Re: VB.net Slots game bug not easily apparent

    And the first part of debugging, is setting a breakpoint and stepping through the code. If you don't know that, it's super easy, and nothing is more useful. You shouldn't need to ask why the amount got so high. Put a breakpoint in there and step through the code. You'll be able to watch how it gets there. That will focus the question down, and will likely answer it. Code isn't a black box (or at least it hasn't been for a few decades, now). You can watch it every step of the way. You can see what it's about to do before it has done it, so you can see what it will do.
    My usual boring signature: Nothing

  5. #5

    Thread Starter
    New Member
    Join Date
    Mar 2024
    Posts
    2

    Re: VB.net Slots game bug not easily apparent

    Thank you for the input. The whole program (excluding the visuals) is about 75 lines of code. Should be very easy to follow, if not, please let me know. And each spin is same as the previous spin interms of code execution. I suddenly get a huge bump in Total winnings on the 6th spin (much more than expected- see image). Below is the appropriate code. Please help if you are able to.

    Code:
    Private Sub ButtonSPIN_Click(sender As Object, e As EventArgs) Handles ButtonSPIN.Click
        Button1.Text = GetRandom(0, 19)
        Button2.Text = GetRandom(0, 19)
        Button3.Text = GetRandom(0, 19)
    
        If ((isEven(Button1.Text) = True) And (isEven(Button1.Text) = True) And (isEven(Button1.Text) = True)) Then
    
            Label_TotalWinnings.Text = (0.1 * Label_TotalCash.Text + Label_TotalWinnings.Text)
        End If
    
        If ((isEven(Button1.Text) = False) And (isEven(Button1.Text) = False) And (isEven(Button1.Text) = False)) Then
            Label_TotalWinnings.Text = (0.1 * Label_TotalCash.Text + Label_TotalWinnings.Text)
    
        End If
    
        If (((Button1.Text) = Button2.Text) And ((Button1.Text) = Button3.Text)) Then
    
            Label_TotalWinnings.Text = (Label_TotalCash.Text + Label_TotalWinnings.Text)
        End If
    
    
        Label_TotalSpinCount.Text = Label_TotalSpinCount.Text + 1
        Label_TotalWinnings.Text = Label_TotalWinnings.Text - 10
        Label_TotalCash.Text = Label_TotalCash.Text - 10
    
        If (Label_TotalWinnings.Text < 0 And Label_TotalCash.Text < 0) Then
    
            Me.Close()
    
        End If
    
    End Sub
    
    Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer
        Dim Generator As System.Random = New System.Random()
        Return Generator.Next(Min, Max)
    End Function
    
    Private Function isEven(numToCheck As Integer) As Boolean
        Return (numToCheck And 1) = 0
    End Function

    Name:  Untitled.jpg
Views: 95
Size:  18.7 KB

  6. #6
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,525

    Re: VB.net Slots game bug not easily apparent

    We have both told you that you need to debug the code yourself. Are you refusing to do that? If so, you'll likely find people who volunteer their time to help strangers unwilling to help those who won't help themselves. If not, go ahead and do it and, if you still need help, tell us what you find when you do it.

    We're not just telling you to debug because we don't want to. It's an essential skill for all developers and not being able to do so will cost you far more time than you invest learning how. It is in your best interest that you learn how to debug code. Telling you to do it the best help we can provide.

  7. #7
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,525

    Re: VB.net Slots game bug not easily apparent

    We can also provide some other general advice that may help you solve this issue but will definitely help you avoid issues in the future. These were provided on your question at SO but let's repeat them here.

    1. Turn Option Strict On. By default, VB allows you to play fast and loose with data types and it will implicitly convert them for you. This can lead to issues when the system guesses incorrectly what you are trying to do. With Option Strict On, you will be forced to be more explicit with data types, converting explicitly where required. This makes you think a bit more about what data types you're using and thus makes your code more robust. It feels a bit painful at first but that's only because you're used to not thinking about something that is really quite important. It soon becomes second nature and you're a better developer for it. You can turn Option Strict On for the current project in the project properties. You should also turn it On in the VS options, so it will be On by default for all future projects.

    2. Don't create multiple Random instances, especially in code that may be executed multiple times in quick succession. The Random class generates a pseudo-random sequence of numbers using the current system time as a seed by default. If you create multiple instances quickly, they may all use the same seed and thus produce the same sequence of numbers. Calling Next on each instance once only will give you multiple numbers with the same value. Create one instance and call Next on it repeatedly. If you're targeting .NET 6 or later, you can use the Shared property of the Random class to get a system-generated instance and use that all the time.

    3. Don't store numbers as text for anything other than display. You're generating numbers and assigning them to the Text property of a control, which converts them to Strings. If you use the Strings later, they are not treated as numbers, so any operation that requires numbers may not work as expected, e.g. "10" is less than "2". When you generate your numbers, assign them to fields of an appropriate numeric type. You can still display the values on controls but always use the actual numbers for any operations, e.g. comparison.

    If you fix all those points then you may find that your issue goes way. If it doesn't, you need to isolate where the issue actually occurs by debugging, as already described. Once you know where it happens, you can see why it happens. That may enable you to fix the issue for yourself. If you still nbeed help, you can tell us exactly what you found so we know exactly what to look for. You can provide a set of steps that we can follow to see the same behaviour you see. That's how to ask question about faulty code.

  8. #8
    PowerPoster
    Join Date
    Nov 2017
    Posts
    3,243

    Re: VB.net Slots game bug not easily apparent

    Multiple If statements that you posted have clear logic issues. Since this is almost certainly homework, I'm not going to tell you what the issues are, but you need to carefully examine all the aspects of your If statements.

  9. #9
    eXtreme Programmer .paul.'s Avatar
    Join Date
    May 2007
    Location
    Chelmsford UK
    Posts
    25,543

    Re: VB.net Slots game bug not easily apparent

    As jmcilhinney told you, numbers are numbers and text is text, however much it looks like a number on your screen.

    If you try Button1.Text = “20” + “5” what do you get??? You’d expect “25”, but Strings don’t work that way. The result is “205”

    Also I can see at least 2 logic errors, as you have also already been told. A logic error could happen where you’re referring to the wrong control in your code.

    The best advice is learning how to debug. You can output variable values at strategic points in your code, or set breakpoints and step through your code line by line to observe and track down what is happening and should be happening differently.

  10. #10
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    39,222

    Re: VB.net Slots game bug not easily apparent

    JMC covered this, but I'll point it out a bit more emphatically: Your random number generator is going to give you the same number each time. You need to create a single instance of Random at form scope, rather than creating a new one on each use. Otherwise, you can pretty much count on getting a whole bunch of repeated numbers.
    My usual boring signature: Nothing

  11. #11
    Powered By Medtronic dbasnett's Avatar
    Join Date
    Dec 2007
    Location
    Jefferson City, MO
    Posts
    9,778

    Re: VB.net Slots game bug not easily apparent

    Quote Originally Posted by Shaggy Hiker View Post
    JMC covered this, but I'll point it out a bit more emphatically: Your random number generator is going to give you the same number each time. You need to create a single instance of Random at form scope, rather than creating a new one on each use. Otherwise, you can pretty much count on getting a whole bunch of repeated numbers.
    A gimmeee
    Code:
        Private Shared Generator As New Random 'once and only once
        Public Function GetRandom(ByVal Min As Integer, ByVal Max As Integer) As Integer
            Return Generator.Next(Min, Max)
        End Function
    My First Computer -- Documentation Link (RT?M) -- Using the Debugger -- Prime Number Sieve
    Counting Bits -- Subnet Calculator -- UI Guidelines -- >> SerialPort Answer <<

    "Those who use Application.DoEvents have no idea what it does and those who know what it does never use it." John Wein

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width