19.12.2012 Views

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

Computer Programming Concepts and Visual Basic David I. Schneider

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Next i<br />

End Sub<br />

Public Function ReadCard(cardNum As Integer) As String<br />

ReadCard = m_deck(cardNum).IdentifyCard<br />

End Function<br />

Private Sub Swap(ByVal i As Integer, ByVal j As Integer)<br />

‘Swap the ith <strong>and</strong> jth card in the deck<br />

Dim TempCard As New CCard<br />

TempCard.Denomination = m_deck(i).Denomination<br />

TempCard.Suit = m_deck(i).Suit<br />

m_deck(i).Denomination =<br />

m_deck(j).Denomination<br />

m_deck(i).Suit = m_deck(j).Suit<br />

m_deck(j).Denomination = TempCard.Denomination<br />

m_deck(j).Suit = TempCard.Suit<br />

End Sub<br />

Public Sub ShuffleDeck()<br />

‘Do 200 passes through the deck. On each pass<br />

‘swap each card with a r<strong>and</strong>omly selected card.<br />

Dim index As Integer, i As Integer, k As Integer<br />

R<strong>and</strong>omize ‘Initialize r<strong>and</strong>om number generator<br />

For i = 1 To 200<br />

For k = 1 To 52<br />

index = Int((52 * Rnd) + 1)<br />

Call Swap(k, index)<br />

Next k<br />

RaiseEvent Shuffling(i, 200)<br />

Next i<br />

End Sub<br />

‘Form Code<br />

Public WithEvents cards As CDeckOfCards<br />

Private Sub Form_Load()<br />

Set cards = New CDeckOfCards<br />

End Sub<br />

Private Sub cmdShuffle_Click()<br />

Call cards.ShuffleDeck<br />

End Sub<br />

Private Sub cmdDeal_Click()<br />

Dim str As String<br />

Dim i As Integer<br />

picH<strong>and</strong>.Cls<br />

For i = 1 To 5<br />

str = cards.ReadCard(i)<br />

picH<strong>and</strong>.Print str<br />

Next i<br />

End Sub<br />

Class Relationships 379

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!