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

Keeping Score

The main idea of the game is to find out who wins (you or the dealer). So here we'll take a look at how to determine this.

Let's create a new function called detirmeneCardValue(ByVal card As String). This will determine what the value of a specific card combination would be. Here is the code for this:

Private Function detirmeneCardValue(ByVal card As String)
        Dim cardCount As String = card.Substring(0, card.ToString().Length - 1)
        If (cardCount = "Q") Or (cardCount = "K") Or (cardCount = "J") Then
                cardCount = "10"
        End If
        
        Return card
End Function

Basically the only tricky part is at line 1. What I'm doing here is get the value before the suite. So this will return something like 1 – 10, J Q K. Then I check if this card is a J Q or K, and then return "10"

Now let's make some changes to our deal function. Firstly we need to create two variables to keep track of the total of the dealer and the total of the player. Add these two variables as global variable:

Private total As Integer = 0
Private dealerTotal As Integer = 0

In the deal function we need to firstly reset these two variables:

total = 0
dealerTotal = 0

Then let's add the following code to our deal function just before each of the following statements: currentDeckPosition += 1. We're going to call the function that we have just created with the card that has just been loaded. This will then return a represented value for this function. For the first two add the following:

total += Integer.Parse(detirmeneCardValue(deck(currentDeckPosition).ToString()))

And for the last two add the following items:

dealerTotal += Integer.Parse(detirmeneCardValue(deck(currentDeckPosition).ToString()))

Lastly we need to display these values somewhere. Lets create two textboxes, textbox3 and textbox4, and write these values to them.

Now we've seen that it's quite easy to write a blackjack game. Next we'll take a look at creating the hit and stay functions.

<< Previous

Next >>