Question

In: Computer Science

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,
           HEARTS,
           SPADES
       };


       enum ranks
       {
           ACE,
           KINGS,
           QUEENS,
           JACKS,
           TEN,
           NINE,
           EIGHT,
           SEVEN,
           SIX,
           FIVE,
           FOUR,
           THREE,
           TWO

       };

b. 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

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

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

Solutions

Expert Solution

// C++ program to create a class that represents a Card in a standard deck of playing cards

#include <iostream>

using namespace std;

#define CARDS 52 // number of cards in a standard deck

// enumeration for suit of a card

enum suits

{

       CLUBS,

       DIAMONDS,

       HEARTS,

       SPADES

};

// enumeration for rank of a card

enum ranks

{

       ACE,

       KINGS,

       QUEENS,

       JACKS,

       TEN,

       NINE,

       EIGHT,

       SEVEN,

       SIX,

       FIVE,

       FOUR,

       THREE,

       TWO

};

// class Card

class Card

{

private:

       suits suit;

       ranks rank;

public:

       Card();

       Card(suits suit, ranks rank);

       void setSuit(suits suit);

       void setRank(ranks rank);

       suits getSuit() const;

       ranks getRank() const;

       void print() const;

};

// default constructor

Card::Card()

{

       suit = CLUBS;

       rank = ACE;

}

// parameterized constructor

Card::Card(suits suit, ranks rank)

{

       this->suit = suit;

       this->rank = rank;

}

// function to set suit of a card

void Card::setSuit(suits suit)

{

       this->suit = suit;

}

// function to set rank of a card

void Card::setRank(ranks rank)

{

       this->rank = rank;

}

// function to return suit of a card

suits Card::getSuit() const

{

       return suit;

}

// function to return rank of a card

ranks Card::getRank() const

{

       return rank;

}

// function to print a card

void Card::print() const

{

       switch(rank)

       {

       case ACE:

             cout<<"Ace of ";

             break;

       case KINGS:

             cout<<"King of ";

             break;

       case QUEENS:

             cout<<"Queen of ";

             break;

       case JACKS:

             cout<<"Jack of ";

             break;

       case TEN:

             cout<<"10 of ";

             break;

       case NINE:

             cout<<"9 of ";

             break;

       case EIGHT:

             cout<<"8 of ";

             break;

       case SEVEN:

             cout<<"7 of ";

             break;

       case SIX:

             cout<<"6 of ";

             break;

       case FIVE:

             cout<<"5 of ";

             break;

       case FOUR:

             cout<<"4 of ";

             break;

       case THREE:

             cout<<"3 of ";

             break;

       case TWO:

             cout<<"2 of ";

             break;

       default:

             cout<<"Invalid ";

       }

       switch(suit)

       {

       case CLUBS:

             cout<<"Clubs";

             break;

       case DIAMONDS:

             cout<<"Diamonds";

             break;

       case HEARTS:

             cout<<"Hearts";

             break;

       case SPADES:

             cout<<"Spades";

             break;

       default:

             cout<<"Invalid";

       }

       cout<<endl;

}

// function declaration

void initialize(Card deck[CARDS]);

void perfectShuffle(Card deck[CARDS]);

void printDeck(Card deck[CARDS]);

bool compareDeck(Card deck1[CARDS], Card deck2[CARDS]);

int main() {

       Card deck[CARDS];

       initialize(deck); // initialize a standard deck

       Card originalDeck[CARDS];

       // set the original deck to initailized deck

       for(int i=0;i<CARDS;i++)

             originalDeck[i] = deck[i];

       // perform a perfect shuffle

       perfectShuffle(deck);

       // print the initial deck

       cout<<"Initial Deck : "<<endl;

       printDeck(originalDeck);

       // print the deck after first shuffle

       cout<<"\nAfter first perfect shuffle : DECK"<<endl;

       printDeck(deck);

       int n=0;

       // loop that performs perfect shuffle until the deck is restored to its original configuration

       while(!compareDeck(originalDeck,deck))

       {

             n++;

             perfectShuffle(deck);

       }

       // print the number of shuffles required

       cout<<"\nNumber of perfect shuffles required to return the deck to its original configuration : "<<n<<endl;

       // print the final deck

       cout<<"\nFinal Deck : "<<endl;

       printDeck(deck);

       return 0;

}

