# Card game help

• 07-14-2005
aaroroge
Card game help
I am making a card game(that i invented my self)but i need to know how to make it so that i get random cards...i no how to do that but not how to make it so the same card apears again...and i am sorry but the last time i posted a card game i invented some 1 copyed it and said it was there so i can not post my actual code sorry
• 07-14-2005
dwks
rand() will give you a random number from 0 to RAND_MAX (usually about 32768). Call srand(time(0)) to seed it - oh yes, and include <time.h> too (or <ctime> for C++).
• 07-14-2005
aaroroge
i no how to do that but how can i make it so the same card dosnt come up twice
• 07-14-2005
Nazca
In what kind of card game do you draw a random card from the deck? o_O

Anyway...
I'd recommend putting all of your cards into a vector, something like:

std::vector <Card> deck;

Then using deck.push_back() to add on each card in the deck. To draw a card:

Code:

```// Gets a random number within the size of deck. int randNum = (rand()*(RAND_MAX+1))%deck.size(); // Sets your new card to the correspond card in your deck Card newCard = deck[randNum]; // Removes that card from the deck - syntax is ugly - could probably be done cleaner. deck.erase(deck.begin()+(randNum-1), deck.begin()+randNum);```
Should work, but I'm not positive and I'm certainly no expert.
• 07-14-2005
aaroroge
k ill try it and experiment
• 07-14-2005
sand_man
Without using the STL. You just need to create a deck, shuffle it and take one of the top at a time.
• 07-14-2005
dwks
Exactly, a stack. And to "shuffle" it you could use rand().

Is the card game in C++?
• 07-14-2005
Nazca
Quote:

Originally Posted by sand_man
Without using the STL. You just need to create a deck, shuffle it and take one of the top at a time.

I thought by "a random card" he meant any card from the deck at any time?

Although I guess that's pretty much the same thing as shuffling it once and taking it off the top...hmm.

I'm silly.
• 07-16-2005
Ruski
Woah.. what's a vector and where can I find detailed information on how to use them? cause when I did my card game I just used the deck as a class.. and had bool card[56] containing 1 or 0 if the card is in place or not, then i randomized between 1 and total number of cards left, and finally through a while loop, got that card out of the deck and showed it to the user once.. had some functions like clear_deck(), get_card(), and stuff...
• 07-16-2005
aaroroge
sorry for not replying i was in calgary