Question: CARD.CPP #include #include card.h /** * */ Card::Card() :suit(), value( Joker ) { } /** * */ Card::Card( std::string suit, std::string value ){ this->suit =

CARD.CPP #include  #include "card.h" /** * */ Card::Card() :suit(), value( "Joker" ) { } /** * */ Card::Card( std::string suit, std::string value ){ this->suit = suit; this->value = value; } /** * */ void Card::display( std::ostream &outs ) const{ outs << std::right << std::setw(3) << this->value << " of " << std::left << std::setw(8) << this->suit; } 

CARD.H

#ifndef CARD_H_INCLUDED #define CARD_H_INCLUDED #include  #include  /** * One playing card */ class Card{ private: std::string suit; ///< One of Diamonds, Hearts, Clubs, or Spades std::string value; ///< Face value of the card (one of Ace, 2-10, Jack, Queen, or King) public: /** * Construct a Joker Card */ Card(); /** * Construct a specified card * * @param suit one of Hearts, Diamonds, Spades, or Clubs * @param value one of J, Q, K, A, or 2-10 */ Card( std::string suit, std::string value ); /** * Retrieve the suit */ std::string getSuit() const; /** * Change the suit */ void setSuit( std::string s ); /** * Retrieve the face value */ std::string getValue() const; /** * Change the face value */ void setValue( std::string v ); /** * Print the Card */ void display( std::ostream &outs ) const; }; /** * Overloaded Card Stream Insertion Operator (Output Operator) */ inline std::ostream& operator<<( std::ostream &outs, const Card &prt ){ prt.display( outs ); return outs; } /** * */ inline std::string Card::getSuit() const{ return this->suit; } /** * */ inline void Card::setSuit( std::string s ){ this->suit = s; } /** * */ inline std::string Card::getValue() const{ return this->value; } /** * */ inline void Card::setValue( std::string v ){ this->value = v; } #endif 
DECK.CPP #include  #include "deck.h" const int Deck::MAX_CARDS = 52; const int Deck::NUM_SUITS = 4; const std::string Deck::SUITS[] = { "Hearts", "Diamonds", "Clubs", "Spades" }; /** * */ Deck::Deck(){ // Create a standard cards of 52 cards cards = new Card[ MAX_CARDS ]; current_size = 52; // start with a full deck // Create each of the suits --i.e., Hearts, Diamonds, Clubs, Spades for( int i = 0; i < NUM_SUITS; i++ ){ int start_index = i*13; // starting index within the array for the suit //Create the Ace cards[ start_index ] = Card( SUITS[i], "A" ); // Create cards 2-10 for( int j = 2; j <= 10; j++ ){ cards[ start_index + j - 1 ] = Card( SUITS[i], convertIntToStr(j) ); } //Create the Jack cards[ start_index + 10 ] = Card( SUITS[i], "J" ); //Create the Queen cards[ start_index + 11 ] = Card( SUITS[i], "Q" ); //Create the King cards[ start_index + 12 ] = Card( SUITS[i], "K" ); } } /** * */ Card Deck::remove( int to_remove ){ Card selected = cards[ to_remove ]; // decrement current_size (as one card has been removed) current_size--; // Shift all cards back one slot (index) for( int i = to_remove; i < current_size; i++ ){ cards[ i ] = cards[ i + 1 ]; } // return the selected card return selected; } /** * */ void Deck::display( std::ostream &outs ) const{ /* * Do you remember this loop? */ for( int i = 0; i < current_size; i++ ){ outs << cards[i] << " "; } } 

DECK.H

#ifndef DECK_H_INCLUDED #define DECK_H_INCLUDED #include  #include "utilities.h" #include "card.h" /** * */ class Deck{ private: static const std::string SUITS[]; ///< Suits in a deck of playing cards static const int MAX_CARDS; ///< Maximum size of a deck static const int NUM_SUITS; ///< Number of suits in a deck Card *cards; ///< Playing cards that compose the deck int current_size; ///< Number of cards currently in the deck public: /** * Create a standard Deck of 52 playing cards */ Deck(); /** * Remove a Card from the deck. This simulates the player * selecting a card * * @param to_remove position from which to retrieve a card * @return Card located at position *to_remove* * * @pre (0 <= to_remove < current_size) && current_size > 0 * @post cards[ to_remove ] has been removed from the deck */ Card remove( int to_remove ); /** * Return the current number of cards in the deck */ int size() const; /** * Print the card deck */ void display( std::ostream &outs ) const; }; /** * Overloaded Deck Stream Insertion Operator (Output Operator) */ inline std::ostream& operator<<( std::ostream &outs, const Deck &prt ){ prt.display( outs ); return outs; } /** * */ inline int Deck::size() const{ return this->current_size; } #endif 

HAND.CPP

#include "hand.h" /** * */ Hand::Hand(){ current_size = 0; size = 7; cards = new Card[ size ]; } /** * */ Hand::Hand( int s ){ current_size = 0; size = s; cards = new Card[ size ]; } /** * */ void Hand::add( Card to_add ){ } /** * */ bool Hand::isFull() const{ return current_size == size; } /** * */ void Hand::display( std::ostream &outs ) const{ /* * Do you remember this loop? */ } 

HAND.H

