A Game of Draw Poker

Martin LissKeep everyone healthy; Get Vaccinated
CERTIFIED EXPERT
Almost 50 years of programming experience. Click '+ More' in my "Full Biography" to see links to some articles I've written.
Published:
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.

Background


A while back I wrote an article called Simple Games which described how to create several "old school" games like Tic-Tac-Toe, Hangman and Blackjack, the latter of which used a set of graphical playing cards. The article has been well-received so I thought I'd write this article that also uses the graphical card deck.


The Card Deck


The card deck which is, or at least used to be, supplied by Microsoft is named VBcards.ocx. In the rest of this article I will refer to it as "the deck". It contains images of the normal 52 playing cards plus several other images that are useful in card games. If you have it on your computer then great. If not then I've attached it here so you can download it. After downloading you'll need to change the extension to "ocx".

Vbcards.txt


Playing the Game

Both versions of the game work the same and the pictures that follow come from the Visual Basic Classic (VB6) Version. 


The 'Deal' Button

Here is what the game looks like after clicking the 'Deal' button. A new, random, set of 5 cards are generated each time it's clicked. 


'Draw' Button


After the cards are dealt you are given the chance to replace one or more cards in the hope of improving your hand. You indicate which cards you want to replace (usually called "throw away" in poker) by clicking on one or more cards and the 'Draw' button becomes available. In the hand shown, adventurous people might throw away the two kings in hopes of getting a flush or straight flush, but most people would correctly throw away the clubs. After doing that the display looks like this:

If you change your mind about one or more cards you can click it/them and the old card will reappear. When you click the 'Draw' button the missing cards are replaced and as in the real game you don't get another chance.


'Cards Dealt' Button


When clicked, a message will be displayed that in the case of our original hand, will say "Two of Clubs, Five of Clubs, King of Diamonds, Three of Clubs and King of Spades".


'Evaluate Hand' Button


When you click this button a message will be displayed that, in the case of our original hand, will say "A pair of Kings".


'Other Cards' Button


As I mentioned above, the deck comes with more than just the 52 normal card faces. While it has nothing to do with Poker, I added this button so that you can see what else comes in the deck. Here's what you'll see after clicking it:


You may notice that I used 'Card 55' in the 'Draw Cards' paragraph.


About the deck


The deck comes with 69 images which consist of the 52 normal card faces and the 17 other images shown in the previous paragraph.


When writing code that involves the deck, the cards are referred to by a number from 1 to 69. Note that somewhat strangely, the cards are not in the normal spades, hearts, diamond, clubs order but rather spades, diamonds, clubs, hearts order:

  • 1 - 13 are the Spades
  • 14 - 26 are the Diamonds
  • 27 - 39 are the Clubs
  • 40 - 52 are the Hearts

Numbers 1, 14, 27 and 40 are the aces. 2, 15, 28, 41 are the twos, and the rest progress in numerical order to the kings.


Programming with the Deck


To use the deck in a program you need to add a Reference to it and the default name for the deck is Deck1.


Drawing a Card


I'm using "drawing" in the poker sense and to do so you just add a line of code like the following:

Deck1.ChangeCard = GetACard

In the code that I've supplied, GetACard is a function that returns an unused, random, number between 1 and 52. ChangeCard is a method supplied in the deck that IMO would be better named ChangeCardTo because it changes the image to be the one associated with the supplied number.


Evaluating a Hand and Other Code


The code behind the 'Evaluate Hand' button uses a group of functions named isPair, isTwoPair, isTrips, etc. all the way up to isRoyalFlush which examine the current hand and return True or False. The Dealt and Rank functions combine to generate the message from the 'Cards Dealt' button. 


One more important piece of code is contained in the Click event of the 'Deal' button and it looks like this in the Excel version:

Private Sub cmdDeal_Click()


    Dim lngIndex As Long
    ReDim mintCardCounts(1 To 13) As Integer
    Dim bTesting As Boolean
    
    ' Initialize the cards used
    mintCardsInHand(0) = 0
    mintCardsInHand(1) = 0
    mintCardsInHand(2) = 0
    mintCardsInHand(3) = 0
    mintCardsInHand(4) = 0


    ' The number of the card we are dealing
    mintCurrentCard = 0
    
    'bTesting = True
    
    ' Deal
    If bTesting Then
        ' Using 1, 10, 11, 12 and 13 (in any order) will
        ' create a spade royal flush
        Deck1.ChangeCard = 1
        imgCard0.Picture = Deck1.Picture
        mintCardsInHand(0) = 1


        Deck1.ChangeCard = 13
        imgCard1.Picture = Deck1.Picture
        mintCardsInHand(1) = 13


        Deck1.ChangeCard = 12
        imgCard2.Picture = Deck1.Picture
        mintCardsInHand(2) = 12


        Deck1.ChangeCard = 11
        imgCard3.Picture = Deck1.Picture
        mintCardsInHand(3) = 11


        Deck1.ChangeCard = 10
        imgCard4.Picture = Deck1.Picture
        mintCardsInHand(4) = 10
    Else
        Deck1.ChangeCard = GetACard
        imgCard0.Picture = Deck1.Picture
    
        Deck1.ChangeCard = GetACard
        imgCard1.Picture = Deck1.Picture


        Deck1.ChangeCard = GetACard
        imgCard2.Picture = Deck1.Picture


        Deck1.ChangeCard = GetACard
        imgCard3.Picture = Deck1.Picture


        Deck1.ChangeCard = GetACard
        imgCard4.Picture = Deck1.Picture
    End If


    GroupCards
    
    cmdDraw.Enabled = False
    For lngIndex = 0 To 4
        mintOldCardsInHand(lngIndex) = mintCardsInHand(lngIndex)
    Next
    imgCard0.Enabled = True
    imgCard1.Enabled = True
    imgCard2.Enabled = True
    imgCard3.Enabled = True
    imgCard4.Enabled = True
    cmdEvaluate.Enabled = True
    cmdCards.Enabled = True
    
    frmDrawPoker.Repaint
    
End Sub

What I'd like to point out is when line 19 is uncommented, you can change the code in lines 23 to 47 and set up different hands for testing purposes.


Differences Between the Two Versions


There is no difference in the way the game is played but there are some differences with the code. In the VB6 version I take advantage of its control array concept to simplify the handling of the card images.


A Problem with the Excel Version


When you open the workbook you will probably get this message. 


I assume that's because it's an old OCX and Office has "forgotten" about it but in my experience it's perfectly safe. As proof of that you can either click 'OK' or 'Cancel' and it still works.


Downloads

Excel: Draw Poker.xlsm

VB6: Draw Poker.zip


Enjoy!


If you find that this article has been helpful, please click the “thumb’s up” button below. Doing so lets me know what is valuable for EE members and provides direction for future articles. It also provides me with positive feedback in the form of a few points. Thanks!

3
4,167 Views
Martin LissKeep everyone healthy; Get Vaccinated
CERTIFIED EXPERT
Almost 50 years of programming experience. Click '+ More' in my "Full Biography" to see links to some articles I've written.

Comments (2)

Shums FarukManaging Director/Excel VBA Developer
CERTIFIED EXPERT
Distinguished Expert 2018

Commented:
Great Expert with great mind
Martin LissKeep everyone healthy; Get Vaccinated
CERTIFIED EXPERT
Most Valuable Expert 2017
Distinguished Expert 2023

Author

Commented:
Thanks.

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.