Question

In: Computer Science

(In C++) Write a class that will represent a card in a standard deck of playing...

(In C++) Write a class that will represent a card in a standard deck of playing cards. You will need to represent both the suit (clubs, diamonds, hearts or spades) as well as the rank (A, K, Q, J, 10, 9, 8, 7, 6, 5, 4, 3, 2) of each card.

Write methods to
• Initialize the deck of cards
• Perform a perfect shuffle
In a perfect shuffle, the deck is broken exactly in half and rearranged so that the first card is followed by the 27th card, followed by the second card, followed by the 28th card, and so on.
• Print the deck of cards
• Compare two decks of cards

Print out the initial, the deck after the first perfect shuffle, and the final deck.

Print out how many perfect shuffles are necessary to return the deck to its original configuration.

Solutions

Expert Solution

#include <iostream>
#include <iomanip>
#include <vector>

using namespace std;

class Card {
        string suit, rank;

        public:
        Card(string s, string r) {
                suit = s;
                rank = r;
        }

        string toString() {
                return rank + suit;
        }
};

class Deck {
        vector<Card> cards;

        public:
        Deck() {
                string suits[] = {"C", "D", "H", "S"};
                string ranks[] = {"A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2"};

                for(string s: suits) {
                        for(string r: ranks) {
                                cards.push_back(Card(s, r));
                        }
                }
        }

        void print() {
                int i = 0;
                for(Card c: cards) {
                        cout << setw(4) << c.toString();
                        if(++i % 13 == 0) {
                                cout << endl;
                        }
                }
                cout << endl;
        }

        void perfectShuffle() {
                for(int i=0; i<26; i++) {
                        Card c = cards[26+i];
                        
                        cards.erase(cards.begin() + 26 + i);

                        cards.insert(cards.begin() + 2*i + 1, c);
                }
        }

        bool isIdenticalToOriginalDeck() {
                string suits[] = {"C", "D", "H", "S"};
                string ranks[] = {"A", "K", "Q", "J", "10", "9", "8", "7", "6", "5", "4", "3", "2"};

                int i = 0;
                for(string s: suits) {
                        for(string r: ranks) {
                                if(r+s != cards[i++].toString()) {
                                        return false;
                                }
                        }
                }
                return true;
        }
};


int main() {
        Deck d;
        d.print();

        cout << "After perfect shuffle: " << endl;
        d.perfectShuffle();
        d.print();

        int shuffleCount = 1;

        while(!d.isIdenticalToOriginalDeck()) {
                d.perfectShuffle();
                shuffleCount++;
        }

        cout << "After " << shuffleCount << " perfect shuffles, The deck is identical" << endl;
        d.print();
}
**************************************************

Thanks for your question. We try our best to help you with detailed answers, But in any case, if you need any modification or have a query/issue with respect to above answer, Please ask that in the comment section. We will surely try to address your query ASAP and resolve the issue.

Please consider providing a thumbs up to this question if it helps you. by Doing that, You will help other students, who are facing similar issue.


Related Solutions

USE C++ 1. Write a class that will represent a card in a standard deck of...
USE C++ 1. Write a class that will represent a card in a standard deck of playing cards. You will need to represent both the suit (clubs, diamonds, hearts or spades) as well as the rank (A, K, Q, J, 10, 9, 8, 7, 6, 5, 4, 3, 2) of each card. Note: Use enumeration instead of strings For example: Do something like this...        enum suits        {            CLUBS,            DIAMONDS,   ...
Write a class whose instances represent a single playing card from a deck of cards. Playing...
Write a class whose instances represent a single playing card from a deck of cards. Playing cards have two distinguishing properties: rank and suit. Use an enum type to represent rank and another enum type to represent suit. Write another class whose instances represent a full deck of cards. Both these classes should have toString methods. Write a small program to test your deck and card classes. The program can be as simple as creating a deck of cards and...
C++ Question Create a class for a card in a deck of playing cards. The object...
C++ Question Create a class for a card in a deck of playing cards. The object must contain methods for setting and retrieving the suit and the type of card (type of card meaning 2,3,4,5,6,7,8,9,10,J,Q,K,A). Separate the declaration of the class from its implementation by using the declaration in a header file and the implementation in a .cpp file. Also create a driver program and a makefile which complies the code and makes it able to run. This is more...
A five-card poker hand dealt from a standard 52-card deck of playing cards is called a...
A five-card poker hand dealt from a standard 52-card deck of playing cards is called a three-of-a-kind hand if it contains exactly three cards of the same rank (e.g. 3 aces and 2 other cards). How many distinct three-of-a-kind hands can be dealt with? Calculate a numeric answer.
Kyd and North are playing a game. Kyd selects one card from a standard 52-card deck....
Kyd and North are playing a game. Kyd selects one card from a standard 52-card deck. If Kyd selects a face card (Jack, Queen, or King), North pays him $6. If Kyd selects any other type of card, he pays North $3. a) What is Kyd's expected value for this game? Round your answer to the nearest cent. $ b) What is North's expected value for this game? Round your answer to the nearest cent. $
Your friend has chosen a card from a standard deck of 52 playing cards and no...
Your friend has chosen a card from a standard deck of 52 playing cards and no one knows the card except himself. Now you have to guess the unknown card. Before guessing the card, you can ask your friend exactly one question, the question must be either Q1, Q2 or Q3 below: Q1. whether the chosen card is an ace (A)? Q2. whether the chosen card is a spade (♠)? Q3. whether the chosen card is the ace of spades...
Your friend has chosen a card from a standard deck of 52 playing cards and no...
Your friend has chosen a card from a standard deck of 52 playing cards and no one knows the card except himself. Now you have to guess the unknown card. Before guessing the card, you can ask your friend exactly one question, the question must be either Q1, Q2 or Q3 below: Q1. whether the chosen card is a king (K)? Q2. whether the chosen card is a spade (♠)? Q3. whether the chosen card is the king of spades...
Question # 6. (a) From a standard 52 card deck of playing cards, how many 5...
Question # 6. (a) From a standard 52 card deck of playing cards, how many 5 card hands, containing exactly 3 Kings, are possible? (b) From a standard 52 card deck of playing cards, how many 5 card hands, containing exactly 1 Diamond and at most 2 Spades, are possible? (c) From a standard 52 card deck of playing cards, how many 5 card hands, containing exactly 1 Spade and twice as many Hearts as Clubs, are possible? (d) From...
A standard 52-card deck of French playing cards consists of four suits: hearts, spades, clubs, and...
A standard 52-card deck of French playing cards consists of four suits: hearts, spades, clubs, and diamonds. There are 13 cards of each suit; each suit has cards of rank 2 through 10, along with an ace, king, queen, and jack. Typically, hearts and diamonds are the red suits, while spades and clubs are the black suits. Four cards are drawn from the deck, one at a time, without replacement. a) The second card drawn is from a red suit....
We are playing with a 52-card standard deck (four ”suits” ♥, ♠, ♦, ♣, with ”faces”...
We are playing with a 52-card standard deck (four ”suits” ♥, ♠, ♦, ♣, with ”faces” A, 2, 3,...,10, J, Q, K). In how many ways can we choose a 5-card hand, such that (a) four cards have same face? (b) all five cards of same suit? (c) the five cards have consecutive faces (where A can be either the lowest or the highest face)? (d) the five cards form a ”straight flush” (all five cards of same suit, and...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT