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

Cleaning Up

There are some minor things that we want to look at in this are. These include some functionality that monitors the player, and enables the hit and stay buttons when something specific happens. (When he goes over 21, or when he clicks on the stay button.) Lets see what functionality we need to change.

Firstly we'll change the handler for the hit button somewhat.

This is what it looked like before we're starting with this:

TextBox1.Text += deck(currentDeckPosition)
total += Integer.Parse(detirmeneCardValue(deck(currentDeckPosition).ToString()))
currentDeckPosition += 1
 
TextBox3.Text = total.ToString()

This is pretty simple. Lets see what we need to do to check if the player is entitled to click on the hit / stay button. Firstly inorder to do this the player needs to have a total of less that 22. Were as 22 is the first total where you'll lose.

Lets add an if statement to this function. Something like this:

If (total > 21) Then
End If

Now. Lets disable the hit and stay buttons, and then call the functionality that checks if the dealer wins or not. (Button_3 in my case).

If (total > 21) Then
        Button1.Enabled = False
        Button2.Enabled = False
        Button3_Click(sender, e)
End If

Now let's take a look at the functionality that we need to add to the stay button to disable the two buttons when the user clicks on them.

Add these two line to the top of you're stay function:

Button1.Enabled = False
Button2.Enabled = False

Lastly we need to enable these two buttons in order to allow us to play the next game. Lets add this to the deal function:

Button2.Enabled = True
Button1.Enabled = True
TextBox5.Visible = False
TextBox6.Visible = False

Note that the two textboxes that I have added are there to clean up for the next game. (They are the W textbox that appears when a player wins.)

Now do a little bit of renaming and this is what you end up with a very basic representation of a blackjack game and how you can write your own. Check out the full code from the link on the left.

<< Previous