Card and Deck Prebuilt Classes
6
This snippet is two classes which represent a playing card and a deck of cards. I built these classes for completion of a final project in one of my VB.NET courses.
Public Class card
'Cory Jackson
'while not all properties are used, they provide a use for other games
'i have taken into consideration all properties of a card i could think of
'stores if red then true
Dim m_red As Boolean
Public ReadOnly Property red()
Get
Return m_red
End Get
End Property
'holds string for picture file name
Dim m_picture As String
Public ReadOnly Property picture()
Get
Return m_picture
End Get
End Property
'suit holds string value of suit
Dim m_suit As String
Public ReadOnly Property suit()
Get
Return m_suit
End Get
End Property
'number holds card value of card
Dim m_cardvalue As Integer
Public ReadOnly Property cardvalue()
Get
Return m_cardvalue
End Get
End Property
Dim m_cardname As String
Public ReadOnly Property cardname()
Get
Return m_cardname
End Get
End Property
'returns the string value of card
Dim m_facecard As Boolean
Public ReadOnly Property facecard()
Get
Return m_facecard
End Get
End Property
'creates a card, gets card number and suit from deckofcards class
Public Sub New(ByVal fromdecknumber As Integer, ByVal fromdecksuit As String)
Dim suits() As String = {"hearts", "diamonds", "clubs", "spades"}
m_cardvalue = fromdecknumber
m_suit = suits(fromdecksuit)
If (m_suit = "diamonds") Or (m_suit = "hearts") Then
m_red = True
End If
If ((fromdecknumber = 1) Or (fromdecknumber = 11) Or (fromdecknumber = 12) Or (fromdecknumber = 13)) Then
m_facecard = True
End If
Select Case fromdecknumber
Case 1
m_cardname = "Ace of " & m_suit
m_cardvalue = 1
m_picture = m_suit & "_ace.png"
Case Is <= 10
m_cardname = fromdecknumber & " of " & m_suit
m_cardvalue = fromdecknumber
m_picture = m_suit & "_" & fromdecknumber & ".png"
Case 11
m_cardname = "Jack of " & m_suit
m_cardvalue = 10
m_picture = m_suit & "_jack.png"
Case 12
m_cardname = "Queen of " & m_suit
m_cardvalue = 10
m_picture = m_suit & "_queen.png"
Case 13
m_cardname = "King of " & m_suit
m_cardvalue = 10
m_picture = m_suit & "_king.png"
End Select
End Sub
End Class
Public Class deckofcards
'Cory Jackson
'sets an array of 52 cards
Public m_deck(52) As card
'constructor makes 52 cards, or 1 complete deck of cards
'passes the card number and suit to the card class and fills the array
Public Sub New()
Dim temp1, temp2 As Integer
Dim suits() As String = {"Hearts", "Diamonds", "Clubs", "Spades"}
For temp1 = 0 To 3
For temp2 = 1 To 13
m_deck(temp1 * 13 + temp2) = New card(temp2, temp1)
Next
Next
End Sub
'swaps card and temp card
Private Sub swap(ByVal shuffletemp1 As Integer, ByVal shuffletemp2 As Integer)
Dim tempcard As card
tempcard = m_deck(shuffletemp1)
m_deck(shuffletemp1) = m_deck(shuffletemp2)
m_deck(shuffletemp2) = tempcard
End Sub
'shuffles cards randomly
Public Sub shuffledeck()
Dim index, shuffletemp1, shuffletemp2 As Integer
Dim randomnum As New Random
For shuffletemp1 = 1 To 2000
For shuffletemp2 = 1 To 52
index = randomnum.Next(1, 53)
swap(shuffletemp2, index)
Next
Next
End Sub
End Class
Comments
Sat. Sep. 9th, 2006 6:31 PM
bertheymans
bertheymans





