Random Card Generator HELP

Sep 11, 2013 at 7:54pm
Heres my code, what I want to do is make it repeat so it shows a different card under the other one every time the user clicks return. The cards cannot repeat and it must show all 52 cards underneath each other.


#include <iostream>
#include <algorithm>
using namespace std;

// names of ranks.
static const char *ranks[] =
{
"Ace", "Two", "Three", "Four", "Five", "Six", "Seven",
"Eight", "Nine", "Ten", "Jack", "Queen", "King"
};

// name of suites
static const char *suits[] =
{
"Spades", "Clubs", "Diamonds", "Hearts"
};

void print_card(int n)
{
cout << ranks[n % 13] << " of " << suits[n / 13] << endl;
}

int main()
{
srand((unsigned int)time(NULL));

int deck[52];

// Prime, shuffle, dump
for (int i=0;i<52;deck[i++]=i);
random_shuffle(deck, deck+52);
for_each(deck, deck+1, print_card);

system("PAUSE");
return 0;
}
Sep 11, 2013 at 8:13pm
- Put your objects into a Deque: http://www.cplusplus.com/reference/deque/deque/

- Apply the "random_shuffle" function from the "algorithm" header: http://www.cplusplus.com/reference/algorithm/random_shuffle/

- Use the front and pop_front methods from deque to access the elements in your array deleting them as you go.
Last edited on Sep 11, 2013 at 8:13pm
Sep 11, 2013 at 8:16pm
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
#include <vector>
#include <iostream>
#include <algorithm>

enum rank
{
    ace,two, three, four, five, six, seven, eight, nine, ten, jack, queen, king
};

enum suit
{
    hearts, clubs, diamonds, spades
};

class Card
{
public:
    rank r;
    suit s;

    Card(rank r, suit s) : r(r), s(s) {}
    void print()
    {
        std::cout << r << s << std::endl;
    }
};

class Deck
{
    std::vector<Card> cards;

public:
    Deck()
    {
        for (int j = hearts; j <= spades; ++j)
            for (int i = ace; i <= king; ++i)
                cards.push_back( Card( (rank)i, (suit)j ) );
    }
    void shuffle()
    {
        std::random_shuffle( cards.begin(), cards.end() );
    }
    void print()
    {
        for (std::vector<Card>::iterator it = cards.begin(); it != cards.end(); ++it)
            it->print();
    }
};

int main()
{
    Deck d;
    d.shuffle();
    d.print();
}
Last edited on Sep 11, 2013 at 8:20pm
Sep 11, 2013 at 8:18pm
can you show me how?
Last edited on Sep 11, 2013 at 8:18pm
Sep 11, 2013 at 8:36pm
I would but it appears that I have been Ninja'd. With proper enumerations, class layout and everything. You had that laying around didn't you Stewbound?
Last edited on Sep 11, 2013 at 8:37pm
Topic archived. No new replies allowed.