hay guys im having rouble with generating a deck of cards...im not really sure exactly how i should do it and i was wondering if i could get some help im using a simple array containing the value 52 but what i want to do is generate a deck of cards and display them, the only problem is im not sure how to do this if you could help me out that would be great.
Try to describe in your own words how you think the algorithm should work. A good way to do this is to take an actual deck of cards. Once you have something you think should work, write it down in human language. Now try to translate that into something a compiler can understand.
If you follow these steps and you're still having problems -- specifically, with the last point -- then come back and show what you have so far.
I'll give you something to get you started: you have 52 spaces, and you can fill them with anything, but a deck has 52 unique cards. What do you do?
int main()
{
int card_id = 0;
for(char i = 0 ; i < 4; ++i)
{
for(char j = 1 ; j <= 13 ; ++j)
{
switch(i)
{
case 0:
deck[card_id].suit = 'D';
break;
case 1:
deck[card_id].suit = 'H';
break;
case 2:
deck[card_id].suit = 'C';
break;
case 3:
deck[card_id].suit = 'S';
break;
}
deck[card_id].value = j;
card_id++;
}
}
return 0;
}
If you are using c++, try a vector instead of a regular old array. There are standard functions in algorithm.h that allow you to shuffle the deck around. You also won't have to deal with buffer overruns. I would also do what helios said above before taking the chuck of code above.
canilao has described one possibility. But a card can be uniquely defined by a single integer between 0 and 51, from which the suit and value can be determined with / and %. Try it that way. Below I've shown the general structure of your program and how to call the standard random_shuffle algorithm.
1 2 3 4 5 6 7 8 9 10
int deck[52];
// fill deck with values 0 to 51
// shuffle deck
random_shuffle( deck, deck + 52 );
// print out one or two hands,
// interpretting each number as suit and value using / and %