# Random card dealer function

• 04-23-2007
Loic
Random card dealer function
I just made a function that will return a random card from a sorted deck, but I cant get it to stop shuffling the cards every time I goto retrieve one,
Code:

char get_card(short int c)
{
vector<char> cards;
cards.push_back('A');
cards.push_back('2');
cards.push_back('3');
cards.push_back('4');
cards.push_back('5');
cards.push_back('6');
cards.push_back('7');
cards.push_back('8');
cards.push_back('9');
cards.push_back('10');
cards.push_back('J');
cards.push_back('Q');
cards.push_back('K');

for (int i=0; i<=randomNumber(0); i++)
{
random_shuffle(cards.begin(), cards.end());
}
return cards[c];
}

Anyone have an idea how to make it so that, that loop will only run ounce? I was thinking of using a “if” statement, but cant get it to work..
• 04-23-2007
Salem
- create a deck (once)
- shuffle the deck (once)
- call get_card() a number of times, removing one card from the shuffled deck.
• 04-23-2007
laserlight
Wouldn't it be simpler to just generate a random integer in the range [0,13) and return the card corresponding to that?
• 04-23-2007
Loic
Quote:

Originally Posted by laserlight
Wouldn't it be simpler to just generate a random integer in the range [0,13) and return the card corresponding to that?

Well for the game I am writing to work, I need the cards to shuffle at the start of each game and I need to retrieve the next card at different points in the game… And obviously I cant get the same card twice, which is why I used a function…
• 04-23-2007
laserlight
• 04-23-2007
Salem
Create a class called "deck"
Create a method called "shuffle"
Create a method called "deal"
• 04-23-2007
Loic
Quote:

Originally Posted by Salem
Create a class called "deck"
Create a method called "shuffle"
Create a method called "deal"

Sorry im not familiar with class's, and methods... i think ill have to look into it..
• 04-23-2007
Salem
I thought that was the whole point of using C++

> cards.push_back('10');
BTW, '10' is not a single character.
• 04-23-2007
Loic
Quote:

Originally Posted by Salem
I thought that was the whole point of using C++

> cards.push_back('10');
BTW, '10' is not a single character.

Still in my first year of uni, my class hasn't covered that yet. only just learned functions this week.

But thats for your help, ill research class's and methods on google...
• 04-23-2007
anon
In any case, you should probably fill the deck only once. You also shouldn't shuffle it in the function that deals a card: otherwise it will be just the same as returning a random value between 0 and 13 (the cards will repeat).

With classes you can naturally have a variable in the Deck class that represents the index of the next card to be dealt, so you'll never need to worry about getting a wrong card. How this value is managed would be completely transparent in the outside world (similarly how the vector automatically "knows" its size). Without classes you'll need to manage this value yourself (in your code you'll need to ensure that you pass the right argument to the function).
• 04-23-2007
Loic
Can someone point me in the right direction for some info on class's? I'm having a bit of trouble finding something that appears to be relevant to a deck of cards? :S
• 04-23-2007
Loic
I have made a few changes to the funtion, as i cant work out how to use class's...

Code:

char get_card(int c, int b)
{
vector<char> cards;
cards.push_back('A');
cards.push_back('2');
cards.push_back('3');
cards.push_back('4');
cards.push_back('5');
cards.push_back('6');
cards.push_back('7');
cards.push_back('8');
cards.push_back('9');
cards.push_back('10');
cards.push_back('J');
cards.push_back('Q');
cards.push_back('K');

if (b == '1')
{
for (int i=0; i<=randomNumber(0); i++)
{
random_shuffle(cards.begin(), cards.end());
}
}
return cards[c];
}

What i am trying to do is, if i give it a value of 1 for b shuffle the cards, other wise leave that part of code out... But when i try to compile it. i get this error...
Quote:

too few arguments to function `char get_card(int, int)'
• 04-23-2007
Daved
Are you sending two arguments to the function?

It won't work anyway, though. The vector of cards is created every time you call the function, so if you send b = '1', then you will get a card from a shuffled deck, but if you do not send b = '1', then you will get a card from the unshuffled deck.

What you want is to create the vector<char> outside that function. Then call random_shuffle in the same place that you create the vector cards or pass it by reference to a new function that initializes and shuffles it. You can also create a function that returns a vector<char> and just save the return value.

Whatever you do, your get_card code should not be creating and initializing al local vector, you need to save the deck in the calling function.
• 04-23-2007
Loic
ah righto, makes sense... cheers mate...