Home > Articles > Programming > Visual Basic

  • Print
  • + Share This

The clsDeck Class

Although Table 8.1 explains how the clsCard class works, you probably won't often need to access the clsCard class directly because it's handled mostly by the clsDeck class. The source code looks like Listing 8.2.

Listing 8.2 The clsDeck Class

1: '///////////////////////////////////////////////////////////
2: '// The clsDeck class
3: '///////////////////////////////////////////////////////////
4: 
5: Option Explicit
6: 
7: Private m_Hands(MAXHANDS) As hand
8: Private m_Cards(51) As clsCard
9: Private m_PositionInDeck As Integer
10: Private m_NumCardsInHand As Integer
11: 
12: '///////////////////////////////////////////////////////////
13: '// Class_Initialize
14: '///////////////////////////////////////////////////////////
15: Private Sub Class_Initialize()
16:  Dim i As Integer
17: 
18:  Randomize
19:  m_PositionInDeck = 0
20:  For i = 0 To 51
21:   Set m_Cards(i) = New clsCard
22:   m_Cards(i).value = i
23:  Next i
24:  Init_Hands
25: End Sub
26: 
27: '///////////////////////////////////////////////////////////
28: '// Shuffle
29: '//
30: '// This subroutine shuffles the deck, resets the
31: '// m_PositionInDeck marker, and initializes the m_Hands.
32: '///////////////////////////////////////////////////////////
33: Sub Shuffle()
34:  Dim CardNum As Integer
35:  Dim temp As clsCard
36:  Dim i As Integer
37: 
38:  m_PositionInDeck = 0
39:  For i = 0 To 51
40:   CardNum = Int(Rnd * 52)
41:   Set temp = m_Cards(i)
42:   Set m_Cards(i) = m_Cards(CardNum)
43:   Set m_Cards(CardNum) = temp
44:  Next i
45:  Init_Hands
46: End Sub
47: 
48: '///////////////////////////////////////////////////////////
49: '// Deal
50: '//
51: '// This subroutine deals num cards into the given hand,
52: '// displaying the cards on screen starting at x,y and
53: '// spacing them each one card width over plus the spacing
54: '// parameter. The parameter face controls whether the cards
55: '// are dealt face-up or face-down.
56: '///////////////////////////////////////////////////////////
57: Sub Deal(num As Integer, hand As Integer, x As Integer, _
58:   y As Integer, spacing As Integer, face As Integer)
59:  Dim pos As Integer
60:  Dim i As Integer
61: 
62:  For i = 0 To num - 1
63:   pos = m_Hands(hand).PositionInHand
64:   Set m_Hands(hand).cards(pos) = m_Cards(m_PositionInDeck)
65:   m_Cards(m_PositionInDeck).Display x, y, face
66:   m_PositionInDeck = m_PositionInDeck + 1
67:   If m_PositionInDeck > 51 Then m_PositionInDeck = 0
68:   m_Hands(hand).PositionInHand = _
69:     m_Hands(hand).PositionInHand + 1
70:   If m_Hands(hand).PositionInHand > 51 Then _
71:     m_Hands(hand).PositionInHand = 0
72:   x = x + 56 + spacing
73:  Next i
74: End Sub
75: 
76: '///////////////////////////////////////////////////////////
77: '// ShowHand
78: '//
79: '// This subroutine shows all the cards in the given hand
80: '// starting at the screen coordinates x,y and spaced
81: '// apart according to the spacing parameter. The cards
82: '// are displayed face-up or face-down depending on the
83: '// face parameter.
84: '///////////////////////////////////////////////////////////
85: Sub ShowHand(hand As Integer, x As Integer, y As Integer, _
86:   spacing As Integer, face As Integer)
87:  Dim num As Integer
88:  Dim i As Integer
89: 
90:  num = m_Hands(hand).PositionInHand
91:  For i = 0 To num - 1
92:   m_Hands(hand).cards(i).Display x, y, face
93:   x = x + 56 + spacing
94:  Next i
95: End Sub
96: 
97: '///////////////////////////////////////////////////////////
98: '// DealReplace
99: '//
100: '// This subroutine deals one card into the given hand,
101: '// replacing the card at the position pos. The parameter
102: '// face controls whether the card is displayed face-up
103: '// or face-down.
104: '///////////////////////////////////////////////////////////
105: Sub DealReplace(hand As Integer, pos As Integer, _
106:   face As Integer)
107:  Dim x As Integer
108:  Dim y As Integer
109: 
110:  x = m_Hands(hand).cards(pos).xPosition
111:  y = m_Hands(hand).cards(pos).yPosition
112:  Set m_Hands(hand).cards(pos) = m_Cards(m_PositionInDeck)
113:  m_Cards(m_PositionInDeck).Display x, y, face
114:  m_PositionInDeck = m_PositionInDeck + 1
115:  If m_PositionInDeck > 51 Then m_PositionInDeck = 0
116: End Sub
117: 
118: '///////////////////////////////////////////////////////////
119: '// Discard
120: '//
121: '// This subroutine removes the card at position pos from
122: '// the hand specified bt the hand parameter.
123: '///////////////////////////////////////////////////////////
124: Sub Discard(hand As Integer, pos As Integer)
125:  Dim x As Integer
126:  Dim y As Integer
127:  Dim DiscardPos As Integer
128:  Dim i As Integer
129: 
130:  DiscardPos = m_Hands(MAXHANDS - 1).PositionInHand
131:  m_Hands(MAXHANDS - 1).PositionInHand = _
132:    m_Hands(MAXHANDS - 1).PositionInHand + 1
133:  Set m_Hands(MAXHANDS - 1).cards(DiscardPos) = _
134:    m_Hands(hand).cards(pos)
135:  For i = pos To m_Hands(hand).PositionInHand - 1
136:   Set m_Hands(hand).cards(i) = m_Hands(hand).cards(i + 1)
137:  Next i
138:  m_Hands(hand).PositionInHand = m_Hands(hand).PositionInHand - 1
139: End Sub
140: 
141: '///////////////////////////////////////////////////////////
142: '// EraseCard
143: '//
144: '// This subroutine erases the card at position pos in
145: '// the hand specified by the hand parameter.
146: '///////////////////////////////////////////////////////////
147: Sub EraseCard(HandNum As Integer, pos As Integer)
148:  m_Hands(HandNum).cards(pos).EraseCard
149: End Sub
150: 
151: '///////////////////////////////////////////////////////////
152: '// ShowHandCard
153: '//
154: '// This subroutine displays the card at position pos in
155: '// the given hand. The parameter face controls whether
156: '// the card is displayed face-up or face-down.
157: '///////////////////////////////////////////////////////////
158: Sub ShowHandCard(hand As Integer, pos As Integer, _
159:   face As Integer)
160:  If face = FaceUp Then
161:   m_Hands(hand).cards(pos).ShowFace
162:  Else
163:   m_Hands(hand).cards(pos).ShowBack
164:  End If
165: End Sub
166: 
167: '///////////////////////////////////////////////////////////
168: '// MoveHandCard
169: '//
170: '// This subroutine moves the card at position pos in the
171: '// given hand to new screen coordinates. The parameter
172: '// face controls whether the card is displayed face-up or
173: '// face-down.
174: '///////////////////////////////////////////////////////////
175: Sub MoveHandCard(hand As Integer, pos As Integer, _
176:   x As Integer, y As Integer, face As Integer)
177:  m_Hands(hand).cards(pos).Display x, y, face
178: End Sub
179: 
180: '///////////////////////////////////////////////////////////
181: '// GetCardValue
182: '//
183: '// This function returns the value of the card at pos in
184: '// the given hand. The value is a number from 0 to 51.
185: '///////////////////////////////////////////////////////////
186: Function GetCardValue(hand As Integer, _
187:   pos As Integer) As Integer
188:  GetCardValue = m_Hands(hand).cards(pos).value
189: End Function
190: 
191: '///////////////////////////////////////////////////////////
192: '// Init_Hands
193: '//
194: '// This subroutine initializes the m_Hands property,
195: '// setting all cards in m_Hands to Nothing and setting
196: '// each hand's PositionInHand property to zero.
197: '///////////////////////////////////////////////////////////
198: Sub Init_Hands()
199:  Dim i As Integer
200:  Dim j As Integer
201: 
202:  For i = 0 To MAXHANDS - 1
203:   m_Hands(i).PositionInHand = 0
204:   For j = 0 To 51
205:    Set m_Hands(i).cards(j) = Nothing
206:   Next j
207:  Next i
208: End Sub
209: 
210: '///////////////////////////////////////////////////////////
211: '// Restore
212: '//
213: '// This subroutine sets the position in the deck back to
214: '// the beginning of the deck.
215: '///////////////////////////////////////////////////////////
216: Sub Restore()
217:  m_PositionInDeck = 0
218: End Sub
219: 
220: '///////////////////////////////////////////////////////////
221: '// Get NumCardsInHand
222: '///////////////////////////////////////////////////////////
223: Property Get NumCardsInHand(hand As Integer) As Integer
224:  If hand < 0 Or hand > MAXHANDS - 1 Then Err.Raise 9
225:  NumCardsInHand = m_Hands(hand).PositionInHand
226: End Property

