Card Games
Has anybody here made a card game: If so, what is a good way to shuffle your deck?
---Ranok---
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
-Brian
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:
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;<br> random_shuffle (cards, cards + 52); // ta da<br>}<br> </pre> <br><br>The actual shuffle algorithm used is to proceed through the array once and swap each element with a randomly-indexed element of the array.<br><br>
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
-Brian
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.
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."
"There are two kinds of people in this world: people with a loaded gun, and people who dig. You dig."
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement