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: 3914969 • 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. 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 if the user chooses NOT to output to a file: Do you want to output all hands to a file?(Yes/No) No Enter number of cards per hand: 5 Enter number of deals (simulations): 10000 Chances of receiving a pair in a hand of 5 cards is: 49.34% Here is what a sample run of your program will look like if the user DOES choose to output to a file: Do you want to output all hands to a file?(Yes/No) Yes Enter name of output file: hands.dat Enter number of cards per hand: 5 Enter number of deals (simulations): 10 Chances of receiving a pair in a hand of 5 cards is: 40% And the file hands.dat in this case would look like this: 10 of Hearts, Ace of Spades, 6 of Clubs, 8 of Spades, Jack of Hearts Found Pair!! Jack of Diamonds, 3 of Diamonds, 2 of Clubs, 2 of Diamonds, 7 of Diamonds Found Pair!! 6 of Clubs, Queen of Hearts, 4 of Spades, 2 of Hearts, 6 of Hearts 7 of Spades, 8 of Clubs, Queen of Hearts, 10 of Spades, 4 of Hearts Found Pair!! 9 of Hearts, King of Clubs, 5 of Spades, 9 of Diamonds, 6 of Diamonds 9 of Spades, 3 of Spades, 4 of Diamonds, Ace of Hearts, Queen of Diamonds 5 of Clubs, 7 of Clubs, 9 of Hearts, Jack of Clubs, 8 of Hearts Jack of Diamonds, 4 of Hearts, 6 of Hearts, Queen of Diamonds, 7 of Diamonds Jack of Spades, 7 of Spades, 10 of Diamonds, 5 of Clubs, 3 of Clubs Found Pair!! 8 of Clubs, Queen of Hearts, 5 of Hearts, Queen of Spades, 10 of Clubs Required Card and Deck interfaces Card.h and Deck.h contain the class declarations for the Card and Deck classes. Use these to create the class definitions in Card.cpp and Deck.cpp. Card.h and Deck.h files cannot be changed. Required global functions (declared and implemented within main.cpp file) /* Passes in a vector Cards and returns true if any 2 Cards have the same rank. Otherwise returns false. */ bool hasPair(const vector &); /* 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 &, const vector &);

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 &);

};

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