Analysis - In the class's Class_Initialize method, Line 18 ensures that the class is capable of producing a different shuffled deck every time it's used. Line 19 initializes the m_PositionInDeck property, and Lines 20 through 23 create 52 objects of the clsCard class. The Init_Hands call (Line 24) empties all the card hands.

Analysis - The Shuffle method shuffles the deck (Lines 39 to 44) by swapping each card with another randomly selected card. The method also resets the m_PositionInDeck marker (Line 38) to 0, which makes the first card in the deck the next card to be drawn. Finally, the method empties all hands (Line 45).

ANALYSIS - The Deal method deals the requested number of cards (specified by the num parameter) into the hand specified by the hand parameter. The cards are displayed on the screen starting at x,y, with each card spaced one card width over plus the spacing parameter. The parameter face controls whether the cards are dealt face-up or face-down. Line 62 begins a For statement that iterates once for each card to display. Inside the For loop, Line 63 gets the position in the hand to which the current card should be dealt, and Line 64 sets the card in that hand position to the next card in the deck. Line 65 calls the card object's Display method to paint the card on the screen, and Lines 66 and 67 move the current position in the deck to the next card. Lines 68 to 71 move forward the location for the next card in the hand. Finally, Line 72 adds the spacing parameter to the horizontal position for the next card to display.

