This is C++
Understand the Class and Problem We continue to work on the card game effort, now adding the source of allcards for the various players, the A class that represents the source of the cards for dealing and, asthe game progresses, the place from which players can receive new cards (say, as they pick cards "from the deck or when future hands are to be dealt from the same deck). Recall this picture, which relates the to the various that it creates through the process called "dealing" hands Let's deconstruct the meaning of this important class. object is the source of all cards, It's where the dealer gets cards to deal, and a player takes an individual card after the Deck: A deal, he takes it from the object. Naturally, the primary member here is an array of Card objects, much like Hand. We'll call this member cards[J A deck normally consists of a single pack of cards: 52 cards (four of 13 each). However, some games If a card game requires two packs, then the deck will consist of two full 52-card packs: 104 cards two, three or more packs. (Many games throw away some cards before beginning. For example Pinochle wants all cards with values 8-and-below to be taken out of the deck, but we wil not trouble ourselves with this complexity. A newly instantiated deck will have a multiple of 52 cards and will contain all the standard cards, so the number of cards in a newly instantiated deck wi be 52, 104, 156, num Packs Clearly, we need an int like Hand's numCards, to keep track of how many cards are actually in the cardsU array. To this end, we'll use to card (not numCards since a deck typically removes and delivers cards to players from the top-of-the-deck, and this is a variable to use for the number of cards as well as the position of the top of the deck. There are a few other useful members for example). In addition to the the usual constructors and accessors, we'll want a dealCard() to return and remove the card at the top of the deck (which may be received by a client and added to some players hand), and a shuffle() to re-order the cards in a random fashion. Also, we'l need to restock the deck (init() to the original full condition in preparation for a fresh deal (we would certainly not want to a new deck when we have a perfectly good one available garbage collection, done by us or by the operating system, resource we do not abuse). is a Phase 1 The Deck Class Private Static Class Constants Define private like MAX PACKS NUM. CARDS PER PACK 52, and MAX CARDS PERLDEC MAX PACKS NUM-CARDS PER PACK Use them to their full benefit in the class code. Private Static Member Data Card masterPackt NUMLCARDS PER PACK This is a private static Card sterPackE1 containing exactly 52 card objects, which includes all the standard cards. It will enable us to avoid capriciously and repeatedly declaring the same 52 cards which are needed as the game proceeds. In other words, once we have, say, a (6, spades) Card constructed and stored (inside this masterPackIJD, we use that same instance whenever we need it as a source to copy in various places, notably during a of the object: it will always be in the masterPack[] array for us to copy. Private Member Data Public Methods Understand the Class and Problem We continue to work on the card game effort, now adding the source of allcards for the various players, the A class that represents the source of the cards for dealing and, asthe game progresses, the place from which players can receive new cards (say, as they pick cards "from the deck or when future hands are to be dealt from the same deck). Recall this picture, which relates the to the various that it creates through the process called "dealing" hands Let's deconstruct the meaning of this important class. object is the source of all cards, It's where the dealer gets cards to deal, and a player takes an individual card after the Deck: A deal, he takes it from the object. Naturally, the primary member here is an array of Card objects, much like Hand. We'll call this member cards[J A deck normally consists of a single pack of cards: 52 cards (four of 13 each). However, some games If a card game requires two packs, then the deck will consist of two full 52-card packs: 104 cards two, three or more packs. (Many games throw away some cards before beginning. For example Pinochle wants all cards with values 8-and-below to be taken out of the deck, but we wil not trouble ourselves with this complexity. A newly instantiated deck will have a multiple of 52 cards and will contain all the standard cards, so the number of cards in a newly instantiated deck wi be 52, 104, 156, num Packs Clearly, we need an int like Hand's numCards, to keep track of how many cards are actually in the cardsU array. To this end, we'll use to card (not numCards since a deck typically removes and delivers cards to players from the top-of-the-deck, and this is a variable to use for the number of cards as well as the position of the top of the deck. There are a few other useful members for example). In addition to the the usual constructors and accessors, we'll want a dealCard() to return and remove the card at the top of the deck (which may be received by a client and added to some players hand), and a shuffle() to re-order the cards in a random fashion. Also, we'l need to restock the deck (init() to the original full condition in preparation for a fresh deal (we would certainly not want to a new deck when we have a perfectly good one available garbage collection, done by us or by the operating system, resource we do not abuse). is a Phase 1 The Deck Class Private Static Class Constants Define private like MAX PACKS NUM. CARDS PER PACK 52, and MAX CARDS PERLDEC MAX PACKS NUM-CARDS PER PACK Use them to their full benefit in the class code. Private Static Member Data Card masterPackt NUMLCARDS PER PACK This is a private static Card sterPackE1 containing exactly 52 card objects, which includes all the standard cards. It will enable us to avoid capriciously and repeatedly declaring the same 52 cards which are needed as the game proceeds. In other words, once we have, say, a (6, spades) Card constructed and stored (inside this masterPackIJD, we use that same instance whenever we need it as a source to copy in various places, notably during a of the object: it will always be in the masterPack[] array for us to copy. Private Member Data Public Methods