// function to initialize a standard deck of cards

void initialize(Card deck[CARDS])

{

       int i=0;

       for(int suit=0;suit<4;suit++)

       {

             for(int rank=0;rank<13;rank++)

             {

                    deck[i].setSuit((suits)suit);

                    deck[i].setRank((ranks)rank);

                    i++;

             }

       }

}

// function to perform perfect shuffle of deck as per the steps specified

void perfectShuffle(Card deck[CARDS])

{

       int n=0;

       Card tempDeck[CARDS];

       for(int i=0, j=26;i<CARDS/2;i++,j++)

       {

             tempDeck[n] = deck[i];

             n++;

             tempDeck[n] = deck[j];

             n++;

       }

       for(int i=0;i<CARDS;i++)

       {

             deck[i] = tempDeck[i];

       }

}

// function to print a deck

void printDeck(Card deck[CARDS])

{

       for(int i=0;i<CARDS;i++)

       {

             deck[i].print();

       }

}

// function to compare two standard decks of cards

bool compareDeck(Card deck1[CARDS], Card deck2[CARDS])

{

       for(int i=0;i<CARDS;i++)

       {

             if((deck1[i].getSuit() != deck2[i].getSuit()) || (deck1[i].getRank() != deck2[i].getRank()))

                    return false;

       }

       return true;

}

//end of program

Output:


Related Solutions

(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...
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++ 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...
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...
1) A single card is drawn from a standard 52 card deck. Calculate the probability that...
1) A single card is drawn from a standard 52 card deck. Calculate the probability that a black card or an ace is drawn. (show answer as a reduced fraction #/#) 2)A professor knows from past experience that in a class of 36 students the time for students to complete an exam has an N(50,10) distribution. Suppose that he wants to allow sufficient time so that 98% of the students will complete the exam in the allotted time. How much...
A standard 52 card deck is being used in an exciting experiment! A card is drawn...
A standard 52 card deck is being used in an exciting experiment! A card is drawn randomly from the deck, its information is recorded, then the card is returned to the deck and it is thoroughly shuffled. (a) Determine the probability that if we perform this process 6 times, we get exactly 3 diamonds, and exactly 1 spade. (b) If we repeat this experiment 11 times, what is the probability that we get three times as many clubs as hearts....
suppose a card is selected from a standard deck, its suit is noted, the card is...
suppose a card is selected from a standard deck, its suit is noted, the card is returned to the deck and the deck is shuffled. this process is done 12 times. let X count the number of spades obtained. a. what kind of discrete random variable is X? b. what is the p.m.f of X? c. what is the expected value of X? d. what is the variance of X?
A single card is drawb from a standard 52-card deck. What is the probability that the...
A single card is drawb from a standard 52-card deck. What is the probability that the card is either a) a seven, or an eight, or a ten? b) a heart, or a club, or a spade? c) a five, or an ace, or a heart? d) a queen, or an eight, or a club, or a spade?
For the following questions, find the probability using a standard 52-card deck. Write your answer as...
For the following questions, find the probability using a standard 52-card deck. Write your answer as a fraction or with a colon in lowest terms. Find the probability of drawing a face card. Find the probability of drawing a black card. Find the probability of drawing a red 10. Find the probability of drawing a king. Find the probability of not drawing a face card. Find the probability of drawing a number less than 4. Find the probability of drawing...
Question 1: Part a) A game consists of drawing 1 card from a standard deck of...
Question 1: Part a) A game consists of drawing 1 card from a standard deck of 52 cards. If you draw an Ace you win $50. If you draw a Jack, Queen, or King you win $20. If you draw a 10, 9 or 8 you win $15. If you draw anything lower than an 8, you don’t win anything. If it costs $10 to play, what is your expected value? Part b) It costs $180/yr to buy insurance for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT