# VBForums CodeBank > Codebank - Game Programming >  [VB.NET] Useful Types for Card Games

## jmcilhinney

Edit: I just remembered that there's a VB 2005 starter kit for a card game so this is probably redundant.  Users of previous version can still download the starter kit and open the code files, even if they can't open the project.  If anyone has a specific desire for me to continue with this then by all means say so, otherwise I might just leave it.

Below is some rough code for some types that can form the basis of any card game.  They are as yet unfinsihed but I thought I'd post them anyway.  I will be making improvements to them myself soon, but feel free to use the code yourself and make your own improvents in the mean time.  I intend to create a project that incorporates the Windows cards.dll (or is it cards32.dll, can't remember) at some stage in the future also.
VB Code:
Public Enum Face
    None 'Used for Jokers
    Ace
    Two
    Three
    Four
    Five
    Six
    Seven
    Eight
    Nine
    Ten
    Jack
    Queen
    King
End Enum
 Public Enum Suit
    None 'Used for Jokers
    Hearts
    Clubs
    Diamonds
    Spades
End Enum

VB Code:
Public Structure Card
     Private m_Face As Face
    Private m_Suit As Suit
     Public ReadOnly Property Face() As Face
        Get
            Return Me.m_Face
        End Get
    End Property
     Public ReadOnly Property Suit() As Suit
        Get
            Return Me.m_Suit
        End Get
    End Property
     Public ReadOnly Property IsJoker() As Boolean
        Get
            Return Me.m_Face = Face.None AndAlso Me.m_Suit = Suit.None
        End Get
    End Property
     Public Sub New(ByVal face As Face, ByVal suit As Suit)
        If face = face.None Xor suit = suit.None Then
            Throw New ApplicationException("If either face or suit is None then the other must also be None.")
        Else
            Me.m_Face = face
            Me.m_Suit = suit
        End If
    End Sub
 End Structure

VB Code:
Public Class Hand
    Inherits CollectionBase
     Default Public Property Item(ByVal index As Integer) As Card
        Get
            Return CType(Me.InnerList(index), Card)
        End Get
        Set(ByVal Value As Card)
            Me.InnerList(index) = Value
        End Set
    End Property
     Public Function Add(ByVal card As Card) As Integer
        Return Me.InnerList.Add(card)
    End Function
     Public Sub Insert(ByVal index As Integer, ByVal card As Card)
        Me.InnerList.Insert(index, card)
    End Sub
 End Class

VB Code:
Public Class Deck
     Private cards As Hand 'The cards currently in the deck.
    Private rand As Random 'Used to generate random numbers.
     Public ReadOnly Property Count() As Integer
        Get
            Return Me.cards.Count
        End Get
    End Property
     Public Sub New()
        Me.cards = New Hand
         'Create a full deck with no Jokers.
        For Each suit As Suit In [Enum].GetValues(GetType(Suit))
            If suit <> suit.None Then
                For Each face As Face In [Enum].GetValues(GetType(Face))
                    If face <> face.None Then
                        Me.cards.Add(New Card(face, suit))
                    End If
                Next face
            End If
        Next suit
         Me.rand = New Random
    End Sub
     Public Sub Shuffle()
        Dim shuffledCards As New Hand
         While Me.cards.Count > 0
            Dim index As Integer = Me.rand.Next(0, Me.cards.Count)
             'Add a card at random to the new deck from the existing deck.
            shuffledCards.Add(Me.cards(index))
             'Remove the chosen card from the existing deck.
            Me.cards.RemoveAt(index)
        End While
         Me.cards = shuffledCards
    End Sub
     Public Sub Cut(ByVal upperCount As Integer)
        'Move the first upperCount cards to the bottom.
        For i As Integer = 1 To upperCount
            Me.cards.Add(Me.cards(0))
            Me.cards.RemoveAt(0)
        Next i
    End Sub
     'Returns the next card without removing it from the deck.
    Public Function PeekNextCard() As Card
        Return Me.cards(0)
    End Function
     'Returns the next card and removes it from the deck.
    Public Function GetNextCard() As Card
        Dim nextCard As Card = Me.cards(0)
         Me.cards.RemoveAt(0)
        Return nextCard
    End Function
     'Returns the specified number of hands, each with the specified number of cards.
    Public Function Deal(ByVal handCount As Integer, ByVal cardCount As Integer) As Hand()
        Dim hands(handCount - 1) As Hand
         For i As Integer = 1 To cardCount Step 1
            For Each hand As Hand In hands
                hand.Add(Me.GetNextCard)
            Next hand
        Next i
         Return hands
    End Function
 End Class

----------


## Hack

As this is regarding game programming, I have moved this thread from the VB.NET CodeBank

