Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Use C++ as a programing language (Visual Studios if you can) Create a program to

ID: 3810676 • Letter: U

Question

Use C++ as a programing language (Visual Studios if you can)

Create a program to shuffle and deal a deck of cards. The program should consist of class Card, class Deck Of Cards and a driver program. Class Card should provide: a) Data members face and suit of type int. b) A constructor that receives two ints representing the face and suit and uses them to initialize the data members. Class Deck Of Cards should contain: a) Two Card objects named deck to store two cards. c) A constructor that takes no arguments and initializes both cards in the deck. You can give these two cards a random face value and suit value, but make sure they are not the same. d) A printCards function that prints the two cards. The driver program should create a Deck Of Cards object, and print the cards that this object has. You need to have 5 files in this project: card.hpp, card.cpp, deckofcards.hpp, deckofcards.cpp, main.cpp

Explanation / Answer

All the files are availaible in below shared location :

https://drive.google.com/open?id=0Bx-j2pttV5o1cFg3QzZlUWdDemM


#include <ostream>
#include <iostream>
#include <string>
using std::cout;
using std::endl;
using std::string;

#include "card.hpp"

const char* Card::getCardTitle() const {
// returns a string object containing the card's title in one the
// following formats: "VALUE of SUIT" or "SIZE Joker"
string strCardTitle;

   // set the proper value
   switch (getFace()) {
       case TWO:
           strCardTitle = "Two";
           break;
       case THREE:
           strCardTitle = "Three";
           break;
       case FOUR:
           strCardTitle = "Four";
           break;
       case FIVE:
           strCardTitle = "Five";
           break;
       case SIX:
           strCardTitle = "Six";
           break;
       case SEVEN:
           strCardTitle = "Seven";
           break;
       case EIGHT:
           strCardTitle = "Eight";
           break;
       case NINE:
           strCardTitle = "Nine";
           break;
       case TEN:
           strCardTitle = "Ten";
           break;
       case JACK:
           strCardTitle = "Jack";
           break;
       case QUEEN:
           strCardTitle = "Queen";
           break;
       case KING:
           strCardTitle = "King";
           break;
       case ACE:
           strCardTitle = "Ace";
           break;
       case SMALL_JOKER:
           strCardTitle = "Small Joker";
           break;
       case BIG_JOKER:
           strCardTitle = "Big Joker";
           break;
   }
  
   // append the "of"
   if (strCardTitle != "Small Joker" && strCardTitle != "Big Joker") {
       strCardTitle += " of ";
   }

   // append the proper Suit
   switch (getSuit()) {
       case HEARTS:
           strCardTitle += "Hearts";
           break;
       case CLUBS:
           strCardTitle += "Clubs";
           break;
       case SPADES:
           strCardTitle += "Spades";
           break;
       case DIAMONDS:
           strCardTitle += "Diamonds";
           break;
   }
  
   return strCardTitle.c_str();
}

int Card::getValue() const {
// returns the value of the card

   switch (getFace()) {
       case TWO:
           return 2;
       case THREE:
           return 3;
       case FOUR:
           return 4;
       case FIVE:
           return 5;
       case SIX:
           return 6;
       case SEVEN:
           return 7;
       case EIGHT:
           return 8;
       case NINE:
           return 9;
       case TEN:
           return 10;
       case JACK:
           return 11;
       case QUEEN:
           return 12;
       case KING:
           return 13;
       case ACE:
           return 14;
       case SMALL_JOKER:
           return 15;
       case BIG_JOKER:
           return 16;
   }
}

