Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

Ranok

Card Games

This topic is 6733 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Has anybody here made a card game: If so, what is a good way to shuffle your deck?

Share this post


Link to post
Share on other sites
Advertisement
The best way is to simply run through the deck once, swapping every card with any card that hasn''t been swapped yet (ie select from any card whose index is greater than or equal to the one you''re filling in.) That gives you a perfectly smooth distribution of all the possible shuffles.

-Brian

Share this post


Link to post
Share on other sites
I know I''m starting to sound like a one-trick pony, but...
STL has a random_shuffle function in <algorithm>.

If you''re using an STL container, just do random_shuffle (cont.begin (), cont.end ())

If you''re using an array, do random_shuffle (array, array + size_of_array).

example:

#include <algorithm>
using namespace std;

void main ()
{
int cards[52];
for (int i=0; i<52; i++)
cards[ i ] = i;
random_shuffle (cards, cards + 52); // ta da
}


The actual shuffle algorithm used is to proceed through the array once and swap each element with a randomly-indexed element of the array.

Share this post


Link to post
Share on other sites
To clarify, the algorithm is basically the one I described. But, yes, I forgot that algorithm was one of the ones included. I was thinking it wasn''t (due to a misinformed discussion elsewhere.) Anyways, that''s your best bet. (Thanks, Stoffel)

-Brian

Share this post


Link to post
Share on other sites
If you want, you might consider instead of doing a random shuffle, do a "perfect" shuffle. That is take the top half the cards and map them in order to all the even slots and then take the bottom half the the cards and map them in order to all the odd slots. Then give the player shuffling options like: "seven perfect shuffles with cut". You can have the dealer cheat by things like not alternating bottoms, etc.

Share this post


Link to post
Share on other sites
Isn''t it true that if you perfectly shuffle some N number of times, all the cards will be back in the same order? I think I recall that from one of the gambling movies I saw.

"There are two kinds of people in this world: people with a loaded gun, and people who dig. You dig."

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!