Question
In C++. Understand the Class and Problem We continue to work on the card game effort, now adding the source of all cards for the
In C++.
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 (init()) 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 private ints 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[ NUM_CARDS_PER_PACK ]
This is a private static Card array, masterPack[], 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 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 = 1) - a constructor that populates the arrays and assigns initial values to members.
bool init(int numPacks = 1) - re-populate cards[] with the standard 52 numPacks cards. We should not repopulate the static array, masterPack[], since that was done once, in the (first-invoked) constructor and never changes. 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[].
An accessor for the int, topCard (no mutator.)
Card inspectCard(int k) - Accessor for an individual card. Returns a card with errorFlag = true if k is bad.
Private Methods
static void allocateMasterPack() - this is a method that will be called by the constructor. However, it has to be done with a very simple twist: even if many Deck objects are constructed in a given program, this static method will not allow itself to be executed more than once. Since masterPack[] is a static, unchanging, entity, it need not be built every time a new Deck is instantiated. So this method needs to be able to ask itself, "Have I been here before?", and if the answer is "yes", it will immediately return without doing anything; it has already built masterPack[] in a previous invocation.
Recommended test of Class Deck
Declare a deck containing two packs of cards. Do not shuffle. Deal all the cards in a loop until the deck is empty (dealt directly to the display/screen, not to any Hand objects just yet). Display each card as it comes off the deck. Next, reset the deck by initializing it again (to the same two packs). Shuffle the deck this time, and re-deal to the screen in a loop again. Notice that the cards are now coming off in a random order.
Repeat this double deal, unshuffled, then shuffled, but this time using a single pack deck.
Example Test Run of Card Class
/* --------------------------------------------------------- K of Spades / Q of Spades / J of Spades / T of Spades / 9 of Spades / 8 of Spades / 7 of Spades / 6 of Spades / 5 of Spades / 4 of Spades / 3 of Spade s / 2 of Spades / A of Spades / K of Hearts / Q of Hearts / J of Hearts / T of Hearts / 9 of Hearts / 8 of Hearts / 7 of Hearts / 6 of Hearts / 5 of Hearts / 4 of Hearts / 3 of Hearts / 2 of Hearts / A of Hearts / K of Diamo nds / Q of Diamonds / J of Diamonds / T of Diamonds / 9 of Diamonds / 8 of Diamonds / 7 of Diamonds / 6 of Diamonds / 5 of Diamonds / 4 of Diamonds / 3 of Diamonds / 2 of Diamonds / A of Diamonds / K of Clubs / Q of Clubs / J of Clubs / T of Clubs / 9 of Clubs / 8 of Clubs / 7 of Clubs / 6 of Clubs / 5 of Clubs / 4 of Clubs / 3 of Clubs / 2 of Clubs / A of Clubs / K of Sp ades / Q of Spades / J of Spades / T of Spades / 9 of Spades / 8 of Spades / 7 of Spades / 6 of Spades / 5 of Spades / 4 of Spades / 3 of Spades / 2 of Spades / A of Spades / K of Hearts / Q of Hearts / J of Hearts / T of He arts / 9 of Hearts / 8 of Hearts / 7 of Hearts / 6 of Hearts / 5 of Hearts / 4 of Hearts / 3 of Hearts / 2 of Hearts / A of Hearts / K of Diamonds / Q of Diamonds / J of Diamonds / T of Diamonds / 9 of Diamonds / 8 of Diamond s / 7 of Diamonds / 6 of Diamonds / 5 of Diamonds / 4 of Diamonds / 3 of Di amonds / 2 of Diamonds / A of Diamonds / K of Clubs / Q of Clubs / J of Clu bs / T of Clubs / 9 of Clubs / 8 of Clubs / 7 of Clubs / 6 of Clubs / 5 of Clubs / 4 of Clubs / 3 of Clubs / 2 of Clubs / A of Clubs / T of Hearts / T of Diamonds / A of Clubs / Q of Spades / 3 of Clubs / 8 of Diamo nds / T of Clubs / 5 of Clubs / J of Clubs / 3 of Clubs / 3 of Diamonds / 3 of H earts / 3 of Spades / 2 of Hearts / Q of Clubs / 7 of Spades / A of Spades / Q o f Hearts / 6 of Spades / 5 of Clubs / K of Spades / 4 of Diamonds / J of Diamond s / Q of Spades / A of Diamonds / K of Clubs / 4 of Hearts / T of Spades / 9 of Hearts / 6 of Clubs / A of Hearts / 8 of Clubs / Q of Clubs / 9 of Clubs / T of Clubs / 6 of Hearts / 6 of Hearts / K of Diamonds / 2 of Clubs / 5 of Diamonds / Q of Hearts / Q of Diamonds / A of Diamonds / 5 of Diamonds / 2 of Spades / 7 o f Clubs / 9 of Diamonds / 6 of Diamonds / 7 of Spades / 4 of Clubs / 9 of Diamon ds / 9 of Spades / A of Spades / 7 of Hearts / 2 of Hearts / J of Clubs / 8 of C lubs / A of Clubs / 4 of Spades / K of Diamonds / T of Spades / T of Diamonds / Q of Diamonds / K of Hearts / 4 of Hearts / 6 of Diamonds / J of Hearts / 6 of S pades / 8 of Spades / 7 of Diamonds / 8 of Diamonds / 2 of Diamonds / 8 of Heart s / K of Hearts / 5 of Spades / K of Clubs / 4 of Spades / J of Hearts / 9 of Sp ades / 3 of Hearts / K of Spades / 4 of Clubs / 9 of Hearts / 6 of Clubs / 7 of Diamonds / A of Hearts / 2 of Spades / J of Spades / J of Spades / T of Hearts / 5 of Spades / 7 of Hearts / J of Diamonds / 5 of Hearts / 3 of Spades / 4 of Di amonds / 2 of Clubs / 8 of Hearts / 2 of Diamonds / 3 of Diamonds / 7 of Clubs / 8 of Spades / 5 of Hearts / 9 of Clubs / K of Spades / Q of Spades / J of Spades / T of Spades / 9 of Spades / 8 of Spades / 7 of Spades / 6 of Spades / 5 of Spades / 4 of Spades / 3 of Spade s / 2 of Spades / A of Spades / K of Hearts / Q of Hearts / J of Hearts / T of Hearts / 9 of Hearts / 8 of Hearts / 7 of Hearts / 6 of Hearts / 5 of Hearts / 4 of Hearts / 3 of Hearts / 2 of Hearts / A of Hearts / K of Diamo nds / Q of Diamonds / J of Diamonds / T of Diamonds / 9 of Diamonds / 8 of Diamonds / 7 of Diamonds / 6 of Diamonds / 5 of Diamonds / 4 of Diamonds / 3 of Diamonds / 2 of Diamonds / A of Diamonds / K of Clubs / Q of Clubs / J of Clubs / T of Clubs / 9 of Clubs / 8 of Clubs / 7 of Clubs / 6 of Clubs / 5 of Clubs / 4 of Clubs / 3 of Clubs / 2 of Clubs / A of Clubs / 2 of Hearts / 3 of Diamonds / 3 of Clubs / 4 of Diamonds / Q of Diamonds / 2 of Spades / 3 of Hearts / K of Hearts / 6 of Clubs / K of Spades / 4 of C lubs / 6 of Spades / 8 of Diamonds / 5 of Clubs / 9 of Diamonds / 5 of Diam onds / 3 of Spades / K of Clubs / J of Diamonds / A of Diamonds / Q of Spad es / T of Hearts / J of Hearts / A of Hearts / J of Clubs / 7 of Spades / 6 of Diamonds / 9 of Hearts / 8 of Clubs / K of Diamonds / 7 of Clubs / 7 o f Diamonds / 8 of Spades / 4 of Spades / 2 of Diamonds / 5 of Hearts / 9 of Spades / T of Spades / 2 of Clubs / 5 of Spades / 6 of Hearts / Q of Clubs / 4 of Hearts / A of Spades / 9 of Clubs / J of Spades / T of Clubs / A o f Clubs / Q of Hearts / T of Diamonds / 7 of Hearts / 8 of Hearts / Press any key to continue . . . --------------------------------------------------------- */
Phase 2: The Deck and Hand Classes
For your second test client, allow your Deck class to interact with your Hand class. Don't add anything to the two classes, but do everything in this phase from within your main() client.
Ask the user (interactively) to select the number of players (a number from 1 to 10). That's one question, one numeric answer, and no further user-interaction. Once you have a legal value, instantiate a single-pack Deck object without shuffling, deal a deck into that many Hand objects, dealing all cards until the deck is empty. Since the number of players chosen by the user may not divide evenly into 52, the number of cards dealt into the various hands might differ, but only by, at most, one. Display all the hands after the deal.
Reset the objects to their initial state, but this time shuffle the deck before a second deal (same # of players).
To be clear, dealing to hands means dealing a single card to each hand, until all hands have one card, then repeating to give all hands a second card, etc., until the cards are gone, and each hand has (nearly) the same number of cards. It does not mean dealing x cards to one hand, then x to the next hand, etc. This is very important.
You don't need any more classes than the ones we've already created, since there should not be that much to do in main().
Example of One of Possibly Many Test Runs of Deck + Card Classes
--------------- run #2 ---------------------------------- How many hands? (1 - 10, please): 6 Here are our hands, from unshuffled deck: Hand = ( K of Spades, 7 of Spades, A of Spades, 8 of Hearts, 2 of Hearts, 9 of Diamonds, 3 of Diamonds, T of Clubs, 4 of Clubs ) Hand = ( Q of Spades, 6 of Spades, K of Hearts, 7 of Hearts, A of Hearts, 8 of Diamonds, 2 of Diamonds, 9 of Clubs, 3 of Clubs ) Hand = ( J of Spades, 5 of Spades, Q of Hearts, 6 of Hearts, K of Diamonds, 7 o f Diamonds, A of Diamonds, 8 of Clubs, 2 of Clubs ) Hand = ( T of Spades, 4 of Spades, J of Hearts, 5 of Hearts, Q of Diamonds, 6 o f Diamonds, K of Clubs, 7 of Clubs, A of Clubs ) Hand = ( 9 of Spades, 3 of Spades, T of Hearts, 4 of Hearts, J of Diamonds, 5 o f Diamonds, Q of Clubs, 6 of Clubs ) Hand = ( 8 of Spades, 2 of Spades, 9 of Hearts, 3 of Hearts, T of Diamonds, 4 o f Diamonds, J of Clubs, 5 of Clubs ) Here are our hands, from SHUFFLED deck: Hand = ( 9 of Clubs, Q of Spades, 8 of Hearts, Q of Hearts, 3 of Hearts, 9 of S pades, K of Hearts, 8 of Spades, T of Diamonds ) Hand = ( T of Clubs, 5 of Spades, 3 of Clubs, A of Diamonds, K of Clubs, 5 of H earts, J of Diamonds, 7 of Diamonds, 2 of Spades ) Hand = ( 4 of Spades, 2 of Clubs, Q of Diamonds, 8 of Clubs, 4 of Hearts, 2 of Hearts, 3 of Spades, 2 of Diamonds, J of Clubs ) Hand = ( 9 of Hearts, 4 of Diamonds, T of Hearts, 4 of Clubs, 5 of Diamonds, 7 of Clubs, A of Clubs, 7 of Spades, A of Hearts ) Hand = ( J of Spades, 6 of Diamonds, 9 of Diamonds, 5 of Clubs, 6 of Spades, J of Hearts, 8 of Diamonds, K of Diamonds ) Hand = ( Q of Clubs, T of Spades, 3 of Diamonds, 6 of Hearts, 6 of Clubs, 7 of Hearts, K of Spades, A of Spades ) Press any key to continue . . . --------------------------------------------------------- */
For this part, you will be graded on how efficiently you put together these two classes. Use what you know about arrays, loops, the methods available in the Deck and Hand classes to give a clean, short and completely tested client that proves that your Deck can feed the number of Hands requested by the user. There is some amount of creativity and variability allowed in this part, and any two correct solutions will look very different. You can implement this in any way that interprets the instructions. Yet, I can and will deduct when I see basic programming concepts misused, deduction amounts commensurate with the type of infraction.
handsStep by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started