void Card::printLargeCard() const {
// draws a large graphical version of the card into the shell/standard ouput buffer

   // print top lines
   cout << "*********" << endl;
   if (Face < TEN) cout << "*" << (Face + 1) << " *" << endl;
   else if(Face == TEN) cout << "*" << (Face + 1) << " *" << endl;
   else {
       switch (Face) {
           case JACK:
               cout << "*J *" << endl;
               break;
           case QUEEN:
               cout << "*Q *" << endl;
               break;
           case KING:
               cout << "*K *" << endl;
               break;
           case ACE:
               cout << "*A *" << endl;
           break;
           case SMALL_JOKER:
               cout << "*J *" << endl;
               break;
           case BIG_JOKER:
               cout << "*J *" << endl;
               break;
       }
   }

   // print middle lines
   switch (Face) {
       case TWO:
           printMiddleLines();
           break;
       case THREE:
           printMiddleLines();
           break;
       case FOUR:
           printMiddleLines();
           break;
       case FIVE:
           printMiddleLines();
           break;
       case SIX:
           printMiddleLines();
           break;
       case SEVEN:
           printMiddleLines();
           break;
       case EIGHT:
           printMiddleLines();
           break;
       case NINE:
           printMiddleLines();
           break;
       case TEN:
           printMiddleLines();
           break;
       case JACK:
           printMiddleLines();
           break;
       case QUEEN:
           printMiddleLines();
           break;
       case KING:
           printMiddleLines();
           break;
       case ACE:
           printMiddleLines();
           break;
       case SMALL_JOKER:
           printMiddleLines();
           break;
       case BIG_JOKER:
           printMiddleLines();
           break;
   }

   // print bottom lines
   if (Face < TEN) cout << "* " << (Face + 1) << "*" << endl;
   else if(Face == TEN) cout << "* " << (Face + 1) << "*" << endl;
   else {
       switch (Face) {
           case JACK:
               cout << "* J*" << endl;
               break;
           case QUEEN:
               cout << "* Q*" << endl;
               break;
           case KING:
               cout << "* K*" << endl;
               break;
           case ACE:
               cout << "* A*" << endl;
           break;
           case SMALL_JOKER:
               cout << "* J*" << endl;
               break;
           case BIG_JOKER:
               cout << "* J*" << endl;
               break;
       }
   }
   cout << "*********" << endl;
}

void Card::printSmallCard() const {
// draws a small graphical version of the card into the shell/standard ouput buffer

switch (Face) {
       case TWO:
           cout << "2" << suit2Sign() << " ";
           break;
   case THREE:
           cout << "3" << suit2Sign() << " ";
           break;
       case FOUR:
           cout << "4" << suit2Sign() << " ";
           break;
       case FIVE:
       cout << "5" << suit2Sign() << " ";
break;
       case SIX:
       cout << "6" << suit2Sign() << " ";
       break;
       case SEVEN:
       cout << "7" << suit2Sign() << " ";
       break;
   case EIGHT:
           cout << "8" << suit2Sign() << " ";
           break;
       case NINE:
       cout << "9" << suit2Sign() << " ";
       break;
       case TEN:
       cout << "10" << suit2Sign() << " ";
       break;
   case JACK:
           cout << "J" << suit2Sign() << " ";
           break;
       case QUEEN:
       cout << "Q" << suit2Sign() << " ";
       break;
       case KING:
       cout << "K" << suit2Sign() << " ";
       break;
   case ACE:
           cout << "A" << suit2Sign() << " ";
           break;
       case SMALL_JOKER:
           cout << "SJ ";
           break;
       case BIG_JOKER:
           cout << "BJ ";
           break;
   }
}

void Card::printMiddleLines() const {
// prints the middle of the card

   switch (Face) {
       case TWO:
           printMiddleLowCard();
           break;
       case THREE:
           printMiddleLowCard();
           break;
       case FOUR:
           printMiddleLowCard();
           break;
       case FIVE:
           printMiddleLowCard();
           break;
       case SIX:
           printMiddleLowCard();
           break;
       case SEVEN:
           printMiddleLowCard();
           break;
       case EIGHT:
           printMiddleLowCard();
           break;
       case NINE:
           printMiddleLowCard();
           break;
       case TEN:
           printMiddleLowCard();
           break;
       case JACK:
           printMiddleHighCard();
           break;
       case QUEEN:
           printMiddleHighCard();
           break;
       case KING:
           printMiddleHighCard();
           break;
       case ACE:
           printMiddleHighCard();
           break;
       case SMALL_JOKER:
           cout << "* *" << endl;
           cout << "* Small *" << endl;
           cout << "* Joker *" << endl;
           cout << "* *" << endl;
           break;
       case BIG_JOKER:
           cout << "* *" << endl;
           cout << "* Big *" << endl;
           cout << "* Joker *" << endl;
           cout << "* *" << endl;
           break;
   }
}

