# VBForums UtilityBank > UtilityBank - Utilities >  VB: Blackjack

## MartinLiss

This version doesn't allow splitting pairs.

Edit: Updated to version 1.2.1

Version 1.2.0
Add variable bet amountAdd display of dealer countAdd variable number of decks. Also previously the deck was shuffled after every hand. It is now shuffled only when the cards run out
Version 1.2.1
Corrected bug where the bet amount was handled incorrectly when it was $1,000 or moreAllow player to set the bet amount to the maximum by double-clicking the bet amount
Version 1.2.2
Corrected bug where the deck could sometimes contain less than 52 cards
Version 1.2.3
Corrected bug where non-dollar-sign-using locales would get an error

----------


## bpd

Very nice.  I didn't notice the value of the dealer's hand being displayed.

----------


## MartinLiss

Thanks. I didn't see a need for the dealer's hand value.

----------


## GaryMazzone

Shouldn't the Ace be allowed to be either 1 or 11?

----------


## MartinLiss

> Shouldn't the Ace be allowed to be either 1 or 11?


It is. Did you find a case where I didn't handle it coprrectly?

----------


## GaryMazzone

Yes. I got a 6 and A on the deal.  I drew a 9 and recorded a bust.

----------


## MartinLiss

Thanks, I'll look into it and fix it.

----------


## MartinLiss

Updated to version 1.0.2

----------


## MartinLiss

BTW, I would be remiss if I did not mention that while it has been drastically changed by me, the original code for this game came from a Blackjack-like game called Pontoon that I helped it's creator, member GregorBerry, write.

----------


## bushmobile

Just to let you know, you've left your Testing deck set up in the Shuffle Sub. 

If only casinos were so considerate!   :Big Grin:

----------


## MartinLiss

Oops.   :Blush:  Fixed it, thanks.

----------


## MartinLiss

Updated to version 1.1.0

----------


## bpd

> I didn't see a need for the dealer's hand value.


Technically there isn't a "need".  I just didn't trust the dealer to tally the values correctly!   :Roll Eyes (Sarcastic):   :Wink:

----------


## MartinLiss

Updated to version 1.2.0

----------


## Google

File not found: Ideas.doc

Thought you'd like to know for whatever reason.

BTW: Can you help me out in the thread "How can I shuffle a deck of cards in VB6?" here: http://www.vbforums.com/showthread.php?p=2402408
please!?

I shouldn't edit my post as much... lol

----------


## MartinLiss

> File not found: Ideas.doc
> 
> Thought you'd like to know for whatever reason.
> 
> BTW: Can you help me out in the thread "How can I shuffle a deck of cards in VB6?" here: http://www.vbforums.com/showthread.php?p=2402408
> please!?


In my first post I mentioned that file so don't worry about it. Also as someone mentioned in the thread you pointed me to, I have a Shuffle sub in the blackjack game's code. Take a look at it and if you have questions then please let me know.

----------


## Google

I just noticed that you include VBCARDS!  Thanks alot for this.  I've been looking for it for a while!

Yeah, I saw you're shuffling routine and I don't understand it.  But thanks for looking  :Smilie: 

Okay, and now that I've played you're BLACKJACK... it's nice but can you add in a "Bet the Bank" option cause after each win I have to click the ^ button a lot to get to the total. I have nearly 3,000 bucks right now. Or, you could set the bet amount back to $5.00 so all I have to do is click the down arrow button once. I would like that change. Otherwise, it's a great game.

I just noticed that the computer reads $1,000.00 as $1.00.

You can see this quickly if you start with a bankroll at $1,000.00 and bet 1,000.00 and then lose. Instead of losing everything the bankroll goes to $999.00 and the betting bar thingy goes all wonky.

----------


## MartinLiss

Thanks for the suggestions and finding the bugs. I don't have time right now but in a few hours I'll post a commented shuffling routine.

----------


## MartinLiss

OK, here is a much stripped-down version of the program with a Shuffle sub that has comments. Let me know if you have any questions.

----------


## bushmobile

Martin, don't you think that it's bad programming practice to include a loop that could (in theory at least) never end?

I'm not trying to pick a fight, just curious as to your opinion as you've been doing this a lot longer than me   :Big Grin:

----------


## MartinLiss

> Martin, don't you think that it's bad programming practice to include a loop that could (in theory at least) never end?
> 
> I'm not trying to pick a fight, just curious as to your opinion as you've been doing this a lot longer than me


I assume you are talking about the Do Until mbOKIsChecked loop. I agree that you should not have loops that will never end but I don't believe that is one of them. It runs only as long as the player hasn't hit the OK button or ended the program. However if you have a better solution I'm open to suggestion.

----------


## bushmobile

I was actually talking about the shuffle loop:


VB Code:
Do While mcolDeck.Count < 52
        intCard = Int(52 * Rnd + 1)
        On Error Resume Next
        mcolDeck.Add CStr(intCard), CStr(intCard)
    Loop

Although longer, these probably turn out to be less lines of executed code:


VB Code:
' mColDeck is populated once (Form_Load or whereever) and then shuffled from the existing pack 
Private Sub Shuffle()
    Dim colTemp As Collection
    Dim intCard As Integer
    
    Randomize
    
    Set colTemp = New Collection
     Do While mcolDeck.Count
        intCard = Int((mcolDeck.Count - 1) * Rnd + 1)
        colTemp.Add mcolDeck(intCard)
        mcolDeck.Remove intCard
    Loop
     Set mcolDeck = Nothing
    For intCard = 1 To colTemp.Count
        mcolDeck.Add colTemp(intCard)
    Next intCard
    Set colTemp = Nothing
