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
- Creating the Deck
- Dealing a New Game
- Keeping Score
- Hit and Stay
- Cleaning Up
Create a New Windows Application
Go to file – new – project and select a new visual basic windows project (see the screenshots for more detail).
Creating a new Project
/
Once this has been completed you should have something like this:
Creating the Game Deck
As this is a very basic tutorial we won't worry about all of the graphics for now. Firstly let's add two textboxes to the page as well as a button. Then well start to develop the function that will populate the main playing deck.
What we need to remember when we create something like a blackjack game is that there are 52 cards in a deck, and we need to ensure that we don't have more than 4 cards of the same type.
So what do we know, we have a deck of cards consisting of 4 types (Harts, Diamonds, Spades, and Clubs) and each suite has 13 cards in it (Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King). So lets find a way that we can construct a deck using VB.net.
Firstly lets create an ArrayList that will contain the deck, and then we'll add two constants that will contain the suite types as well as the different cards in the suite. Lets first create a method called fillDeck() and then add the two parameters to this method. We'll have something like this:
Private deck As ArrayList = New ArrayList()
Private Sub fillDeck()
Dim cards As String = "K,Q,J,10,9,8,7,6,5,4,3,2,1,"
Dim Suites As String = "C,S,H,D,"
End Sub
Well use these two parameters to construct our deck. Now the next thing that we need to add in a Random number generator. Well use the random class to create our random numbers. Add the following line of code to the end of your fillDeck() method:
Dim ran As Random = New Random()
For i = 0 To 10
ran.NextDouble()
Next
You'll notice that I have added a for loop that will generate 10 random numbers. I use this to ensure that there's always a unique number when the class is first called. I have found that sometimes you get the same first 2 – 3 numbers for up to 5 or 6 cycles.
Next we acutely need to build the deck. Firstly create a for loop that will stretch from 1 to 52. This will be used to fill the deck ArrayList.
Now we need to find a way of retrieving a true unique card combination. Let's add two integer objects one representing a position in the cards string and the other representing a position in the Suites array. Lets call them cardItem and suiteItem for short. Basically what this will be is a random double generated from our random class (ran) and then converted to an item within our original scope of possible items (for the cards string it's 13 and for the suite string it's 4.)
Dim cardItem As Integer = Integer.Parse((ran.NextDouble() * ((cards.Length - 1) / 2)).ToString().Split(".")(0))
Dim SuiteItem As Integer = Integer.Parse((ran.NextDouble() * ((Suites.Length - 1) / 2)).ToString().Split(".")(0))
This is the two variables that I have come up with. Basically it generated a new random number. Which is the multiplied with the amount of available items? (cards.Length – 1 -> String.Lenght starts at 0 instead of 1. Lastly you'll notice that I devide by 2. This is done because of the seperato items that has been inclded.)
Now lets add this new item to the deck ArrayList that we have created for our purpose:
deck.Add(cards.Split(",")(cardItem) + Suites.Split(",")(SuiteItem))
Here is what the completed method looks like till current:
Private Sub fillDeck()
Dim cards As String = "K,Q,J,10,9,8,7,6,5,4,3,2,1,"
Dim Suites As String = "C,S,H,D,"
Dim i As Integer = 0
Dim ran As Random = New Random()
For i = 0 To 10
ran.NextDouble()
Next
For i = 1 To 52
Dim cardItem As Integer = Integer.Parse((ran.NextDouble() * ((cards.Length - 1) / 2)).ToString().Split(".")(0))
Dim SuiteItem As Integer = Integer.Parse((ran.NextDouble() * ((Suites.Length - 1) / 2)).ToString().Split(".")(0))
deck.Add(cards.Split(",")(cardItem) + Suites.Split(",")(SuiteItem))
Next
End Sub
Although this is pretty effective and you'll see that you have a complete array with all 52 items in it, as soon as you exanimate this ArrayList you'll find that there's some of the items that actually occur more than four times. So let's create a method that will check to see if the item is currently in the deck, and the amount of occurrences in out deck. Let's call it checkItem. This method will take a parameter of the string that we'll use to search for. So go and create the following method:
Private Function checkItem(ByVal search As String)
End Function
There's several ways of finding how many times you have added this item to the list, basically it's up to you which one you would like to use. Here is the most effective one in my opinion: We step through the ArrayList, and basically check the text to our search criteria. If it matches, increase it. If it doesn't don't. In the event that there's more than 3 items already in the list (thus there's 4 items) return false, so that I know there's more than 3 items in the list already. Otherwise return true. To tell the program that it can add another item. The code will look something like this:
Private Function checkItem(ByVal search As String)
Dim j As Integer
Dim counter = 0
If (deck.Count > 0) Then
For j = 0 To deck.Count - 1
If (deck(j).ToString() = search) Then
counter += 1
End If
Next
End If
If (counter > 3) Then
Return False
End If
Return True
End Function
Next >>