In: Computer Science
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.
// 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: