1

I was wondering if you could help me understand how to create a Card object (with a value and suit) and use 52 of those cards to make an object called deck.

I have created my card class how do I initialize every card inside the deck class? Should I do it one by one? How do I link all those cards to one deck.

Thanks

user3363744
  • 167
  • 1
  • 9

3 Answers3

3

As it happen I did read your previous question earlier today.

First, create a suit enum.

Public Enum Suit As Integer
    Hearts = 1
    Diamonds = 2
    Clovers = 3
    Spades = 4
End Enum

Then create the card class. Notice that the properties are read only as a card never changes its value. (Maybe not true if you're a magician)

Public Class Card

    Public Sub New(suit As Suit, value As Integer)
        Me.m_suit = suit
        Me.m_value = value
    End Sub

    Public ReadOnly Property Suit() As Suit
        Get
            Return Me.m_suit
        End Get
    End Property

    Public ReadOnly Property Value() As Integer
        Get
            Return Me.m_value
        End Get
    End Property

    Private m_suit As Suit
    Private m_value As Integer

End Class

Finally, create the deck class and populate 52 cards.

Public Class Deck

    Public Sub New()
        Dim cards = New Card(52 - 1) {}
        Dim num As Integer = 0
        For s As Integer = 1 To 4
            For v As Integer = 1 To 13
                cards(num) = New Card(CType(s, Suit), v)
                num += 1
            Next
        Next
        Me.m_cards = New Collections.ObjectModel.ReadOnlyCollection(Of Card)(cards)
    End Sub

    Public ReadOnly Property Cards() As Collections.ObjectModel.ReadOnlyCollection(Of Card)
        Get
            Return Me.m_cards
        End Get
    End Property

    Private ReadOnly m_cards As Collections.ObjectModel.ReadOnlyCollection(Of Card)

End Class
Bjørn-Roger Kringsjå
  • 9,849
  • 6
  • 36
  • 64
  • Question, I am very new to VB, where do you create your enum portion is it within the class or within the main program? – user3363744 Feb 28 '14 at 18:19
  • @user3363744 For each item (an enum, a class, .. or whatsoever) just right-click the project in the solutions pane , go to `Add` > `New`, select `Class` and type the item name. Eg. `Suit.vb`. Then remove the code created by VS and replace it with `Public Enum Suit As Integer` and so on. – Bjørn-Roger Kringsjå Feb 28 '14 at 18:28
1

You need two Enumerations and two Classes:

  • Enumerations
    1. CardFaceValue - with values ranging from Ace-10 (inclusive), Jack, Queen, King.
    2. CardFaceType - with values Hearts, Spades, Clubs, Diamonds
  • Classes
    1. Deck - Has one property to contain the collection of all cards
      1. Cards - of Type Array of Cards, sized 52.
    2. Card - Has two properties
      1. CardFaceValue
      2. CardFaceType

In the constructor of the Deck class run a loop within a loop. The outer loop will run for 4 times for each of the CardFaceType enumeration, and the inner loop will run for 13 times for cards 1-10, J, Q, K.

With these loops iterate through the enumeration values and add cards to your Deck.

dhrumilap
  • 142
  • 1
  • 17
0

This is just a quick draft of what I envision

You'll need the card class first.

Public Class Card

    Private cSuit As String
    Private cValue As Integer

    Public Property suit() As String
        Get
            Return cSuit
        End Get
        Set(ByVal value As String)
            cSuit = value
        End Set
    End Property

    Public Property value() As Integer
        Get
            Return cValue
        End Get
        Set(ByVal value As Integer)
            value = cValue
        End Set
    End Property

    Public Sub New(ByVal TheSuit As String, ByVal TheValue As Integer)

        cSuit = TheSuit
        cValue = TheValue

    End Sub

Then you can make a new object for each card and add it to the deck collection.

Dim Deck As New List(Of Card)
        Dim Suit As String = "Spade"
        Dim Value As Integer = 11

        Dim AceOfSpades As New Card(Suit, Value)

        Deck.Add(AceOfSpades)
Ccorock
  • 892
  • 12
  • 37
  • But then the Deck might be incomplete, have duplicates and/or over 52 cards. – Crono Feb 28 '14 at 17:44
  • What if you want an incomplete deck or more than one deck of cards to increase the odds? Many games play with more than one deck. I think a basic adaptable solution for a vague question is fair. Even further, what if he isn't playing with a standard deck. What if its Yugio or Pokemon or something. – Ccorock Feb 28 '14 at 17:45
  • Why would the OP specifically ask for a Deck class if he just wanted a collection? Also he specifically asked for the card to be linked to the deck. In your code the card could very well be in multiple decks. – Crono Feb 28 '14 at 17:49
  • If you have one collection, that's only one deck. – Ccorock Feb 28 '14 at 17:52
  • This was the approach I was taking. Question, wouldnt you also need to add a method .Add? – user3363744 Feb 28 '14 at 18:17
  • Negative, the .Add method is already included in the List(of T) class http://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx and so are many other methods for finding stored objects and manipulating them. – Ccorock Feb 28 '14 at 18:25