#ifndef HAND_H_INCLUDED #define HAND_H_INCLUDED #include  #include  #include "card.h" /** * Store the Cards held by a player */ class Hand{ private: Card *cards; ///< Array of cards int size; ///< Max size (# of cards) int current_size; ///< Current size (# of cards) public: /** * Default to a hand of 7 cards */ Hand(); /** * Allow a hand of any size * * @param s desired hand size * @pre s > 0 */ Hand( int s ); /** * Add a card to the player's hand * * @param to_add desired card */ void add( Card to_add ); /** * Return the current size * of the player's hand */ int getCurrentSize() const; /** * Return true once the Hand can no longer * accommodate additional cards */ bool isFull() const; /** * Print all cards contained in the Hand */ void display( std::ostream &outs ) const; }; /** * Overloaded Hand Stream Insertion Operator (Output Operator) */ inline std::ostream& operator<<( std::ostream &outs, const Hand &prt ){ prt.display( outs ); return outs; } /** * */ inline int Hand::getCurrentSize() const{ return this->current_size; } #endif 

UTILITIES.CPP

#include "utilities.h" /** * */ void printHorizontalLine( std::ostream& outs, char line_char, int width ){ outs << std::setfill( line_char ) << std::left << std::setw( width ) << line_char << " "; //reset outs fill outs.fill( ' ' ); } /** * */ void trim( std::string &str ){ if( str.empty() ){ return; } int first_nonspace = str.find_first_not_of(" \t"); int last_non_space = str.find_last_not_of(" \t"); str = str.substr( first_nonspace, last_non_space + 1 ); } 

UTILITIES.H

#ifndef UTILITIES_H_INCLUDED #define UTILITIES_H_INCLUDED #include  #include  #include  /* * The Utilities Module is reponsible for any functionalty that can * be generalized for use in any program. This includes printing a blank line * and printing a horizontal line. * * In future examples, I will discuss a more complete form of this * Module. This Module is a collection of commopnly used utility functions */ /** * Print a blank line - we will discuss the meaning of *inline* * in a future Review Session */ void println( std::ostream& outs=std::cout ); /** * Print a horizontal line * * @param outs output stream--e.g., cout or ofstream * @param line_char character used to construct the line * @param width length of the line * * @pre measure does not exceed three characters in length */ void printHorizontalLine( std::ostream& outs, char line_char, int width ); /** * */ inline void println( std::ostream &outs ){ outs << " "; } /** * Trim leading and trailing whitespace from a string. * * @param str string to prune * * @pre str is nonempty */ void trim( std::string &str ); /** * Convert an integer to a string */ inline std::string convertIntToStr( int to_convert ){ std::ostringstream magic; //Use this for magic magic << to_convert; //Use magic to convert the integer to a string return magic.str(); //Return some magic } #endif 

This assignment deals with a Deck of Cards. A player is dealt a number of cardswhich are held in his Hand. Note that Card, Deck, and Hand are emphasized. These are the three ADTs used within this assignment. Review all classes carefullyespecially the Card class.

1.1 Input

This program takes two types of input:

Command line - an integer specifying the desired hand sizei.e., maximum number of cards held by a player

Standard Input - a series of integers in response to interactive prompts. These specify the positions from which to retrieve cards from within the Deck.

1.2 Output

If the program is run with the command, ./cardShuffle 4, the following output should be generated:

Select in number in the range [1, 52]: 1 Select in number in the range [1, 51]: 8 Select in number in the range [1, 50]: 16 Select in number in the range [1, 49]: 32 Current Deck ---------------- 2 of Hearts 3 of Hearts 4 of Hearts 5 of Hearts 6 of Hearts 7 of Hearts 8 of Hearts 10 of Hearts J of Hearts Q of Hearts K of Hearts A of Diamonds 2 of Diamonds 3 of Diamonds 4 of Diamonds 6 of Diamonds 7 of Diamonds 8 of Diamonds 9 of Diamonds 10 of Diamonds J of Diamonds Q of Diamonds K of Diamonds A of Clubs 2 of Clubs 3 of Clubs 4 of Clubs 5 of Clubs 6 of Clubs 7 of Clubs 8 of Clubs 10 of Clubs J of Clubs Q of Clubs K of Clubs A of Spades 2 of Spades 3 of Spades 4 of Spades 5 of Spades 6 of Spades 7 of Spades 8 of Spades 9 of Spades 10 of Spades J of Spades Q of Spades K of Spades Player Hand ---------------- A of Hearts 9 of Hearts 5 of Diamonds 9 of Clubs 

Note that the first four lines are user promptsi.e., the values 1, 8, 16, and 32 must be entered manually. You may use the command:

echo "1 8 16 32" | ./cardShuffle 4 

to enter these values automatically. (This makes use of pipes a topic from CS 252.) On a Windows system, you would omit the ./.

The easiest way to see generate the expected output is to run the sample executable solution I have provided.

1.3 Your Tasks

Complete the remaining 2 member functions within the Hand class:

Hand::add

Hand::display

Refer to the comments in hand.h. You are required to complete the definitions of these two functions in hand.cpp.

The add function takes a card and adds it to the end of the cards array.

The display function requires you to print each element of the cards array. An individual Card can be printed with the usual <

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 Databases Questions!