Question: Instructions Hand in only one program, please. Understand the Class and Problem We continue to work on the card game effort, now adding the source

Instructions
Hand in only one program, please.
Understand the Class and Problem
We continue to work on the card game effort, now adding the source of all cards for the various players, the Deck.
Deck: A class that represents the source of the cards for dealing and, as the 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 Deck to the various Hands that it creates through the process called "dealing":
Let's deconstruct the meaning of this important class.
Deck: A Deck object is the source of all cards. It's where the dealer gets cards to deal, and if a player takes an individual card after the deal, he takes it from the Deck object. Naturally, the primary member here is an array of Card objects, much like Hand. We'll call this member cards[]. A deck normally consists of a single pack of cards: 52 cards (four suits of 13 values each). However, some games use two, three or more packs. If a card game requires two packs, then the deck will consist of two full 52-card packs: 104 cards. (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 will 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 will be 52,104,156,..., i.e., numPacks 52.
Clearly, we need an int like Hand's numCards, to keep track of how many cards are actually in the cards[] array. To this end, we'll use topCard (not numCards), since a deck typically removes and delivers cards to players from the top-of-the-deck, and this is a convenient 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 (numPacks, 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 player's hand), and a shuffle() to re-order the cards in a random fashion. Also, we'll need to restock the deck (initializePack()) to the original full condition in preparation for a fresh deal (we would certainly not want to re-instantiate a new deck when we have a perfectly good one available: garbage collection, done by us or by the operating system, is a resource we do not abuse).
Phase 1: The Deck Class
Private Static Class Constants
Define a private final int value like MAX_PACKS =6, NUM_CARDS_PER_PACK =52, and MAX_CARDS_PER_DECK = MAX_PACKS * NUM_CARDS_PER_PACK. Use them to their full benefit in the class code.
Private Static Member Data
Card[] masterPack
This is a private static Card array, masterPack[], containing exactly 52 card references, which point to 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 masterPack[]), we use that same instance whenever we need it as a source to copy in various places, notably during a re-initialization of the Deck object; it will always be in the masterPack[] array for us to copy.
Private Member Data
Card[] cards;
int topCard;
int numPacks;
Public Methods
Deck(int numPacks)- a constructor that populates the arrays and assigns initial values to members. Overload so that if no parameters are passed, one pack is assumed. This constructor can call a helper, allocateMasterPack()(see below), but that helper would only do something the very first time it gets called per program (no need to allocate a static array more than once per program, right?). It would then use another helper, initializePack(), to assign the master pack Cards to the various cards[] elements.
boolean initializePack(int numPacks)- re-populate cards[] with the standard 52 numPacks cards. (This also gives the client a chance to change the number of packs in the deck in preparation for a new game.) We should not repopulate the static array, masterPack[], since that was done once, in the (first-invoked) constructor and never changes. The elements of the cards[] array can reference the masterPack[] objects -- that's safe since we will never give the client any of those objects to modify (see dealCard() on this issue). If numPacks is out-of-range, return false without changing the object; else return true and make the change.
void shuffle()- mixes up the cards with the help of the standard random number generator.
Card dealCard()- returns and removes (effectively, not physically) the card in the top occupied position of cards[]. Here we have to return a copy of the card, not the actual reference to the object in the cards[] array, since that obj

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!