void Card::printMiddleHighCard() const{
// the jack/queen/king/ace have similar output so this function prints
// the middle section for those cards

   switch (Suit) {
       case HEARTS:
           cout << "* *" << endl;
           cout << "*  *" << endl;
           cout << "* *" << endl;
           cout << "* *" << endl;
           break;
       case CLUBS:
           cout << "* *" << endl;
           cout << "*  *" << endl;
           cout << "* *" << endl;
           cout << "* *" << endl;
           break;
       case SPADES:
           cout << "* *" << endl;
           cout << "*  *" << endl;
           cout << "* *" << endl;
           cout << "* *" << endl;
           break;
       case DIAMONDS:
           cout << "* *" << endl;
           cout << "*  *" << endl;
           cout << "* *" << endl;
           cout << "* *" << endl;
           break;
   }
}

void Card::printMiddleLowCard() const {
// the cards 2-10 have similar output so this function prints
// the middle section for those cards

   // find the Suit's sign
   const string SIGN = suit2Sign();

   // print the proper number of signs using that Suit
   switch (Face) {
       case TWO:
           cout << "* " << SIGN << " " << SIGN << " *" << endl;
           cout << "* *" << endl;
           cout << "* *" << endl;
           cout << "* *" << endl;
           break;
       case THREE:
           cout << "* " << SIGN << " " << SIGN << " *" << endl;
           cout << "* " << SIGN << " *" << endl;
           cout << "* *" << endl;
           cout << "* *" << endl;
           break;
       case FOUR:
           cout << "* " << SIGN << " " << SIGN << " *" << endl;
           cout << "* " << SIGN << " " << SIGN << " *" << endl;
           cout << "* *" << endl;
           cout << "* *" << endl;
           break;
       case FIVE:
           cout << "* " << SIGN << " " << SIGN << " *" << endl;
           cout << "* " << SIGN << " " << SIGN << " *" << endl;
           cout << "* " << SIGN << " *" << endl;
           cout << "* *" << endl;
           break;
       case SIX:
           cout << "* " << SIGN << " " << SIGN << " *" << endl;
           cout << "* " << SIGN << " " << SIGN << " *" << endl;
           cout << "* " << SIGN << " " << SIGN << " *" << endl;
           cout << "* *" << endl;
           break;
       case SEVEN:
           cout << "* " << SIGN << " " << SIGN << " *" << endl;
           cout << "* " << SIGN << " " << SIGN << " *" << endl;
           cout << "* " << SIGN << " " << SIGN << " *" << endl;
           cout << "* " << SIGN << " *" << endl;
           break;
       case EIGHT:
           cout << "* " << SIGN << " " << SIGN << " *" << endl;
           cout << "* " << SIGN << " " << SIGN << " *" << endl;
           cout << "* " << SIGN << " " << SIGN << " *" << endl;
           cout << "* " << SIGN << " " << SIGN << " *" << endl;
           break;
       case NINE:
           cout << "* " << SIGN << SIGN << SIGN << " *" << endl;
           cout << "* " << SIGN << " " << SIGN << " *" << endl;
           cout << "* " << SIGN << " " << SIGN << " *" << endl;
           cout << "* " << SIGN << " " << SIGN << " *" << endl;
           break;
       case TEN:
           cout << "* " << SIGN << SIGN << SIGN << " *" << endl;
           cout << "* " << SIGN << " " << SIGN << " *" << endl;
           cout << "* " << SIGN << SIGN << SIGN << " *" << endl;
           cout << "* " << SIGN << " " << SIGN << " *" << endl;
           break;
   }
}

const char* Card::suit2Sign() const {
// returns the sign associated with a Suit
const char* HEART = "";
const char* DIAMOND = "";
const char* CLUB = "";
const char* SPADE = "";

   // find the Suit
   switch(Suit) {
       case HEARTS:
           return HEART;
       case CLUBS:
return CLUB;
       case SPADES:
return SPADE;
       case DIAMONDS:
return DIAMOND;
   }
}

