none
Blackjack Help RRS feed

  • Question

  • I tell the program to only give numbers between 1 and 10 but when I deal occasionally it will spit out the number 0. I haven't seen it happen when I hit the button Hit though

    Here is my code and hank you for your help

    Public Class Form1
        Dim randomHC, randomYC, RandomNum As Integer
        Dim TotalYC As Integer
        Dim TotalHC As Integer
        Private Sub Randomizer_Tick(sender As Object, e As EventArgs) Handles Randomizer.Tick
            RandomNum = CInt(Int((10 * Rnd()) + 1))
        End Sub
        Private Sub Disable(TV As Boolean)
            cmdHit.Enabled = TV
            cmdStand.Enabled = TV
        End Sub
        Private Sub RNDdeal_Tick(sender As Object, e As EventArgs) Handles RNDdeal.Tick
            If cmdDeal.Visible = True Then
                Call Disable(False)
            End If
            randomHC = CInt(Int((10 * Rnd()) + 1))
            randomYC = CInt(Int((10 * Rnd()) + 1))
            If cmdHit.Enabled = True Then
                cmdNextTurn.Enabled = False
            Else
                cmdNextTurn.Enabled = True
            End If
        End Sub
        Private Sub cmdDeal_Click(sender As Object, e As EventArgs) Handles cmdDeal.Click
            lblUrDeck.Text = randomYC & ", " & RandomNum
            lblHcDeck.Text = randomHC & ", ?"
            cmdDeal.Visible = False
            cmdNextTurn.Visible = True
            lblUrTotal.Text = randomYC + RandomNum
            TotalHC = randomHC + RandomNum
            lblAITotal.Text = TotalHC
            Call Disable(True)

        End Sub
        Private Sub cmdHit_Click(sender As Object, e As EventArgs) Handles cmdHit.Click
            lblUrDeck.Text = lblUrDeck.Text & ", " & RandomNum
            Call Disable(False)
            lblUrTotal.Text = Val(lblUrTotal.Text) + RandomNum
        End Sub
        Private Sub cmdNextTurn_Click(sender As Object, e As EventArgs) Handles cmdNextTurn.Click
            lblHcDeck.Text = lblHcDeck.Text + ", ?"
            lblAITotal.Text = Val(lblHcDeck.Text) + RandomNum
            Call Disable(True)
        End Sub
    End Class


    Ben Hughes

    Thursday, May 4, 2017 2:27 PM

Answers

  • You are using timers to get some of the numbers. If it does not fire, those numbers will be zero.

    • Marked as answer by BadNewsHughes Wednesday, May 17, 2017 5:11 PM
    Saturday, May 6, 2017 9:43 PM

All replies

  • If you want to get a random number between two numbers you use the syntax:

            Dim rnd As New Random(Now.Millisecond)
            Dim wordIndex As Integer = rnd.Next(1, 10)
    


    Lloyd Sheen

    Thursday, May 4, 2017 2:35 PM
  • @badnews - first of all don't use Rnd, it is much easier to use the Random class.  Rnd is a holdover from VB6.  When using Random it is important to note that .Next is exclusive of the last parameter.

    examples

        Dim prng As New Random
        Dim randNum1_10incl10 As Integer
        Dim randNum1_10excl10 As Integer
    
        Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
            randNum1_10incl10 = prng.Next(11) ' zero to 10 
            randNum1_10excl10 = prng.Next(10) ' zero to 9, exclusive of 10
            randNum1_10incl10 = prng.Next(1, 11) ' one to 10
            randNum1_10excl10 = prng.Next(1, 10) ' one to 9, exclusive of 10
        End Sub
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.

    Thursday, May 4, 2017 3:04 PM
  • For some reason after I try both of the answers that were given it still happens

    Ben Hughes

    Thursday, May 4, 2017 10:12 PM
  • For some reason after I try both of the answers that were given it still happens

    Ben Hughes

    What 'it' still happens?  I changed the Rnd in your code and nothing else.

    Public Class Form1
        Private prng As New Random
        Dim randomHC, randomYC, RandomNum As Integer
        Dim TotalYC As Integer
        Dim TotalHC As Integer
        Private Sub Randomizer_Tick(sender As Object, e As EventArgs) Handles Randomizer.Tick
            RandomNum = prng.Next(1, 11)
        End Sub
        Private Sub Disable(TV As Boolean)
            cmdHit.Enabled = TV
            cmdStand.Enabled = TV
        End Sub
        Private Sub RNDdeal_Tick(sender As Object, e As EventArgs) Handles RNDdeal.Tick
            If cmdDeal.Visible = True Then
                Call Disable(False)
            End If
            randomHC = prng.Next(1, 11)
            randomYC = prng.Next(1, 11)
            If cmdHit.Enabled = True Then
                cmdNextTurn.Enabled = False
            Else
                cmdNextTurn.Enabled = True
            End If
        End Sub
        Private Sub cmdDeal_Click(sender As Object, e As EventArgs) Handles cmdDeal.Click
            lblUrDeck.Text = randomYC & ", " & RandomNum
            lblHcDeck.Text = randomHC & ", ?"
            cmdDeal.Visible = False
            cmdNextTurn.Visible = True
            lblUrTotal.Text = randomYC + RandomNum
            TotalHC = randomHC + RandomNum
            lblAITotal.Text = TotalHC
            Call Disable(True)
    
        End Sub
        Private Sub cmdHit_Click(sender As Object, e As EventArgs) Handles cmdHit.Click
            lblUrDeck.Text = lblUrDeck.Text & ", " & RandomNum
            Call Disable(False)
            lblUrTotal.Text = Val(lblUrTotal.Text) + RandomNum
        End Sub
        Private Sub cmdNextTurn_Click(sender As Object, e As EventArgs) Handles cmdNextTurn.Click
            lblHcDeck.Text = lblHcDeck.Text + ", ?"
            lblAITotal.Text = Val(lblHcDeck.Text) + RandomNum
            Call Disable(True)
        End Sub
    End Class
    


    "Those who use Application.DoEvents() have no idea what it does and those who know what it does never use it." - MSDN User JohnWein    Multics - An OS ahead of its time.

    Thursday, May 4, 2017 11:00 PM
  • You are using timers to get some of the numbers. If it does not fire, those numbers will be zero.

    • Marked as answer by BadNewsHughes Wednesday, May 17, 2017 5:11 PM
    Saturday, May 6, 2017 9:43 PM