C++ Problem: Deck of Cards

The problem is to create a program that will randomly select four cards from a deck of 52 cards.

All the cards can be represented using an array named deck, filled with initial values 0 to 51, as follows:

int deck[52];

// Initialize cards

for (int i = 0; i < NUMBER_OF_CARDS; i++)

deck[i] = i;

Card numbers 0 to 12, 13 to 25, 26 to 38, 39 to 51 represent 13 spades, 13 hearts, 13 dia­monds, and 13 clubs, respectively, as shown in Figure 7.3. cardNumber / 13 determines the suit of the card and cardNumber % 13 determines the rank of the card, as shown in Figure 7.4. After shuffling the array deck, pick the first four cards from deck.

Listing 7.3 gives the solution to the problem.

Listing 7.3 DeckOfCards.cpp

1 #include <iostream>
2
#include <ctime>
3
#include <string>
4
using namespace std;
5
6
int main()
7 {
8   
const int NUMBER_OF_CARDS = 52;
9   
int deck[NUMBER_OF_CARDS];
10   string suits[] = {
“Spades”, “Hearts”, “Diamonds”, “Clubs”};
11   string ranks[] = {
“Ace”, “2”, “3”, “4”, “5”, “6”, “7”, “8”, “9”,
12   
“10”, “Jack”, “Queen”, “King”};
13
14   
// Initialize cards
15   for (int i = 0; i < NUMBER_OF_CARDS; i++)
16   deck[i] = i;
17
18   
// Shuffle the cards
19    srand(time(0));

20    for (int i = 0; i < NUMBER_OF_CARDS; i++)
21    {
22       
// Generate an index randomly
23       int index = rand() % NUMBER_OF_CARDS;
24       
int temp = deck[i];
25       deck[i] = deck[index];
26       deck[index] = temp;
27    }
28
29   
// Display the first four cards
30    for (int i = 0; i < 4; i++)
31    {
32       string suit = suits[deck[i] /
13];
33       string rank = ranks[deck[i] %
13];
34       cout <<
“Card number ” << deck[i] << “: ”
35       << rank << ” of ” << suit << endl;
36     }
37
38   
return 0;
39 }

The program defines an array deck for 52 cards (line 9). The deck is initialized with values 0 to 51 in lines 15-16. A deck value 0 represents the card Ace of Spades, 1 represents the card 2 of Spades, 13 represents the card Ace of Hearts, and 14 represents the card 2 of Hearts. Lines 20-27 randomly shuffle the deck. After a deck is shuffled, deck[i] con­tains an arbitrary value. deck[i] / 13 is 0, 1, 2, or 3, which determines a suit (line 32). deck[i] % 13 is a value between 0 and 12, which determines a rank (line 33). If the suits array is not defined, you would have to determine the suit using a lengthy multiway if-else statement as follows:

if (deck[i] / 13 == 0)

cout << “suit is Spades” << endl;

else if (deck[i] / 13 == 1)

cout << “suit is Heart” << endl;

else if (deck[i] / 13 == 2)

cout << “suit is Diamonds” << endl;

else

cout << “suit is Clubs” << endl;

With suits = {“Spades”, “Hearts”, “Diamonds”, “Clubs”} declared as an array, suits[deck / 13] gives the suit for the deck. Using arrays greatly simplifies the solution for this program.

Source: Liang Y. Daniel (2013), Introduction to programming with C++, Pearson; 3rd edition.

Leave a Reply

Your email address will not be published. Required fields are marked *