Resetting a class

Megalith

Well-known member
Joined
Aug 21, 2006
Messages
66
Programming Experience
10+
i'm developing a card game ive come a long way with it but have discovered a problem. In essence the cards are a class and instances of the class are loaded into a list by a class called deck the deck can be shuffled and cards dealt 1 at a time. my problem is how would you go about resetting the deck so that i once more have a full deck of cards. heres my card test application in a form1

VB.NET:
Imports cardgame.CardGameFramework
Public Class Form1
    Dim m_Deck As New Cards ' this class contains the card images
    Dim m_D As New Deck  ' this class contains shuffle new and deal methods

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Ace As Card ' instance of a card with various card properties
        Dim q As Integer
        Ace = m_D.Draw()  ' draw a card
        q = Ace.CardVal get its value from 1 to 52
        PictureBox1.Image = m_Deck.Card(q) 'retrieve the card for the value
        TextBox1.Text = Ace.ToString ' output the name of the card
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Destroy the old Deck Create new one
        Dim m_D As New Deck
        MakeDeck()
    End Sub

    Protected Sub MakeDeck()
    ' Routine to shuffle
        m_D.Shuffle()
    End Sub

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        MakeDeck()
    End Sub
End Class

the code in button 1 draws a card and that in button 2 is supposed to replace the deck with a new one and shuffle it however it shuffles the cards remaining in the deck and after a few deals you have an empty deck. i'm rusty on Object programming (vb5 then an absense of programming for 4+ years) and new to the .NET framework.
 
try this for button2:
m_Deck = New Card
m_D = New Deck

so the whole thing would be:
VB.NET:
Imports cardgame.CardGameFramework
Public Class Form1
    Private m_Deck As New Cards ' this class contains the card images
    Private m_D As New Deck  ' this class contains shuffle new and deal methods

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim Ace As Card ' instance of a card with various card properties
        Dim q As Integer
        Ace = m_D.Draw()  ' draw a card
        q = Ace.CardVal get its value from 1 to 52
        PictureBox1.Image = m_Deck.Card(q) 'retrieve the card for the value
        TextBox1.Text = Ace.ToString ' output the name of the card
    End Sub

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Destroy the old Deck Create new one
        m_D = New Deck
        m_Deck = New Card
        MakeDeck()
    End Sub

    Protected Sub MakeDeck()
    ' Routine to shuffle
        m_D.Shuffle()
    End Sub

    Public Sub New()

        ' This call is required by the Windows Form Designer.
        InitializeComponent()

        ' Add any initialization after the InitializeComponent() call.
        MakeDeck()
    End Sub
End Class
 
yeah spagetti lol i initially used private m_deck as Cards but it didnt work so i changed it to dim and it still didnt :-\ I always have problems naming variables when classes are involved, mostly the class name is the name i would like to use for the variable repredenting the class object. sometimes i have used the same name but it makes the code harder to read, private members help but yeah i know its out of context perhaps UserDeck would be a good option?
 
Well, it's rare that a class name will be a plural. A class represents a modeled object, and if you want several of them then they are either haivng another name (One Deck contains many Card) or you have an array:

Dim cards(0 to 9) as Card


Do strive for consistency though.. in your posted code i see the following all instance variables:

m_D
m_Deck
q
Ace

i would have recommended:
cardImages
dealerDeck
i
aceCard

VBN has a tendency to autogenerate names like TextBox1 and i've never liked the leading capital - i usually lowercase my variables, and have properties capitalised. It's born out of a java and C# convention that variable names start lowercase and class names start caps:

instanceVariable.methodCall()
instanceVariable.Property
Class.staticMethodCall()
Class.StaticProperty

Because VB is somewhat looser in its insistence about correct formatting (it allows the user to miss the brackets if they are empty) it can be harder to write code that is consistently formatted. Looking at any line of code, just from the casing and presence of brackets I can tell you whether it's an instance variable or static class, method call, public variable access or property in use. You can enforce this in your own mind, should you choose. It's a shame the VB compiler doesnt have an option to do so.
 
Thanks for your suggestions cjard, Ive completely rewritten the code, The classes Card and Cards are now a single class called Card the new Testing Form now looks like this

VB.NET:
Imports CardGame.CardGameFramework
Public Class TestForm
    Private userDeck As New Deck

    Private Sub Deal_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim card As Card
        card = userDeck.Draw()
        PictureBox1.Image = card.Card
        PictureBox2.Image = card.Back(My.Settings.CardBack)
        TextBox1.Text = card.ToString + ". " + userDeck.TotalCards.ToString + " Left"
    End Sub

    Private Sub Reset_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
        'Destroy the old Deck Create new one
        userDeck = New Deck
        userDeck.Shuffle()
        My.Settings.CardBack = Int(Rnd() * 11)
    End Sub

    Public Sub New()
        ' This call is required by the Windows Form Designer.
        InitializeComponent()
        ' Add any initialization after the InitializeComponent() call.
        userDeck.Shuffle()
    End Sub

End Class

It seems to make more sense now thanks
 
Looks good! I might have been tempted to call the Card.Card property (that is the image of the card's face, i presume) something like FaceImage and its corollorary as BackImage:

Me.PictureBox1.Image = card.FaceImage

It makes the code more self-documenting. If youre in an academic context, lecturers really like this kind of code. Out of it, other develllopers love it too - always try to write self-documenting code, as it reduces the need to add comments (which most developers are loathe to do except when working out a complex algorithm :) )

On the use of "Draw" in userDeck.Draw() - Draw is usually associated with changing pixel colours on screen to render an image. You may find "DealCard()" less ambiguous?
 
Back
Top