In: Computer Science
C++
In this assignment you will use your Card class to simulate a deck of cards. You will create a Deck class as follows:
Write a main function that does the following:
#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;
}