main.cpp

#include <cstdlib>

#include <ostream>
#include <iostream>
#include <string>
#include <algorithm>
#include <iterator>
using std::cout;
using std::endl;
using std::string;

#include "card.hpp"
#include "deckofcards.hpp"

typedef Deck<Card> DeckOfCards;

// prototypes for a few functions that aid in the demonstration
const int getNumberOfCardsInSuit(DeckOfCards &DeckToUse, const SUIT SuitToCheck);
void printDeck(DeckOfCards &DeckToPrint, const bool blnTopToBottom = true);
const DeckOfCards spawnDeck(const bool blnIncludeJokers = false);

int main(int argc, char* argv[]) {
   // Intro
   cout << "***This is a demonstration of the deck class & card class*** " << endl;
  
   // Creating a few demonstration cards
   Card CardNine(NINE, SPADES);
   Card CardQueen(QUEEN, HEARTS);
   Card CardBigJ(BIG_JOKER, NOSUIT);
   Card CardSmallJ(SMALL_JOKER, NOSUIT);

   // Printing the name, value and image in order to demonstrate the Card class works
   cout << "Demonstrating Card Class: " << endl;
   string strTitleBuffer;

   cout << " " << CardNine.getCardTitle() << " (value is " << CardNine.getValue() << "):" << endl;
   CardNine.printLargeCard();

   cout << " " << CardQueen.getCardTitle() << " (value is " << CardQueen.getValue() << "):" << endl;
   CardQueen.printLargeCard();

   cout << " " << CardBigJ.getCardTitle() << " (value is " << CardBigJ.getValue() << "):" << endl;
   CardBigJ.printLargeCard();

   cout << " " << CardSmallJ.getCardTitle() << " (value is " << CardSmallJ.getValue() << "):" << endl;
   CardSmallJ.printLargeCard();
  
   // Creating a deck class, with jokers
   DeckOfCards MyDeck(54);
   MyDeck = spawnDeck(true);
  
   // Demonstrating shuffle method to show that they work
   cout << " Demonstrating Deck Class:" << endl;
  
   cout << " Cards in the deck (shown Bottom to Top):" << endl;
   printDeck(MyDeck, false);
  
   cout << " Cards in the deck (shown Top to Bottom):" << endl;
   printDeck(MyDeck, true);
  
   cout << " Cards in the deck (Shuffled and shown Top to Bottom):" << endl;
   MyDeck.shuffle();
   printDeck(MyDeck, true);
  
   // Demonstrating drawing methods to show that they work
   cout << " Card-drawing related methods:" << endl;
   Card DemoCard(NOFACE, NOSUIT);
  
   DemoCard = MyDeck.draw(TOP);
   cout << "Card drawn from top: ";
   DemoCard.printSmallCard();
   cout << " ";
  
   DemoCard = MyDeck.draw(MIDDLE);
   cout << "Card drawn from middle: ";
   DemoCard.printSmallCard();
   cout << " ";
  
   DemoCard = MyDeck.draw(BOTTOM);
   cout << "Card drawn from bottom: ";
   DemoCard.printSmallCard();
   cout << " ";
  
   DemoCard = MyDeck.peek();
   cout << "Peek at top card: " << DemoCard.getCardTitle();;
  
   // Demonstrating miscellaneous methods to show that they work
   cout << " Testing Miscellaneous methods:" << endl;
   cout << "Heart cards left in deck: " << getNumberOfCardsInSuit(MyDeck, HEARTS) << endl;
   cout << "Club cards left in deck: " << getNumberOfCardsInSuit(MyDeck, CLUBS) << endl;
   cout << "Spade cards left in deck: " << getNumberOfCardsInSuit(MyDeck, SPADES) << endl;
   cout << "Diamond cards left in deck: " << getNumberOfCardsInSuit(MyDeck, DIAMONDS) << endl;
   cout << "Joker cards left in deck: " << getNumberOfCardsInSuit(MyDeck, NOSUIT) << endl;
   cout << "Total cards left in deck: " << MyDeck.size() << endl;
  
   Card CardToSearchFor = Card(QUEEN, HEARTS);
   cout << "Queen of Hearts still in deck: ";
   if (MyDeck.checkFor(CardToSearchFor) == true) cout << "yes" << endl;
   else cout << "no" << endl;
  
   cout << " ";
  
   return EXIT_SUCCESS;
}

