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

Write a Card class and a Deck class that stores a deck of these Cards. Then, use

ID: 3717229 • Letter: W

Question

Write a Card class and a Deck class that stores a deck of these Cards.

Then, use the Monte Carlo method to determine the odds of receiving at least one pair in a hand of cards. A pair is any two cards with the same rank. Recall we used the Monte Carlo method to determine the value of PI in LAB 1 this quarter.

Along with outputting to the terminal the percentage of times a pair is found, your program should also give the user the option to output to a file the contents of each hand and whether it found a pair in that hand. Be sure to reshuffle the deck between each deal (simulation).

Your main function should seed the random function with the seed 2222.

Given this seed, here is what a sample run of your program will look like in cloud9 if the user chooses NOT to output to a file:

Here is what a sample run of your program will look like in cloud9 if the user DOES choose to output to a file:

And the file hands.dat in this case would look like this:

Required Card interface

Required Deck interface

Required global functions (declared and implemented within main.cpp file)

Explanation / Answer

main.cpp


#include <fstream>
#include <ostream>
#include <string>
#include <cstdlib>
#include <vector>
#include "Deck.h"
using namespace std;
bool hasPair(const vector<Card> &);
ostream & operator<<(ostream &, const vector<Card> &);

int main() {
    srand(2222);
    Deck d1;
    vector<int> theDeck;
    ofstream outFS;
    vector<Card> hand;
    string fileYN = "";
    string fileName = "";
    int cardsPerHand = 0;
    int simulations = 0;
    int numPairs = 0;
    int simNum = 0;
    cout << "Do you want to output all hands to a file?(Yes/No) ";
    cin >> fileYN;
    cout << endl;
    if(fileYN == "Yes"){
        cout << "Enter name of output file: ";
        cin >> fileName;
        cout << endl;
         outFS.open(fileName.c_str());
            if (!outFS.is_open()) {
                cout << "Could not open file " << fileName << "." << endl;
                return 1;
                }
    }
        cout << "Enter number of cards per hand: ";
        cin >> cardsPerHand;
        cout << endl;
        cout << "Enter number of deals (simulations): ";
        cin >> simulations;
        cout << endl;
      
      
      
      
    if(fileYN == "Yes"){
        while(simNum < simulations){
            d1.shuffleDeck();
            for(int i = 0; i < cardsPerHand; i++){
                 hand.push_back(d1.dealCard());
            }
            if(hasPair(hand) == true){
                numPairs++;
                outFS << "Found Pair!! " << hand.at(0) << ", ";
                for(unsigned int x = 1; x < hand.size(); x++){
                    outFS << hand.at(x) << ", ";
                }
                outFS << hand.at(hand.size() - 1) << endl;
            }
            else{
                outFS << "             " << hand.at(0);
                for(unsigned int x = 0; x < hand.size(); x++){
                    outFS << hand.at(x) << ", ";
                }
                outFS << hand.at(hand.size() - 1) << endl;
            }
        hand.clear();
        simNum++;
    }
        outFS.close();
    }
    else{
        while(simNum < simulations){
        d1.shuffleDeck();
        for(int i = 0; i < cardsPerHand; i++){
            hand.push_back(d1.dealCard());
        }
            if(hasPair(hand) == true){
                numPairs++;
            }
            simNum++;
        hand.clear();
        }
    }    
    cout << "Chances of receiving a pair in a hand of " << cardsPerHand << " cards is: " << (static_cast<double>(numPairs) / simulations) * 100 << "%" << endl;

  
    return 0;
}

   /* Passes in a vector Cards and returns true if any 2 Cards have the same rank.
   Otherwise returns false.
*/
bool hasPair(const vector<Card> &hand){
    for(unsigned int i = 0; i < hand.size(); i++){
        for(unsigned int d = i + 1; d < hand.size(); d++){
            if(hand.at(d).getRank() == hand.at(i).getRank()){
                return true;
            }
        }
    }
        return false;

}
/* Passes in a vector of Cards and outputs to the stream each Card
   separated by a comma and a space,
   with no comma, space, or newline at the end.
*/
ostream & operator<<(ostream &out, const vector<Card> &dealtCards){
    for(unsigned int i = 0; i < dealtCards.size() - 1; i++){
        out << dealtCards.at(i) << ", ";
    }
    out << dealtCards.at(dealtCards.size() - 1);
    return out;
}