Analysis - This ShowHand method shows all the cards in the given hand, starting at the screen coordinates x,y and spaced apart according to the spacing parameter. The cards are displayed face-up or face-down depending on the face parameter. Line 90 gets the number of cards in the hand, and Lines 91 to 94 call each card object's Display method to show the card on the screen. Notice how the Display method is a member of the currently indexed element of the cards() array, which is itself a member of the m_Hands() array.

Analysis - The DealReplace method deals one card into the given hand, replacing the card at the position pos. The parameter face controls whether the card is displayed face-up or face-down. Lines 110 and 111 get the coordinates of the card to replace, and Line 112 places the next card in the deck into the given position in the hand. Line 113 then displays the new card on the screen. Finally, Lines 114 and 115 move the position in the deck to the next card.

Analysis - The Discard method removes the card at position pos from the hand specified by the hand parameter. Line 130 gets the current position in the discard hand, and Lines 131 and 132 move the discard hand's current position forward one card. Line 133 removes the discarded card from the hand and places it into the discard hand, and then Lines 135 to 137 move the cards in the hand back one position in order to fill in the position where the discarded card used to be. Finally, Line 138 updates the position in the hand, setting it back one.

Analysis - The EraseCard method erases the card at position pos in the hand specified by the hand parameter. Calling the card object's EraseCard method is all that's required to erase the card from the screen.

Analysis - The ShowHandCard method displays the card at position pos in the given hand. The parameter face controls whether the card is displayed face-up or face-down. Lines 160 and 161 display the card's face if the face parameter is FaceUp, and Lines 162 and 163 display the card face-down if the face parameter is FaceDown.

Analysis - The MoveHandCard method moves the card at position pos in the given hand to new screen coordinates. The parameter face controls whether the card is displayed face-up or face-down. Line 177 calls the card object's Display method to display the card in its new position.