const int getNumberOfCardsInSuit(DeckOfCards &DeckToUse, const SUIT SuitToCheck) {
// returns the number cards in a particular suit as an integer
int intCardsInSuit = 0;

   // look at card
   for (std::vector<Card>::iterator iterIndex = DeckToUse.begin(); iterIndex != DeckToUse.end(); iterIndex++) {
       if (iterIndex->getSuit() == SuitToCheck) intCardsInSuit++;
   }
  
   return intCardsInSuit;
}

void printDeck(DeckOfCards &DeckToPrint, const bool blnTopToBottom) {
// prints list of cards in deck
using std::for_each;
using std::mem_fun_ref;
using std::reverse;

   // reverse if needed
   if (blnTopToBottom == false) reverse(DeckToPrint.begin(), DeckToPrint.end());

   // now print
   for_each(DeckToPrint.begin(), DeckToPrint.end(), mem_fun_ref(&Card::printSmallCard));
  
   // reverse back to the correct way if needed
   if (blnTopToBottom == false) reverse(DeckToPrint.begin(), DeckToPrint.end());
}

const DeckOfCards spawnDeck(const bool blnIncludeJokers) {
// creates a deck of 52/54 cards

   DeckOfCards LocalDeck;
  
   // add the two jokers into the deck if needed
   if (blnIncludeJokers == true) {
       LocalDeck.add(Card(BIG_JOKER, NOSUIT));
       LocalDeck.add(Card(SMALL_JOKER, NOSUIT));
   }
  
   // add the usual 52 cards
   SUIT CurrentSuit;
   for (int i = DIAMONDS; i != NOSUIT; i--) {
       switch (SUIT(i)) {
           case HEARTS:
               CurrentSuit = HEARTS;
               break;
           case CLUBS:
               CurrentSuit = CLUBS;
               break;
           case SPADES:
               CurrentSuit = SPADES;
               break;
           case DIAMONDS:
               CurrentSuit = DIAMONDS;
               break;
           default:
               // no action to take place
               break;
       }
      
       // adding card's 2-Ace to the deck for the current suit
       for (int i = ACE; i >= TWO; i--) {
           LocalDeck.add(Card(FACE(i), CurrentSuit));
       }
   }

   return LocalDeck;
}

deckofcards.hpp

#ifndef DECKOFCARDS_HPP
#define DECKOFCARDS_HPP

#include <cstdlib>
#include <string>
#include <ctime>
#include <vector>
#include <algorithm>

enum SPOT {TOP, MIDDLE, BOTTOM};

template <typename T>
class Deck {
   typedef typename std::vector<T>::iterator iterator;
  
   public:
       Deck() { }
       Deck(const int intInitialAmount);
       ~Deck() { }
      
       // arrangement-related methods
       void shuffle();

       // drawing-related methods
       const T draw(const SPOT FromWhere = TOP);
       const T peek(const SPOT FromWhere = TOP) const;
      
       // other deck specific methods
       void add(const T &ToInsert, const SPOT ToWhere = TOP);
       const bool checkFor(T &ToFind) const;

       // standard container-related methods
       iterator begin()
           {
               // returns const iterator to the top of the deck
               return InDeck.begin();
           }
       iterator end()
           {
               // returns iterator to the bottom of the deck
               return InDeck.end();
           }
       const int size() const
           {
               return InDeck.size();
           }
                  
   protected:
       void removeDrawn(const T& CardToRemove);
      
   public:
       std::vector<T> InDeck;
};

#endif

deckofcards.cpp

#include <cstdlib>
#include <string>
#include <ctime>
#include <vector>
#include <algorithm>

#include "deckofcards.hpp"

template <typename T>
Deck<T>::Deck(const int intInitialAmount) {
   // reserve memory upfront. This way we don't have to wait
   // to allocate memory for each new element later.
   InDeck.reserve(intInitialAmount);
}

