Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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: 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 in cloud9 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 interface

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

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

/* Assigns the Card the suit and rank provided. suits: c = Clubs, d = Diamonds, h = Hearts, s = Spades If an invalid suit is provided, sets the suit to Clubs ranks: 1 - 13 (1 = Ace, 11 = Jack, 12 = Queen, 13 = King) If an invalid rank is provided, sets the rank to 2 Accepts lower or upper case characters for suit */ Card(char, int); /* Returns the Card's suit */ char getSuit() const; /* Returns the Card's rank as an integer */ int getRank() const; /* Outputs a Card in the following format: Rank of Suit For example, if the rank is 3 and the suit is h: 3 of Hearts Or, if the rank is 1 and the suit is d: Ace of Diamonds Or, if the rank is 12 and the suit is c: Queen of Clubs etc. */ friend ostream & operator<<(ostream &, const Card &); }; Required Deck interface class Deck { private: vector theDeck; vector dealtCards; public: /* Constructs a Deck of 52 cards: Ace, 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King of each suit. Cards should start off in this order with the order of suits being: Clubs, Diamonds, Hearts, Spades. */ 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. Uses random_shuffle function from algorithm standard library. */ void shuffleDeck(); /* returns the size of the Deck (how many cards have not yet been dealt). */ unsigned deckSize() const; }; 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 &);

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Processing

Authors: David Kroenke

11th Edition

0132302675, 9780132302678

More Books

Students also viewed these Databases questions