Simpe Playing Card Deck Creator and Shuffler

This is a playing card deck creator and shuffler i made yesterday. It is very simple now, but i believe that it could easily be implemented into a card game program of some sort. Any tips/criticisms regarding the code or ways to improve it would be appreciated :)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <ctime>

#define STANDARD_DECK 52


using std::cout;
using std::endl;
using std::cin;



class playingCard               //When created, an instance of this class is equal to one playing card
{
    public:
        std::string suit;               //The suit of the card
        char card;                      //The number/ letter of the card
        int value;                      //Value of card

        int index;                      //Index number, used for shuffling
};


void init_deck(playingCard fresh_deck[STANDARD_DECK])
{
    int currentCard = 0;

    for(int suit = 0; suit<4; suit++)           //Does one loop for each suit
    {
        for(int card = 0; card<13; card++)          //Does one loop for each card
        {
            if(suit == 0)
            {
                fresh_deck[currentCard].suit = "Spades";
            }
            else if(suit == 1)
            {
                fresh_deck[currentCard].suit = "Hearts";
            }
            else if(suit == 2)
            {
                fresh_deck[currentCard].suit = "Clubs";
            }
            else
            {
                fresh_deck[currentCard].suit = "Diamonds";
            }
            fresh_deck[currentCard].value = card;        //Gives the card a set value
            fresh_deck[currentCard].index = currentCard; //Gives each card an index number for easy reference
            currentCard++;
        }
    }

    currentCard = 0;

    while(currentCard<STANDARD_DECK) //This loop sets the cards symbol in relation to
                                     // the value it recieved in the previous loop
    {
        if(fresh_deck[currentCard].value == 0)
            fresh_deck[currentCard].card = '2';

        else if(fresh_deck[currentCard].value == 1)
            fresh_deck[currentCard].card = '3';

        else if(fresh_deck[currentCard].value == 2)
            fresh_deck[currentCard].card = '4';

        else if(fresh_deck[currentCard].value == 3)
            fresh_deck[currentCard].card = '5';

        else if(fresh_deck[currentCard].value == 4)
            fresh_deck[currentCard].card = '6';

        else if(fresh_deck[currentCard].value == 5)
            fresh_deck[currentCard].card = '7';

        else if(fresh_deck[currentCard].value == 6)
            fresh_deck[currentCard].card = '8';

        else if(fresh_deck[currentCard].value == 7)
            fresh_deck[currentCard].card = '9';

        else if(fresh_deck[currentCard].value == 8)
            fresh_deck[currentCard].card = 'T';

        else if(fresh_deck[currentCard].value == 9)
            fresh_deck[currentCard].card = 'J';

        else if(fresh_deck[currentCard].value == 10)
            fresh_deck[currentCard].card = 'Q';

        else if(fresh_deck[currentCard].value == 11)
            fresh_deck[currentCard].card = 'K';

        else if(fresh_deck[currentCard].value == 12)
            fresh_deck[currentCard].card = 'A';

        currentCard++;
    }
}

void print_deck(playingCard deck[STANDARD_DECK])
{
    for(int cc = 0; cc< STANDARD_DECK; cc++)    //This loop will print 52 individual cards
    {
        cout << deck[cc].card << " of " << deck[cc].suit<< " and the index # is : " << deck[cc].index << endl;

        if((cc + 1)% 13 == 0)       //This if statement will create a line
               // break after every 13 cards / every suit in a fresh deck
            cout << endl;
    }




}

void shuffle_deck( playingCard real_deck[STANDARD_DECK])
{
    playingCard temp_deck[STANDARD_DECK];   //This creates a fresh, new temporary deck of cards
    init_deck(temp_deck);

    for(int i = 0; i < STANDARD_DECK; i++)
    {
        real_deck[i] = temp_deck[rand()%STANDARD_DECK];    //Select a random 
                         //card from temp_deck and place it in the real_deck

        for(int j = 0; j <= i; j++)
        {
            if(real_deck[i].index == real_deck[j].index && j!=i )//If the card
                             //selected has already been placed before the current one
                --i;         //it insert another random card for the spot in
                             //the array until a new one is chosen
        }
    }
}

int main()
{
    srand(time(0));

    playingCard my_deck[STANDARD_DECK];      //This creates an array of 52 cards
    init_deck(my_deck);            //This fills the array with all the standard playing cards



    print_deck(my_deck);           //This function prints the deck of cards from top to bottom

    cin.get();
    shuffle_deck(my_deck);         //This function shuffles the array in a random order
    print_deck(my_deck);
    
    cin.get();
    shuffle_deck(my_deck);         //An example of re-shuffling
    print_deck(my_deck);

}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include <iostream>
#include <string>
#include <ctime>
#include <algorithm>

struct playing_card
{
        std::string suit;               //The suit of the card
        char card;                      //The number/ letter of the card
        int value;                      //Value of card
        std::size_t index ;
};

// http://www.stroustrup.com/C++11FAQ.html#constexpr
constexpr std::size_t NSUITS = 4 ;
constexpr std::size_t NCARDS_PER_SUIT = 13 ;
constexpr std::size_t NCARDS = NSUITS * NCARDS_PER_SUIT ;

// http://en.cppreference.com/w/cpp/language/type_alias
using standard_deck = playing_card[NCARDS] ;

void init( standard_deck& fresh_deck )
{
    static const std::string suits[NSUITS] = { "Spades", "Hearts", "Diamonds", "Clubs" } ;
    static constexpr char letters[NCARDS_PER_SUIT+1] = "23456789TJQKA" ;

    for( std::size_t index = 0 ; index < NCARDS ; ++index )
    {
        fresh_deck[index].index = index ;
        fresh_deck[index].suit = suits[ index / NCARDS_PER_SUIT ] ;
        fresh_deck[index].value = index % NCARDS_PER_SUIT ;
        fresh_deck[index].card = letters[ fresh_deck[index].value ] ;
    }
}

void print( const playing_card& card ) { std::cout << card.card << " of " << card.suit << '\n' ; }

void print( const standard_deck& deck )
{
    // http://www.stroustrup.com/C++11FAQ.html#for
    // http://www.stroustrup.com/C++11FAQ.html#auto
    for( const auto& card : deck ) print(card) ;
    std::cout << "-------------------------\n" ;
}

// http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle
// http://en.cppreference.com/w/cpp/algorithm/random_shuffle
void shuffle( standard_deck& deck ) { std::random_shuffle( deck, deck+NCARDS ) ; }

int main()
{
    // http://www.stroustrup.com/C++11FAQ.html#nullptr
    std::srand( std::time(nullptr) ) ;

    standard_deck my_deck ;

    init(my_deck) ;
    print(my_deck) ;

    shuffle(my_deck) ;
    print(my_deck) ;
}

http://coliru.stacked-crooked.com/a/afa9702b845abcb7
Last edited on
Topic archived. No new replies allowed.