Open In App

How to Represent the Deck of Cards Using Array in C++?

Last Updated : 06 Jun, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

A deck of cards is a collection of 52 playing cards in which each card is uniquely identified by its suit and rank, there are four suits: heart, diamond, club and spade each having 13 ranks: Ace, 2 to 10, jack, queen, and king. In this article, we will learn how to represent a deck of cards using a C++ array.

Representing Deck of Cards in C++ Arrays

In C++, we can make the use of classes to represent a deck of cards as it provides a structured and efficient way to represent a deck of cards. This arrangement allows for easy access, manipulation, shuffling, and allotment of cards to players, as the deck internally uses array the user defined data type card.

Approach

  • Define a Card class with suit and rank as members.
  • Create a Deck class to manage the deck of cards. Inside it, we create an array of cards to store the deck of card.
  • Create and Initialize the suits and ranks arrays with the names of the suits and ranks.
  • Initialize the deck of cards in a nested loop.
  • For each combination of suit and rank, create a card and add it to the deck.
  • Finally, using a loop, print each card in the deck.

C++ Program that Represents a Deck of Cards

Below is the C++ program that represents a deck of cards using an array

C++
// C++ program for representing the deck of cards in arrays
#include <iostream>
#include <string>
using namespace std;

// class representing a card
class Card {
private:
    string suit;
    string rank;

public:
    // Default constructor
    Card()
        : suit("")
        , rank("")
    {
    }

    // Parameterized constructor
    Card(string s, string r)
        : suit(s)
        , rank(r)
    {
    }

    // Setter for suit
    void setSuit(string s) { suit = s; }

    // Setter for rank
    void setRank(string r) { rank = r; }

    // Getter for suit
    string getSuit() const { return suit; }

    // Getter for rank
    string getRank() const { return rank; }

    // Method to print card details
    void printCard() const
    {
        cout << suit << " " << rank;
    }
};

// class representing a deck of cards
class Deck {
private:
    Card deck[52];

    // data for initialization
    string suits[4]
        = { "hearts", "diamonds", "clubs", "spades" };
    string ranks[13]
        = { "Ace", "2", "3",  "4",    "5",     "6",   "7",
            "8",   "9", "10", "Jack", "Queen", "King" };

public:
    // Constructor to initialize and populate the deck
    Deck()
    {
        for (int i = 0; i < 4; i++) {
            for (int j = 0; j < 13; j++) {
                deck[i * 13 + j].setSuit(suits[i]);
                deck[i * 13 + j].setRank(ranks[j]);
            }
        }
    }

    // Method to print the entire deck
    void printDeck() const
    {
        int suitChange = 0;
        for (int i = 0; i < 52; i++) {
            deck[i].printCard();
            suitChange++;
            if (suitChange < 13)
                cout << " | ";
            else {
                suitChange = 0;
                cout << endl;
            }
        }
    }
};

// Driver code
int main()
{
    Deck deck;

    // Print the deck of cards
    deck.printDeck();

    return 0;
}


Output

hearts Ace | hearts 2 | hearts 3 | hearts 4 | hearts 5 | hearts 6 | hearts 7 | hearts 8 | hearts 9 | hearts 10 | hearts Jack | hearts Queen | hearts King
diamonds Ace | diamonds 2 | diamonds 3 | diamonds 4 | diamonds 5 | diamonds 6 | diamonds 7 | diamonds 8 | diamonds 9 | diamonds 10 | diamonds Jack | diamonds Queen | diamonds King
clubs Ace | clubs 2 | clubs 3 | clubs 4 | clubs 5 | clubs 6 | clubs 7 | clubs 8 | clubs 9 | clubs 10 | clubs Jack | clubs Queen | clubs King
spades Ace | spades 2 | spades 3 | spades 4 | spades 5 | spades 6 | spades 7 | spades 8 | spades 9 | spades 10 | spades Jack | spades Queen | spades King

Time Complexity: O(1), It is constant because the deck size is fixed (52).
Auxiliary Space: O(1)




Next Article
Practice Tags :

Similar Reads