Analysis - The GetCardValue function returns the value of the card at pos in the given hand. The value is a number from 0 to 51 and is obtained from the card object's value property in Line 188.

Analysis - The Init_Hands method initializes the m_Hands property, setting all cards in m_Hands to Nothing (Lines 204 to 206) and setting each hand's PositionInHand property to zero (Line 203).

Analysis - The Restore method sets the position in the deck back to the beginning of the deck. It does this in Line 217 by setting m_PositionInDeck to 0. This method is handy when you want to reuse the same deck of cards.

In the clsDeck class, the variable m_Cards is a 52-element array of clsCard objects. These objects make up the deck of cards. The integer m_PositionInDeck keeps track of the next card to be dealt. That is, at the beginning of a program, m_PositionInDeck is 0, indicating that the first card in the deck will be dealt next. Each time a card is dealt, m_PositionInDeck increments. When m_PositionInDeck equals 51, there's only one card left to deal in the deck. To avoid array-indexing errors, if your program tries to deal more than 52 cards before reshuffling the deck, m_PositionInDeck starts back at 0 and goes through the deck again.

The variable m_Hands is an array of hand objects, which is a user-defined data type (defined in a module called Cards.bas). The hand data type is as follows:

Public Type hand
 PositionInHand As Integer
 cards(51) As clsCard
End Type

As you can see, the members of hand are similar to two members of the clsDeck class. The integer PositionInHand keeps track of the position in the hand where the next card will be dealt. The array cards holds the clsCard objects that make up the hand.

Although you cannot anticipate all the different ways that you might need to manipulate a deck of cards, the clsDeck class includes 10 methods that you can call in your programs. These methods, which are listed in Table 8.2, enable you to program many card games without adding anything to the class. Study this table now so that you understand how to use the clsDeck class.

Table 8.2 Methods of the clsDeck Class

Member Function

Description

Shuffle

Shuffles the deck and resets the m_PositionInDeck marker. It also calls the private member function Init_Hands to initialize all eight hands that the clsDeck class handles.

Deal(num As Integer,

Deals num cards into the hand specified by the hand parameter and hand As Integer, x As displays the cards onscreen, starting at the coordinates x and y and Integer, y As Integer, spacing the cards each one card-width over plus the spacing para-spacing As Integer, meter. The parameter face must have the value FaceUp or FaceDown,face As Integer) which controls whether the cards are dealt face-up or face-down.

ShowHand(hand As

Shows all the cards in the hand specified by hand, starting at the Integer, x As Integer, screen coordinates x and y and spaced apart according to the spacingy As Integer, spacing parameter. The cards are displayed face-up or face-down depending As Integer, face As on the face parameter with a value that must be either FaceUp or Integer) FaceDown.

DealReplace(hand As

Deals one card into the given hand, replacing the card at the position Integer, pos As pos. The parameter face, which must be the value FaceUp or FaceDown Integer, face As controls whether the cards are displayed face-up or face-down.Integer)

EraseCard(HandNum As

Erases the card at position pos in the hand specified by the HandNum Integer, pos As parameter.Integer)

Discard(hand As

Removes the card at position pos from hands(hand), placing the card Integer, pos As into hands(7), which is the discard pile.Integer)

Sub ShowHandCard(hand

Displays the card at position pos in the given hand. The parameter As Integer, pos As face, which must have the value FaceUp or FaceDown, controls Integer, face As whether the cards are displayed face-up or face-down.Integer)

MoveHandCard(hand As

Moves the card at position pos in the given hand to the new screenInteger, pos As coordinates, x and y. The parameter face, which must have the value Integer, x As Integer, FaceUp or FaceDown, controls whether the card is displayed face-up y As Integer, face As or face-down.Integer)

GetCardValue(hand As

Returns the value of the card at the position pos in the given hand. Integer, pos As The value is a number from 0 to 51.Integer)

Restore

Sets the m_PositionInDeck data member back to 0, restoring the deck to the state it was in before the program dealt the first card.


  • + Share This
  • 🔖 Save To Your Account

Related Resources

There are currently no related titles. Please check back later.