End Sub
 ' or repopulating each time:
Private Sub Shuffle()
    Dim colTemp As Collection
    Dim intCard As Integer
    
    Randomize
    
    Set colTemp = New Collection
    Set mcolDeck = Nothing
     For intCard = 1 To 52
        colTemp.Add Cstr(intCard)
    Next intCard
     Do While colTemp.Count
        intCard = Int((colTemp.Count - 1) * Rnd + 1)
        mcolDeck .Add colTemp(intCard)
        colTemp.Remove intCard
    Loop
     Set colTemp = Nothing
End Sub

(I've just written those two out, so apologies if there's any errors)

----------


## MartinLiss

That loop can not go on forever. I don't know how many numbers it generates per second but I'm sure it's a large number and so the chance of not getting an unused number in a few seconds (or less) is vanishingly small.

I think I'll keep my routine the way it is but of course you are welcome to do what ever you want with your copy.

----------


## MartinLiss

Updated to version 1.21 based on the comments in post #17 by Google.

----------


## bushmobile

> That loop can not go on forever. I don't know how many numbers it generates per second but I'm sure it's a large number and so the chance of not getting an unused number in a few seconds (or less) is vanishingly small.


Vanishingly small...but possible?  :Ehh:  But seriously, I know very well that that would never happen, but relying on chance is bound to make the process slower than it could be.

I did a speed test with the three shuffle subs mentioned in post #22. I called them Shuffle, ShuffleA and ShuffleB respectively. (3 lots of 3000 repetitions)



```
 Shuffle
 -------
 1.83535142036209 
 1.8560085150487 
 1.84219167519894 

 Shuffle A
 ---------
 0.405486451490343 
 0.406130108714934 
 0.404578794232228 

 Shuffle B
 ---------
 0.353086216264916 
 0.350343130202302 
 0.355339295916101
```

I've attached the project I used to do the speed test.

----------


## Rich2189

Found a bug:



```

GetRawAmount = CDbl(frmGame.lblBet.Caption)
```

frmGame.lblBet.Caption holds the value "$10.00"

Since storing "£10.00" in that variable works I'm assuming it crashes because my computer is English(UK).

Changing the line below in frmGame_Load fixes it.



```

lblBet.Caption = "$10.00"

'for:

lblBet.Caption = 10
lblBet.Caption = Format(lblBet.Caption, "Currency")
```

----------


## afwebvrwbbre

I found a bug.

I got a subscript out of range error and it points to the line Deck1.ChangeCard = mcolDeck(NextCard).
The values at the time of the error are
Deck1.ChangeCard = 25
NextCard = 52

----------


## MartinLiss

That line appears in three different places: cmdHit_Click, mnuNewHand_Click and PlayCPUHand. Do you happen to know where it was when you got the error?

----------


## MartinLiss

Corrected and updated.

----------


## MartinLiss

Updated to version 1.2.3

----------


## UnLoad

what version of vb does the code run in?

----------


## MartinLiss

Vb 6

----------


## UnLoad

thank you, do you know where i can download a trial version of vb6

----------


## MartinLiss

Go to eBay.

----------


## Paddymay

vbp files don't have an association, and it won't open properly when it's a vbproj file!

----------


## MartinLiss

> vbp files don't have an association, and it won't open properly when it's a vbproj file!


Well I think that's your problem.

----------


## si_the_geek

To expand on what Marty said, the project is for Classic VB (VB6 to be precise), and you are using VB.Net (VB2008).

Despite both having VB in the name, they are not directly compatible - to use a project from one in the other requires some level of re-writing (often taking more effort than re-creating the project from scratch).

----------


## dbender

Can I use the Vbcards.ocx control in a VB Express 2008 project?  I downloaded it, copied it into my c:\windows\system32 folder, and tried to register it using regsvr32, but I keep getting an error message saying the module could not be loaded.  I've double-checked that I'm using the correct folder path in regsvr32, but the other part of the error message says there may be a problem with the ocx.

If this VB6-created control is incompatible with VB2008, does anyone know of a similar control that would be compatible?  Thanks.

Duane Bender

----------


## John_Wayne

I can't open the game
It keeps coming up with errors.
I'm using VB 6.0

----------


## MartinLiss

> I can't open the game
> It keeps coming up with errors.
> I'm using VB 6.0


I just downloaded it and it runs perfectly. What kind of errors are you getting?

----------


## dehboy

I downloaded the zip and extracted everything, but I don't see any code or executables...  I was hoping to play a little and check the code to see how different classes/routines can be used in vb.  Please help?

----------


## MartinLiss

There is no executable in the zip file but everything else you need to run the project is in the zip file.

----------


## si_the_geek

Based on another thread it seems that dehboy is using VBScript rather than VB 5/6, so will not be able to use the project - and even copying parts of the code is unlikely to be worthwhile.

----------


## dehboy

Ah, I see.  I just downloaded Microsoft Visual Basic 2010 Express, so hopefully I'll be able to work with everything now.  Thanks guys!

----------


## MartinLiss

I'm sorry but while you now have a nice tool to work with, you still won't be able to do anything with my code because it was written in VB6 which is not directly compatible with Microsoft Visual Basic 2010 Express or any other member of the VB.Net family of languages.

----------

