Please finish the following using C++ programming: This week you will create the
ID: 3675933 • Letter: P
Question
Please finish the following using C++ programming:
This week you will create the beginnings of a Five Crowns program (this is a card game). This program
will be built on in future projects.
1. You will create a deck of 116 cards (suggested implementation: a vector. This allows you to use
push_back to add each card to the vector).
a. You should create a class Card that contains the following:
i. Two enum types representing faces and suits.
ii. Data members face and suit, of enum type.
iii. A constructor that receives two enums representing face and suit and uses them
to initialize the data members.
b. A “toString” function that returns a string in the form “face of suit” (eg. “three of
spades.”). Jokers do not have a suit, so they would simply return “Joker”.
2. After you create the deck, print it out.
3. Then you will use a method you create to shuffle the deck. Here is a suggested algorithm for
shuffling: Iterate through the array of Cards. For each Card, randomly select another card in the
deck and swap the two Cards.
4. Once you have shuffled, print the deck again.
5. This assignment should have THREE separate files: header, implementation, and main.
Here are the details on the 116-card deck:
1. There are five suits:
a. Clubs
b. Diamonds
c. Hearts
d. Spades
e. Stars
2. Numbers of cards:
a. There are 10 (2 of each suit) of 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, and King. (i.e. no twos
and no aces).
b. There are six Jokers (Jokers do not have a suit).
(This adds up to 116 cards).
3. Point values:
a. Threes have 3 points, fours have 4 points, and so on up to 10 (5 – 5, 6 – 6, 7 – 7, 8 – 8, 9–
9, 10 – 10)
b. Jacks have 11 points.
c. Queens have 12 points.
d. Kings have 13 points.
e. Jokers have 20 points.
Explanation / Answer
cards.h
#include <iostream>
#include <string>
using namespace std;
enum faces {Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Joker};
enum suits {Clubs, Diamonds, Hearts, Spades, Stars, NotAvailable};
class Card {
public:
faces face;
suits suit;
Card(faces face, suits suit);
string toString();
};
cards.cpp
#include "cards.h"
#include <iostream>
#include <string>
using namespace std;
string getTextForSuit( suits suit )
{
switch( suit )
{
case Clubs:
return "Clubs";
case Diamonds:
return "Diamonds";
case Hearts:
return "Hearts";
case Spades:
return "Spades";
case Stars:
return "Stars";
default:
return "Not recognized..";
}
}
string getTextForFace( faces face) {
switch( face ) {
case Three:
return "Three";
case Four:
return "Four";
case Five:
return "Five";
case Six:
return "Six";
case Seven:
return "Seven";
case Eight:
return "Eight";
case Nine:
return "Nine";
case Ten:
return "Ten";
case Jack:
return "Jack";
case Queen:
return "Queen";
case King:
return "King";
default:
return "Not recognized..";
}
}
Card::Card(faces face, suits suit) {
this->face = face;
this->suit = suit;
}
string Card::toString() {
if (this->face == Joker)
return "Joker";
return getTextForFace(face) + " of " + getTextForSuit(suit);
}
cardsImplementation.cpp
#include "cards.cpp"
#include <iostream>
#include <vector>
#include <string>
#include <random>
using namespace std;
void shuffleDeck(vector<Card> *deck) {
mt19937 rng;
rng.seed(random_device()());
uniform_int_distribution<std::mt19937::result_type> dist(0,115);
for (int i = 0; i < deck->size(); i++) {
int randomIndex = dist(rng);
Card randomCard = deck->at(randomIndex);
(*deck)[randomIndex] = (*deck)[i];
(*deck)[i] = randomCard;
}
}
int main() {
vector<Card> deck;
for (int suit = Clubs; suit <= Stars; suit++) {
for (int face = Three; face <= King; face++) {
Card newCard = Card(static_cast<faces>(face), static_cast<suits>(suit));
deck.push_back(newCard);
deck.push_back(newCard);
}
}
for (int i = 0; i < 6; i++) {
Card newCard = Card(Joker, NotAvailable);
deck.push_back(newCard);
}
for (int i = 0; i < deck.size(); i++) {
cout << deck[i].toString() << endl;
}
shuffleDeck(&deck);
cout << "************SHUFFLED DECK****************" << endl;
for (int i = 0; i < deck.size(); i++) {
cout << deck[i].toString() << endl;
}
}