Create class to represent Card Shuffling and Dealing - C++ Class

C++ examples for Class:Member Function

Description

Create class to represent Card Shuffling and Dealing

Demo Code

                                                                                                                                                           
#include <string>
                                                                                                                                                           
class Card {/*from w  w  w.  j a v a2 s  . c  o m*/
 public:
    Card(int, int);
                                                                                                                                                           
    std::string toString() const;
                                                                                                                                                           
 private:
    static std::string suits[];
    static std::string faces[];
                                                                                                                                                           
    int face;
    int suit;
};
std::string Card::suits[5] = {"", "clubs", "diamonds", "hearts", "spades"};
std::string Card::faces[14] = {"",     "ace",  "two",   "three", "four",
                               "five", "six",  "seven", "eight", "nine",
                               "ten",  "jack", "queen", "king"};
                                                                                                                                                           
Card::Card(int f, int s) {
    face = f;
    suit = s;
}
std::string Card::toString() const {
    return faces[face] + " of " + suits[suit];
}
#include <vector>
                                                                                                                                                           
class DeckOfCards {
 public:
    DeckOfCards();
                                                                                                                                                           
    void shuffle();
    Card dealCard();
    bool moreCards() const;
                                                                                                                                                           
 private:
    const int TOTAL_CARDS = 52;
                                                                                                                                                           
    std::vector<Card> deck;
                                                                                                                                                           
    int currentCard;
};
#include <algorithm>
#include <ctime>
#include <stdexcept>
                                                                                                                                                           
DeckOfCards::DeckOfCards() : currentCard(0) {
    std::srand(std::time(0));
                                                                                                                                                           
    for (int i = 1; i <= 4; ++i) {
        for (int j = 1; j <= 13; ++j) {
            deck.push_back(Card(j, i));
        }
    }
}
void DeckOfCards::shuffle() {
    for (int i = 0, r1 = 0, r2 = 0; i < TOTAL_CARDS; ++i) {
        r1 = rand() % TOTAL_CARDS;
        r2 = rand() % TOTAL_CARDS;
                                                                                                                                                           
        std::iter_swap(deck.begin() + r1, deck.begin() + r2);
    }
}
Card DeckOfCards::dealCard() {
    if (moreCards())
        return deck[currentCard++];
    else
        throw std::invalid_argument("end of deck reached");
}
bool DeckOfCards::moreCards() const { return currentCard < TOTAL_CARDS; }
                                                                                                                                                           
#include <iostream>
                                                                                                                                                           
int main(int argc, const char *argv[]) {
    DeckOfCards deck1;
                                                                                                                                                           
    deck1.shuffle();
                                                                                                                                                           
    while (deck1.moreCards()) {
        std::cout << deck1.dealCard().toString() << std::endl;
    }
    return 0;
}

Result


Related Tutorials