Card.cpp

#include <string>
#include <ostream>
#include "Card.h"
using namespace std;
    /* Assigns a default value of 2 of Clubs
    */
    Card::Card(){
     rank = 2;
     suit = 'c';
    }
    Card::Card(char a, int x){
      if(rank < 2 || rank > 13){
       rank = 2;
      }
      rank = x;
      suit = tolower(a);
    
      return;
    }
    /* Returns the Card's suit
    */
    char Card::getSuit() const{
     return suit;
    }
    /* Returns the Card's rank as an integer
    */
    int Card::getRank() const{
     return rank;
    }

    ostream & operator<<(ostream &out, const Card &cardShown){
         char z = cardShown.getSuit();
         string suitName = "";
         if(z == 'c'){
          suitName = "Clubs";
         }
         if(z == 'h'){
          suitName = "Hearts";
         }
         if(z == 's'){
          suitName = "Spades";
         }
         if(z == 'd'){
          suitName = "Diamonds";
         }
         if(cardShown.getRank() == 1){
          out << "Ace" << " of " << suitName;
         }
         else if(cardShown.getRank() == 11){
          out << "Jack" << " of " << suitName;
         }
         else if(cardShown.getRank() == 12){
       out << "Queen" << " of " << suitName;
         }
         else if(cardShown.getRank() == 13){
          out << "King" << " of " << suitName;
         }
         else{
         out << cardShown.getRank() << " of " << suitName;
         }
         return out;
    }
  
   Card.h
  
   #include <iostream>
using namespace std;

class Card {
private:
    char suit;
    int rank;
public:

    /* Assigns a default value of 2 of Clubs
    */
    Card();


    Card(char, int);


    /* Returns the Card's suit
    */
    char getSuit() const;


    /* Returns the Card's rank as an integer
    */
    int getRank() const;

    friend ostream & operator<<(ostream &, const Card &);
};


Deck.cpp

#include <algorithm>
#include <vector>
#include <string>
#include "Deck.h"
using namespace std;
    Deck::Deck(){
     char suit = 'a';
      for(unsigned int z = 1; z < 5; z++){
       if(z == 4){
        suit = 'c';
       }
       if(z == 3){
        suit = 'd';
       }
       if(z == 2){
        suit = 'h';
       }
       if(z == 1){
        suit = 's';
       }
         for(unsigned int i = 13; i > 0; i--){
            theDeck.push_back(Card(suit, i));
          }
      }
    }
    /* Deals (returns) the top card on the deck.
       Removes this card from theDeck and places it in the dealtCards.
    */
    Card Deck::dealCard(){
     Card CardDealt = theDeck.back();
     theDeck.pop_back();
     dealtCards.push_back(CardDealt);
     return CardDealt;
    }


    /* Places all cards back into theDeck and shuffles them into random order.
    */
    void Deck::shuffleDeck(){
     reverse(dealtCards.begin(), dealtCards.end());
     for(unsigned int i = 0; i < dealtCards.size(); i++){
      theDeck.push_back(dealtCards.at(i));
     }
     dealtCards.resize(0);
     random_shuffle(theDeck.begin(), theDeck.end());
    }


    /* returns the size of the Deck (how many cards have not yet been dealt).
    */
    unsigned Deck::deckSize() const{
     return theDeck.size();
    }
  
  
Deck.h

#include "Card.h"
#include <vector>

using namespace std;
class Deck {
private:
    vector<Card> theDeck;
    vector<Card> dealtCards;
public:
    Deck();

    /* Deals (returns) the top card on the deck.
       Removes this card from theDeck and places it in the dealtCards.
    */
    Card dealCard();


    /* Places all cards back into theDeck and shuffles them into random order.
    */
    void shuffleDeck();


    /* returns the size of the Deck (how many cards have not yet been dealt).
    */
    unsigned deckSize() const;
};

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