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 EnumVB 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 StructureVB 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 ClassVB 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




Reply With Quote