----------


## phillt78

to let you know a little about myself i have only been coding vb for about six months the way i coded my cards was to define a type card in a module the code for that is:


VB Code:
Type card
     Index As Integer
     value As Integer
     suit As String
     picturefront As String
     pictureback As String
     position As Integer
     shuffled As Boolean
     
End Type
    and then in a second module set up a few arrays of type card these are



VB Code:
Public deck(52) As card
Public firstdeck(52) As card
Public seconddeck(52) As card
Public thirddeck(52) As card
Public fourthdeck(52) As card
Public fithdeck(52) As card
Public sixthdeck(52) As card
Public shoeshuff1(78) As card
Public shoeshuff2(78) As card
Public firsthalfshoe(156) As card
Public secondhalfshoe(156) As card
Public shoe(312) As card
Public path As String
Public item As Integer
Public number1 As Integer
Public number2 As Integer
Public topdeck(26) As card
Public bottomdeck(26) As card
Public tempdeck(52) As card
Public firstQuaterDeck(13) As card
Public secondquaterdeck(13) As card
Public thirdquaterdeck(13) As card
Public fourthquaterdeck(13) As card

The set up for the cards is simple but longwinded


VB Code:
Public Function setupcards()
'sets the actual deck in suit order used at the start form load
Dim count, x As Integer
'hearts'
 For x = 52 To 40 Step -1
    deck(x).Index = (x - 39)
    deck(x).suit = "hearts"
        If (x - 39) < 10 Then
           deck(x).value = (x - 39)
        End If
        If (x - 39) > 10 Then
           deck(x).value = 10
        End If
Next x
 'clubs
 For x = 39 To 27 Step -1
    deck(x).Index = (x - 26)
    deck(x).suit = "clubs"
         If (x - 26) < 10 Then
            deck(x).value = (x - 26)
         End If
         If (x - 26) > 10 Then
            deck(x).value = (10)
        End If
 Next x
 'diamonds
 For x = 26 To 14 Step -1
    deck(x).Index = (x - 13)
    deck(x).suit = "diamonds"
        If (x - 13) < 10 Then
           deck(x).value = (x - 13)
        End If
        If (x - 13) > 10 Then
           deck(x).value = (10)
        End If
Next x
 'spades
 For x = 13 To 1 Step -1
    
    If x < 10 Then
       deck(x).value = x
    End If
    If x > 10 Then
       deck(x).value = 10
    End If
    deck(x).Index = x
    deck(x).suit = "spades"
Next x
For count = 1 To 52
    deck(count).shuffled = False
Next count
   'set back of cards
For x = 1 To 52
    deck(x).pictureback = path & "ims\cardback.jpg"
Next x
'set front art work
deck(1).picturefront = path & "ims\Aspades.jpg"
deck(2).picturefront = path & "ims\2spades.jpg"
deck(3).picturefront = path & "ims\3spades.jpg"
deck(4).picturefront = path & "ims\4spades.jpg"
deck(5).picturefront = path & "ims\5spades.jpg"
deck(6).picturefront = path & "ims\6spades.jpg"
deck(7).picturefront = path & "ims\7spades.jpg"
deck(8).picturefront = path & "ims\8spades.jpg"
deck(9).picturefront = path & "ims\9spades.jpg"
deck(10).picturefront = path & "ims\10spades.jpg"
deck(11).picturefront = path & "ims\Jspades.jpg"
deck(12).picturefront = path & "ims\Qspades.jpg"
deck(13).picturefront = path & "ims\Kspades.jpg"
deck(14).picturefront = path & "ims\Adiamonds.jpg"
deck(15).picturefront = path & "ims\2Diamonds.jpg"
deck(16).picturefront = path & "ims\3diamonds.jpg"
deck(17).picturefront = path & "ims\4diamonds.jpg"
deck(18).picturefront = path & "ims\5diamonds.jpg"
deck(19).picturefront = path & "ims\6diamonds.jpg"
deck(20).picturefront = path & "ims\7diamonds.jpg"
deck(21).picturefront = path & "ims\8diamonds.jpg"
deck(22).picturefront = path & "ims\9diamonds.jpg"
deck(23).picturefront = path & "ims\10diamonds.jpg"
deck(24).picturefront = path & "ims\Jdiamonds.jpg"
deck(25).picturefront = path & "ims\Qdiamonds.jpg"
deck(26).picturefront = path & "ims\kdiamonds.jpg"
deck(27).picturefront = path & "ims\Aclubs.jpg"
deck(28).picturefront = path & "ims\2clubs.jpg"
deck(29).picturefront = path & "ims\3clubs.jpg"
deck(30).picturefront = path & "ims\4clubs.jpg"
deck(31).picturefront = path & "ims\5clubs.jpg"
deck(32).picturefront = path & "ims\6clubs.jpg"
deck(33).picturefront = path & "ims\7clubs.jpg"
deck(34).picturefront = path & "ims\8clubs.jpg"
deck(35).picturefront = path & "ims\9clubs.jpg"
deck(36).picturefront = path & "ims\10clubs.jpg"
deck(37).picturefront = path & "ims\Jclubs.jpg"
deck(38).picturefront = path & "ims\Qclubs.jpg"
deck(39).picturefront = path & "ims\Kclubs.jpg"
deck(40).picturefront = path & "ims\Ahearts.jpg"
deck(41).picturefront = path & "ims\2hearts.jpg"
deck(42).picturefront = path & "ims\3hearts.jpg"
deck(43).picturefront = path & "ims\4hearts.jpg"
deck(44).picturefront = path & "ims\5hearts.jpg"
deck(45).picturefront = path & "ims\6hearts.jpg"
deck(46).picturefront = path & "ims\7hearts.jpg"
deck(47).picturefront = path & "ims\8hearts.jpg"
deck(48).picturefront = path & "ims\9hearts.jpg"
deck(49).picturefront = path & "ims\10hearts.jpg"
deck(50).picturefront = path & "ims\Jhearts.jpg"
deck(51).picturefront = path & "ims\Qhearts.jpg"
deck(52).picturefront = path & "ims\Khearts.jpg"
End Function
these decks are designed to simulate the packs used in the casino where i work. and so chimile shuffle is an important function i use two random numbers generated here:

