Results 1 to 8 of 8

Thread: [VB.NET] Useful Types for Card Games

  1. #1

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    [VB.NET] Useful Types for Card Games

    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:
    1. Public Enum Face
    2.     None 'Used for Jokers
    3.     Ace
    4.     Two
    5.     Three
    6.     Four
    7.     Five
    8.     Six
    9.     Seven
    10.     Eight
    11.     Nine
    12.     Ten
    13.     Jack
    14.     Queen
    15.     King
    16. End Enum
    17.  
    18. Public Enum Suit
    19.     None 'Used for Jokers
    20.     Hearts
    21.     Clubs
    22.     Diamonds
    23.     Spades
    24. End Enum
    VB Code:
    1. Public Structure Card
    2.  
    3.     Private m_Face As Face
    4.     Private m_Suit As Suit
    5.  
    6.     Public ReadOnly Property Face() As Face
    7.         Get
    8.             Return Me.m_Face
    9.         End Get
    10.     End Property
    11.  
    12.     Public ReadOnly Property Suit() As Suit
    13.         Get
    14.             Return Me.m_Suit
    15.         End Get
    16.     End Property
    17.  
    18.     Public ReadOnly Property IsJoker() As Boolean
    19.         Get
    20.             Return Me.m_Face = Face.None AndAlso Me.m_Suit = Suit.None
    21.         End Get
    22.     End Property
    23.  
    24.     Public Sub New(ByVal face As Face, ByVal suit As Suit)
    25.         If face = face.None Xor suit = suit.None Then
    26.             Throw New ApplicationException("If either face or suit is None then the other must also be None.")
    27.         Else
    28.             Me.m_Face = face
    29.             Me.m_Suit = suit
    30.         End If
    31.     End Sub
    32.  
    33. End Structure
    VB Code:
    1. Public Class Hand
    2.     Inherits CollectionBase
    3.  
    4.     Default Public Property Item(ByVal index As Integer) As Card
    5.         Get
    6.             Return CType(Me.InnerList(index), Card)
    7.         End Get
    8.         Set(ByVal Value As Card)
    9.             Me.InnerList(index) = Value
    10.         End Set
    11.     End Property
    12.  
    13.     Public Function Add(ByVal card As Card) As Integer
    14.         Return Me.InnerList.Add(card)
    15.     End Function
    16.  
    17.     Public Sub Insert(ByVal index As Integer, ByVal card As Card)
    18.         Me.InnerList.Insert(index, card)
    19.     End Sub
    20.  
    21. End Class
    VB Code:
    1. Public Class Deck
    2.  
    3.     Private cards As Hand 'The cards currently in the deck.
    4.     Private rand As Random 'Used to generate random numbers.
    5.  
    6.     Public ReadOnly Property Count() As Integer
    7.         Get
    8.             Return Me.cards.Count
    9.         End Get
    10.     End Property
    11.  
    12.     Public Sub New()
    13.         Me.cards = New Hand
    14.  
    15.         'Create a full deck with no Jokers.
    16.         For Each suit As Suit In [Enum].GetValues(GetType(Suit))
    17.             If suit <> suit.None Then
    18.                 For Each face As Face In [Enum].GetValues(GetType(Face))
    19.                     If face <> face.None Then
    20.                         Me.cards.Add(New Card(face, suit))
    21.                     End If
    22.                 Next face
    23.             End If
    24.         Next suit
    25.  
    26.         Me.rand = New Random
    27.     End Sub
    28.  
    29.     Public Sub Shuffle()
    30.         Dim shuffledCards As New Hand
    31.  
    32.         While Me.cards.Count > 0
    33.             Dim index As Integer = Me.rand.Next(0, Me.cards.Count)
    34.  
    35.             'Add a card at random to the new deck from the existing deck.
    36.             shuffledCards.Add(Me.cards(index))
    37.  
    38.             'Remove the chosen card from the existing deck.
    39.             Me.cards.RemoveAt(index)
    40.         End While
    41.  
    42.         Me.cards = shuffledCards
    43.     End Sub
    44.  
    45.     Public Sub Cut(ByVal upperCount As Integer)
    46.         'Move the first upperCount cards to the bottom.
    47.         For i As Integer = 1 To upperCount
    48.             Me.cards.Add(Me.cards(0))
    49.             Me.cards.RemoveAt(0)
    50.         Next i
    51.     End Sub
    52.  
    53.     'Returns the next card without removing it from the deck.
    54.     Public Function PeekNextCard() As Card
    55.         Return Me.cards(0)
    56.     End Function
    57.  
    58.     'Returns the next card and removes it from the deck.
    59.     Public Function GetNextCard() As Card
    60.         Dim nextCard As Card = Me.cards(0)
    61.  
    62.         Me.cards.RemoveAt(0)
    63.         Return nextCard
    64.     End Function
    65.  
    66.     'Returns the specified number of hands, each with the specified number of cards.
    67.     Public Function Deal(ByVal handCount As Integer, ByVal cardCount As Integer) As Hand()
    68.         Dim hands(handCount - 1) As Hand
    69.  
    70.         For i As Integer = 1 To cardCount Step 1
    71.             For Each hand As Hand In hands
    72.                 hand.Add(Me.GetNextCard)
    73.             Next hand
    74.         Next i
    75.  
    76.         Return hands
    77.     End Function
    78.  
    79. End Class
    Last edited by Hack; Feb 10th, 2006 at 06:45 AM. Reason: Add Programming Language To Thread Title

  2. #2
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: [VB.NET] Useful Types for Card Games

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

  3. #3
    New Member
    Join Date
    Mar 2006
    Posts
    2

    Cool Re: [VB.NET] Useful Types for Card Games

    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:
    1. Type card
    2.      Index As Integer
    3.      value As Integer
    4.      suit As String
    5.      picturefront As String
    6.      pictureback As String
    7.      position As Integer
    8.      shuffled As Boolean
    9.      
    10. End Type
    and then in a second module set up a few arrays of type card these are


    VB Code:
    1. Public deck(52) As card
    2. Public firstdeck(52) As card
    3. Public seconddeck(52) As card
    4. Public thirddeck(52) As card
    5. Public fourthdeck(52) As card
    6. Public fithdeck(52) As card
    7. Public sixthdeck(52) As card
    8. Public shoeshuff1(78) As card
    9. Public shoeshuff2(78) As card
    10. Public firsthalfshoe(156) As card
    11. Public secondhalfshoe(156) As card
    12. Public shoe(312) As card
    13. Public path As String
    14. Public item As Integer
    15. Public number1 As Integer
    16. Public number2 As Integer
    17. Public topdeck(26) As card
    18. Public bottomdeck(26) As card
    19. Public tempdeck(52) As card
    20. Public firstQuaterDeck(13) As card
    21. Public secondquaterdeck(13) As card
    22. Public thirdquaterdeck(13) As card
    23. Public fourthquaterdeck(13) As card

    The set up for the cards is simple but longwinded

    VB Code:
    1. Public Function setupcards()
    2. 'sets the actual deck in suit order used at the start form load
    3. Dim count, x As Integer
    4. 'hearts'
    5.  
    6. For x = 52 To 40 Step -1
    7.     deck(x).Index = (x - 39)
    8.     deck(x).suit = "hearts"
    9.         If (x - 39) < 10 Then
    10.            deck(x).value = (x - 39)
    11.         End If
    12.         If (x - 39) > 10 Then
    13.            deck(x).value = 10
    14.         End If
    15. Next x
    16.  
    17. 'clubs
    18.  
    19. For x = 39 To 27 Step -1
    20.     deck(x).Index = (x - 26)
    21.     deck(x).suit = "clubs"
    22.          If (x - 26) < 10 Then
    23.             deck(x).value = (x - 26)
    24.          End If
    25.          If (x - 26) > 10 Then
    26.             deck(x).value = (10)
    27.         End If
    28.  
    29. Next x
    30.  
    31. 'diamonds
    32.  
    33. For x = 26 To 14 Step -1
    34.     deck(x).Index = (x - 13)
    35.     deck(x).suit = "diamonds"
    36.         If (x - 13) < 10 Then
    37.            deck(x).value = (x - 13)
    38.         End If
    39.         If (x - 13) > 10 Then
    40.            deck(x).value = (10)
    41.         End If
    42. Next x
    43.  
    44. 'spades
    45.  
    46. For x = 13 To 1 Step -1
    47.    
    48.     If x < 10 Then
    49.        deck(x).value = x
    50.     End If
    51.     If x > 10 Then
    52.        deck(x).value = 10
    53.     End If
    54.     deck(x).Index = x
    55.     deck(x).suit = "spades"
    56. Next x
    57. For count = 1 To 52
    58.     deck(count).shuffled = False
    59. Next count
    60.  
    61.  
    62.  
    63. 'set back of cards
    64. For x = 1 To 52
    65.     deck(x).pictureback = path & "ims\cardback.jpg"
    66. Next x
    67. 'set front art work
    68. deck(1).picturefront = path & "ims\Aspades.jpg"
    69. deck(2).picturefront = path & "ims\2spades.jpg"
    70. deck(3).picturefront = path & "ims\3spades.jpg"
    71. deck(4).picturefront = path & "ims\4spades.jpg"
    72. deck(5).picturefront = path & "ims\5spades.jpg"
    73. deck(6).picturefront = path & "ims\6spades.jpg"
    74. deck(7).picturefront = path & "ims\7spades.jpg"
    75. deck(8).picturefront = path & "ims\8spades.jpg"
    76. deck(9).picturefront = path & "ims\9spades.jpg"
    77. deck(10).picturefront = path & "ims\10spades.jpg"
    78. deck(11).picturefront = path & "ims\Jspades.jpg"
    79. deck(12).picturefront = path & "ims\Qspades.jpg"
    80. deck(13).picturefront = path & "ims\Kspades.jpg"
    81. deck(14).picturefront = path & "ims\Adiamonds.jpg"
    82. deck(15).picturefront = path & "ims\2Diamonds.jpg"
    83. deck(16).picturefront = path & "ims\3diamonds.jpg"
    84. deck(17).picturefront = path & "ims\4diamonds.jpg"
    85. deck(18).picturefront = path & "ims\5diamonds.jpg"
    86. deck(19).picturefront = path & "ims\6diamonds.jpg"
    87. deck(20).picturefront = path & "ims\7diamonds.jpg"
    88. deck(21).picturefront = path & "ims\8diamonds.jpg"
    89. deck(22).picturefront = path & "ims\9diamonds.jpg"
    90. deck(23).picturefront = path & "ims\10diamonds.jpg"
    91. deck(24).picturefront = path & "ims\Jdiamonds.jpg"
    92. deck(25).picturefront = path & "ims\Qdiamonds.jpg"
    93. deck(26).picturefront = path & "ims\kdiamonds.jpg"
    94. deck(27).picturefront = path & "ims\Aclubs.jpg"
    95. deck(28).picturefront = path & "ims\2clubs.jpg"
    96. deck(29).picturefront = path & "ims\3clubs.jpg"
    97. deck(30).picturefront = path & "ims\4clubs.jpg"
    98. deck(31).picturefront = path & "ims\5clubs.jpg"
    99. deck(32).picturefront = path & "ims\6clubs.jpg"
    100. deck(33).picturefront = path & "ims\7clubs.jpg"
    101. deck(34).picturefront = path & "ims\8clubs.jpg"
    102. deck(35).picturefront = path & "ims\9clubs.jpg"
    103. deck(36).picturefront = path & "ims\10clubs.jpg"
    104. deck(37).picturefront = path & "ims\Jclubs.jpg"
    105. deck(38).picturefront = path & "ims\Qclubs.jpg"
    106. deck(39).picturefront = path & "ims\Kclubs.jpg"
    107. deck(40).picturefront = path & "ims\Ahearts.jpg"
    108. deck(41).picturefront = path & "ims\2hearts.jpg"
    109. deck(42).picturefront = path & "ims\3hearts.jpg"
    110. deck(43).picturefront = path & "ims\4hearts.jpg"
    111. deck(44).picturefront = path & "ims\5hearts.jpg"
    112. deck(45).picturefront = path & "ims\6hearts.jpg"
    113. deck(46).picturefront = path & "ims\7hearts.jpg"
    114. deck(47).picturefront = path & "ims\8hearts.jpg"
    115. deck(48).picturefront = path & "ims\9hearts.jpg"
    116. deck(49).picturefront = path & "ims\10hearts.jpg"
    117. deck(50).picturefront = path & "ims\Jhearts.jpg"
    118. deck(51).picturefront = path & "ims\Qhearts.jpg"
    119. deck(52).picturefront = path & "ims\Khearts.jpg"
    120. 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:
    1. Public Function random()
    2. ' a simple random number gen
    3. Randomize
    4. number1 = 52 * Rnd
    5. Randomize
    6. number2 = 52 * Rnd
    7. checkrandomnumbers
    8. End Function
    and using those perform a selection sort:

    VB Code:
    1. Public Function ChimShuffle()
    2. 'sets up the cards for chim shuffle
    3. Dim count, x, pos As Integer
    4. 'chimmile shuffle
    5. 'set temp deck order to deck order
    6. For count = 1 To 52
    7.      tempdeck(count) = deck(count)
    8.      
    9. Next count
    10. 'perform chim shuffle
    11. For x = 1 To 52
    12.     random
    13.     checkrandomnumbers
    14.     shuffledeck
    15. Next x
    16.  
    17.  
    18. 'put shuffled deck back together in new order
    19. For count = 1 To 52
    20.     deck(count) = tempdeck(count)
    21.     tempdeck(count).shuffled = False
    22. Next count
    23. 'check deck for consistancy
    24. deckcheck
    25. value
    26. For x = 1 To 52
    27.     If deck(x).picturefront = "" Then
    28.        MsgBox ("a card is missaing at position " & x)
    29.     End If
    30. Next x
    31. End Function
    32.  
    33. Public Function shuffledeck()
    34. 'this is the actual chim shuffle it is called from within a fixed itteration loop
    35. 'this works like a slection sort card at position 1 is copied to the same position in the temp deck
    36. 'number 1 is then overwritten by number 2
    37. 'number 2 is then overwritten by number 1 the positions are therefore exchanged
    38.  
    39. 'set deck to hold first random number
    40. deck(number1) = tempdeck(number1)
    41.  
    42. 'set tempdeck to overwrite first number with second number
    43. tempdeck(number1) = tempdeck(number2)
    44.  
    45. 'set tepmdeck to overwrite second number with first
    46. tempdeck(number2) = deck(number1)
    47.  
    48.  
    49. 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.
    Last edited by phillt78; Mar 30th, 2006 at 06:17 AM.

  4. #4
    Junior Member iRoN_RoCK's Avatar
    Join Date
    Jul 2008
    Posts
    31

    Re: [VB.NET] Useful Types for Card Games

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

  5. #5

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: [VB.NET] Useful Types for Card Games

    Quote Originally Posted by iRoN_RoCK View Post
    How to sort cards in a hand? Can we use LINQ?
    vb.net Code:
    1. Dim cards = From c In myHand.Cast(Of Card)() _
    2.             Order By c.Suit, c.Face _
    3.             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.

  6. #6
    New Member
    Join Date
    Nov 2012
    Posts
    15

    Re: [VB.NET] Useful Types for Card Games

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

  7. #7

    Thread Starter
    Super Moderator jmcilhinney's Avatar
    Join Date
    May 2005
    Location
    Sydney, Australia
    Posts
    110,274

    Re: [VB.NET] Useful Types for Card Games

    Quote Originally Posted by somekidben View Post
    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.

  8. #8
    New Member
    Join Date
    Nov 2013
    Posts
    1

    Re: [VB.NET] Useful Types for Card Games

    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
    Last edited by cba01; Dec 6th, 2013 at 05:57 PM.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width