Results 1 to 8 of 8

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

Threaded View

  1. #1

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

    [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

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