VB Code:
Public Function random()
' a simple random number gen
Randomize
number1 = 52 * Rnd
Randomize
number2 = 52 * Rnd
checkrandomnumbers
End Function
and using those perform a selection sort:


VB Code:
Public Function ChimShuffle()
'sets up the cards for chim shuffle
Dim count, x, pos As Integer
'chimmile shuffle
'set temp deck order to deck order
For count = 1 To 52
     tempdeck(count) = deck(count)
     
Next count
'perform chim shuffle
For x = 1 To 52
    random
    checkrandomnumbers
    shuffledeck
Next x
  'put shuffled deck back together in new order
For count = 1 To 52
    deck(count) = tempdeck(count)
    tempdeck(count).shuffled = False
Next count
'check deck for consistancy
deckcheck
value
For x = 1 To 52
    If deck(x).picturefront = "" Then
       MsgBox ("a card is missaing at position " & x)
    End If
Next x
End Function
 Public Function shuffledeck()
'this is the actual chim shuffle it is called from within a fixed itteration loop
'this works like a slection sort card at position 1 is copied to the same position in the temp deck
'number 1 is then overwritten by number 2
'number 2 is then overwritten by number 1 the positions are therefore exchanged
 'set deck to hold first random number
deck(number1) = tempdeck(number1)
 'set tempdeck to overwrite first number with second number
tempdeck(number1) = tempdeck(number2)
 'set tepmdeck to overwrite second number with first
tempdeck(number2) = deck(number1)
  End Function
The other shuffles are a four way cut and a simple riffle shuffle.
My initial intention was to start with a simple game like poker and work up to blackjack in a virtual casno then code it in java so that i can plug it into an applet an let it run on my website.
This is a very longwinded way of doing things but this is an acurate simulation of what happens on a gaming table.

----------


## iRoN_RoCK

How to sort cards in a hand? Can we use LINQ?

----------


## jmcilhinney

> How to sort cards in a hand? Can we use LINQ?



vb.net Code:
Dim cards = From c In myHand.Cast(Of Card)() _
            Order By c.Suit, c.Face _
            Select c
That will give you a sorted sequence but it doesn't change the Hand itself.  For that you'd have to clear out all the existing Cards and add them all again from the sequence.  This code is a bit long in the tooth now anyway so a bit of updating would be in order.  For a start, I'd look at inheriting Hand from Collection(Of Card) rather than CollectionBase.  I would also add a constructor that would allow creation directly from a list of Cards, which you could get from your LINQ query.  You could also have the Card type implement IComparable and then an array or collection could be sorted automatically.

----------


## somekidben

VERY late on a response but how could I use this for the card game WAR?

----------


## jmcilhinney

> VERY late on a response but how could I use this for the card game WAR?


This is a CodeBank thread so there's no such thing as a late response.  I'm afraid that I'm not familiar with that game though, so I wouldn;t be able to answer the question without additional information.

----------


## cba01

Hi, phillt78

I liked your code, but it seems that there is some code missing.
  checkrandomnumbers()
    deckcheck()
        value()

 is not declared.

Can you please resubmit the complete code, maybe in a zip file.

thank you

----------