template <typename T>
void Deck<T>::shuffle() {
// simply shuffles the deck
using std::random_shuffle;

   // ensure that the shuffle is truly random
   srand((unsigned)time(NULL));
  
   // shuffle in an unpredictable order
   random_shuffle(InDeck.begin(), InDeck.end());
}

template <typename T>
const T Deck<T>::draw(const SPOT FromWhere) {
// draws a type either from the top, middle, or bottom of the deck.
//
// NOTE: This method throws an std::out_of_range exception if there are
// not enough cards in the deck.

   // draw the type
   T Buffer;
   if (FromWhere == TOP)
       Buffer = InDeck.at(0);
   else if (FromWhere == MIDDLE)
       Buffer = InDeck.at(InDeck.size() / 2);
   else if (FromWhere == BOTTOM)
       Buffer = InDeck.at(InDeck.size() - 1);
  
   // remove the type drawn
   removeDrawn(Buffer);
  
   return Buffer;
}

template <typename T>
const T Deck<T>::peek(const SPOT FromWhere) const {
// returns type from the deck's top, middle, or bottom.
//
// NOTE: This method throws an std::out_of_range exception if there are
// not enough cards in the deck.
  
   // peek at the top card
   T Buffer;
  
   if (FromWhere == TOP)
       Buffer = InDeck.at(0);
   else if (FromWhere == MIDDLE)
       Buffer = InDeck.at(InDeck.size() / 2);
   else if (FromWhere == BOTTOM)
       Buffer = InDeck.at(InDeck.size() - 1);

   return Buffer;
}

template <typename T>
void Deck<T>::add(const T &ToInsert, const SPOT ToWhere) {
// adds a card to the deck at either the top, middle, or bottom
//
// NOTE: This method throws an std::out_of_range exception if there are
// not enough cards in the deck.
  
   // add the card to spot in the deck
   if (ToWhere == TOP)
       InDeck.insert(InDeck.begin(), ToInsert);
   else if (ToWhere == MIDDLE)
       InDeck[InDeck.size() / 2] == ToInsert;
   else if (ToWhere == BOTTOM)
       InDeck.push_back(ToInsert);
}

template <typename T>
const bool Deck<T>::checkFor(T &ToFind) const {
// returns true if the value passed is in the deck, otherwise false
using std::find;

   if (find(InDeck.begin(), InDeck.end(), ToFind) != InDeck.end()) return true;
  
   return false;
}

template <typename T>
void Deck<T>::removeDrawn(const T &ToRemove) {
// remove a particular card from the deck of cards
using std::find;

   InDeck.erase(find(InDeck.begin(), InDeck.end(), ToRemove));
}

cards.hpp

#ifndef CARD_HPP
#define CARD_HPP

enum FACE {NOFACE, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE, SMALL_JOKER, BIG_JOKER};
enum SUIT {NOSUIT, HEARTS, CLUBS, SPADES, DIAMONDS};

class Card {
   public:
       Card()
           {
               Face = NOFACE;
               Suit = NOSUIT;
           }
       Card(const FACE FaceWanted, const SUIT SuitWanted)
           {
               Face = FaceWanted;
               Suit = SuitWanted;
           }
       ~Card() { }
      
       // accessor methods
       SUIT getSuit() const { return Suit; }
       FACE getFace() const { return Face; }
       const char* getCardTitle() const;
       int getValue() const;
  
       // overloaded operators
       bool operator==(const Card &CardComparedAgainst) const
           {
               if (CardComparedAgainst.getFace() == Face && CardComparedAgainst.getSuit() == Suit) {
                   return true;
               } else {
                   return false;
               }
           }
       Card operator=(const Card &CardToEqual)
           {
               Face = CardToEqual.getFace();
               Suit = CardToEqual.getSuit();
               return *this;
           }
  
       // other useful methods
       void printLargeCard() const;
       void printSmallCard() const;
      
   protected:
       void printMiddleLines() const;
       void printMiddleHighCard() const;
       void printMiddleLowCard() const;
       const char* suit2Sign() const;
      
   private:
       SUIT Suit;
       FACE Face;
};

#endif

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote