Black Jack in VB.NET

Written By: Pedri

- 12 Jan 2008 -
















Description: This tutorial teaches us to program a player vs dealer blackjack game using Visual Basic.NET

  1. Creating the Deck
  2. Dealing a New Game
  3. Keeping Score
  4. Hit and Stay
  5. Cleaning Up

Hit and Stay

Firstly let's create two buttons. One called Hit and one called stay. I'm firstly going to handle the hit function:

For the hit functionality let's create a handler to handle the hit buttons onclick event. Basically all that this function needs to do is get the next card, and determine the value of that card (Which is then added to the player's total). This is pretty simple and I have handled all of this in the deal function. So without any delay here's the code:

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        TextBox1.Text += deck(currentDeckPosition)
        total += Integer.Parse(detirmeneCardValue(deck(currentDeckPosition).ToString()))
        currentDeckPosition += 1
 
        TextBox3.Text = total.ToString()
End Sub

Now let's take a look at the stay function:

Create a function to handle the onclick event for your stay button. This is also not that complex, so let's jump right in:

Let's create a function called FindWinner() this will determine who the winner is. So let's start. Firstly this will check to see that the dealer's Total is less than 22. (Thus 21 and less.) If the dealer total is greater than the player total it will reports that the dealer wins. Let's take a look at the code:

Private Function FindWinner()
        If (dealerTotal < 22) Then
                If dealerTotal > total Then
                        Return True
                End If
        End If
        If (total > 21) Then
                Return True
        End If
                
        Return False
End Function

Now we need to create a function to determine if the dealer hits or stays. For this example I'll say that if the dealer has less than 16 he hits. Otherwise he'll stay. Once agene I have gone through all of this code so it's pretty simple. Here is the function:

Private Sub FindDealerWinningHand()
        While dealerTotal < 16
                TextBox3.Text += deck(currentDeckPosition)
                dealerTotal += Integer.Parse(detirmeneCardValue(deck(currentDeckPosition).ToString()))
                TextBox4.Text = dealerTotal.ToString()
                currentDeckPosition += 1
        End While
End Sub

Now let's add two textboxes and make them invisible. Let's say textbox5 and textbox6. If the player wins we'll display textbox5 and if the dealer wins we'll display textbox6.

So finally the code to handle the stay button will look something like this:

Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
        If (Not (FindWinner())) Then
                FindDealerWinningHand()
                If (Not (FindWinner())) Then
                        TextBox5.Visible = True
                Else
                        TextBox6.Visible = True
                End If
        Else
                TextBox6.Visible = True
        End If
End Sub

Here's a screenshot of what our user interface looks like currently:

Black Jack in VB.NET 4

<< Previous

Next >>