Question

In: Computer Science

C++ In this assignment you will use your Card class to simulate a deck of cards....

C++

In this assignment you will use your Card class to simulate a deck of cards. You will create a Deck class as follows:

  • The constructor will create a full 52-card deck of cards. 2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, Ace for each suit: Clubs, Diamonds, Hearts, Spades
  • Each card will be created on the heap and the deck will be stored using an STL vector of Card pointers.
  • The destructor will free the memory associated with each Card instance.
  • A shuffle method will shuffle the Card pointers into an STL stack. Note that only the pointers will be added to the stack. Shuffle may be called at any time to "reshuffle". Reshuffling clears the stack and repopulates with the full deck.
  • A draw method will allow the caller to receive a pointer to the Card on the top of the shuffled deck. The Card will be popped from the stack.

Write a main function that does the following:

  • Creates a deck of cards.
  • Shuffles the deck.
  • Draws 10 cards and prints each one.
  • Reshuffles the deck.
  • Draws 10 cards and prints each one.

Solutions

Expert Solution

#include <iostream>     // for using cout
#include <vector>       // for using vector
#include <stack>        // for using stack
#include <string>       // for using string
#include <algorithm>    // for using STL algortithms (std::shuffle())
#include <random>       // for using std::default_random_engine()
#include <chrono>       // for generating time based seed

using namespace std;

static vector<string> RANKS = {"0", "A", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
static vector<string> SUITS = {"Clubs", "Diamonds", "Hearts", "Spades"};


class Card {
    /*---------------------------------------------------------------------------
        Card: Class for simulate a card from rank and suit.
        Suits mapping:
            0 --> Clubs
            1 --> Diamonds
            2 --> Hearts
            3 --> Spades
        Ranks mapping:
            ** All numerical ranks is mapped from 2 through 10
            Ace --> 1
            Jack --> 11
            Queen --> 12
            King --> 13

        Functions: 
            getRank(): Function to return rank.
            getSuit(): Function to return suit.
    ----------------------------------------------------------------------------- */
    
    private:
        int rank;
        int suit;
    
    public:
        Card(int rank, int suit) {
            this->rank = rank;
            this->suit = suit;
        }
        
        int getRank() {
            return rank;
        }
        
        int getSuit() {
            return suit;
        }

};


class Deck {

    /*---------------------------------------------------------------------------
        Deck: Class for deck of cards.
        Functions: 
            deckShuffle(): Function to shuffle the deck.
            drawCard(): Function to draw a card from the top of the stack.
    ----------------------------------------------------------------------------- */

    private:
        vector<Card*> deck;
        stack<Card*> deckStack;
        int noOfCards = 52;

    public: 
        Deck() {
            // Initailizing the 52 cards deck.

            for ( int suit = 0; suit < 4; suit++ ) {
                for ( int rank = 1; rank <= 13; rank++ ) {
                    deck.push_back(new Card(rank, suit));
                } 
            }

        }

        void deckShuffle() {
            // Function to shuffle the deck.

            // std:shuffle() used to shuffle the deck
            // using time-based seed
            unsigned seed = std::chrono::system_clock::now().time_since_epoch().count();
            shuffle (deck.begin(), deck.end(), default_random_engine(seed));

            // clearing the stack and then reshuffling.
            while(!deckStack.empty()) {
                deckStack.top() = NULL;
                deckStack.pop();
            }

            for( int i = 0; i < noOfCards; i++ ) {
                deckStack.push(deck[i]);
            }
        }

        Card* drawCard() {
            // Function to draw a card from the top of the stack.

            int cardRank = deckStack.top()->getRank();
            int cardSuit = deckStack.top()->getSuit();
            Card* card = new Card(cardRank, cardSuit);

            // Removing the drawn card from the deck.
            // Comment the below 3 lines of code to always draw from a full shuffled deck.
            delete deck[noOfCards - 1];
            deck.pop_back();
            noOfCards--;

            deckStack.top() = NULL;
            deckStack.pop();
            return card;
        }

        ~Deck() {
            while( !deckStack.empty() ) {
                deckStack.top() = NULL;
                deckStack.pop();
            }

            for(Card* card: deck) {
                delete card;
            }
            deck.clear();  
        }

};

int main() {
    // Your code execution starts from here.

    Deck deck;
    deck.deckShuffle();

    for ( int i = 0; i < 10; i++ ) {
        Card* card = deck.drawCard();
        cout << RANKS[card->getRank()] << " " << SUITS[card->getSuit()] << endl;
    }

    cout << endl;
    deck.deckShuffle();

    for ( int i = 0; i < 10; i++ ) {
        Card* card = deck.drawCard();
        cout << RANKS[card->getRank()] << " " << SUITS[card->getSuit()] << endl;
    }

    return 0;

}



Related Solutions

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...
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,   ...
You are dealt one card from a full deck of 52 cards and your opponent is...
You are dealt one card from a full deck of 52 cards and your opponent is dealt two cards (without replacement). If you get a card between 6 and 10, your opponent pays you 4, and if you get a King or Queen, your opponent pays you 3. If you don't have a 6-10, Queen or King, but you have more hearts than your opponent, your opponent pays you 1. In all other cases you pay 2. What is the...
Cards: Suppose you draw one card from a single deck of cards. (a) What is the...
Cards: Suppose you draw one card from a single deck of cards. (a) What is the probability that you draw an queen? Round your answer to 3 significant digits*. (b) What is the probability that you draw a heart? Round your answer to 3 significant digits*. (c) What is the probability that you draw the queen of hearts? Round your answer to 3 significant digits*. ............................................... *Significant Digits: Here are some probabilities expressed to 3 significant digits. You start counting...
(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...
. If 2 cards are selected from a standard deck of cards. The first card is...
. If 2 cards are selected from a standard deck of cards. The first card is not replaced in the deck before the second card is drawn. Find the following probabilities: a) P(2 Aces) b) P(Queen of hearts and a King) c) P(Q of Hearts and Q of hearts )
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...
Design a c++ program to simulate the BlackJack game. Rules: 1) single deck, 52 cards -both...
Design a c++ program to simulate the BlackJack game. Rules: 1) single deck, 52 cards -both player and dealer are taking cards off the same deck (the same 52 values). 2) display BOTH cards of the players, and ONE card of the computer. 3) Check for blackjack (starting value of 21 for computer or player) -if either side has a blackjack end of the game, next hand of blackjack 4) Computer must hit if 16 or below. 5) Computer must...
You are dealt one card from a deck of 52 cards. What is the probability of...
You are dealt one card from a deck of 52 cards. What is the probability of drawing the jack of clubs or the three of diamonds?
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...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT