rbutler Posted September 20, 2003 Posted September 20, 2003 I'm not sure if this is the right forum, but I'm trying to figure out how to deal out a set of 52 cards to two players, using classes. This is what I've got so far: Module Module1 ' 'declare an enum structure to hold the four types of cards 'available to loop through ' Enum Suite As Short Hearts = 3 Spades = 6 Diamonds = 4 Clubs = 5 End Enum Class Card Public m_nValue As Short 'value of cards Public m_nSuite As Suite 'declare instance for enum structure Public Overrides Function ToString() As String Return m_nValue & Chr(m_nSuite) End Function End Class Class Deck Dim m_nRandom As Random 'to sort cards randomly Public m_Cards As Collection 'data structure for cards Public m_Shuffled As Boolean 'to check to see if cards have been shuffled or not Sub New() 'new card deck function m_Cards = New Collection 'establish a m_Cards data structure m_Shuffled = False 'setting the initial value of cards shuffled to false FillDeck() 'calling my fill deck function everytime the deck is created Shuffle() 'calling the shuffle function everytime the deck is created End Sub Sub FillDeck() 'fill deck function m_Shuffled = False m_Cards = New Collection 'sets the cards to nothing, then create a new deck of cards Dim nLoopProcess As Short 'loop variable to count card types 1-13 Dim nSuits As Suite 'variable to count through all the available cards Dim nCardNew As Card 'variable to hold a new card in ' 'loop processing to looping through the available card types ' For nLoopProcess = 1 To 13 For nSuits = Suite.Hearts To Suite.Clubs nCardNew.m_nValue = nLoopProcess nCardNew.m_nSuite = nSuits m_Cards.Add(nCardNew) Next Next End Sub Sub Shuffle() 'the shuffle deck function ' 'declaring two random number variables to shuffle the card deck ' Dim nRandom1 As Short = m_nRandom.Next(0, m_Cards.Count) Dim nRandom2 As Short = m_nRandom.Next(0, m_Cards.Count - 1) ' 'declaring a card swap variable to remove the first random number, then add the card swap variable to the second card ' Dim nCardSwap As Card = m_Cards.Item(nRandom1) m_Cards.Remove(nRandom1) m_Cards.Add(nCardSwap, , nRandom2) ' 'use a for/next loop to go through the 52 possibilities of the card deck and re-sort ' For nRandom2 = 1 To 52 m_Cards.Remove(nRandom1) 'randomly selecting a card and removing it m_Cards.Add(nCardSwap, , nRandom2) 'randomly adding a card to the card swap variable, with the random 2 variable Next m_Shuffled = True 'setting the shuffled property to true in this case End Sub Sub DealDeck() 'dealing cards to players function End Sub End Class I'm thinking two more random numbers b/c player 1 might get more than player 2, so forth. I merely posted code to show you what I have, any tips/suggestions for dealing this deck would be appreciated. Quote
*Experts* Nerseus Posted September 22, 2003 *Experts* Posted September 22, 2003 I'm not sure if the following will help, but maybe check out this link for a Card Game type of demo: http://www.xtremedotnettalk.com/showthread.php?s=&threadid=49509 -Ner Quote "I want to stand as close to the edge as I can without going over. Out on the edge you see all the kinds of things you can't see from the center." - Kurt Vonnegut
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.