From: http://www.solitairelaboratory.com/mshuffle.txt
From: jimh@exmsft.com (Jim Horne)
I'm happy to share the card shuffle algorithm, but I warn you,
it does depend on the rand() and srand() function built into MS
compilers. The good news is that I believe these work the same
for all our compilers.
I use cards.dll which has it's own mapping of numbers (0-51) to
cards. The following will give you the idea. Play around with
this and you'll be able to generate all the games.
Go ahead and post the code. People might as well have fun with it.
Please keep me posted on anything interesting that comes of it.
Thanks.
_______________________________________________________________
#define BLACK 0 // COLOUR(card)
#define RED 1
#define ACE 0 // VALUE(card)
#define DEUCE 1
#define CLUB 0 // SUIT(card)
#define DIAMOND 1
#define HEART 2
#define SPADE 3
#define SUIT(card) ((card) % 4)
#define VALUE(card) ((card) / 4)
#define COLOUR(card) (SUIT(card) == DIAMOND || SUIT(card) ==
HEART)
#define MAXPOS 21
#define MAXCOL 9 // includes top row as column 0
CARD card[MAXCOL][MAXPOS]; // current layout of cards, CARDs are
ints
int i, j; // generic counters
int col, pos;
int wLeft = 52; // cards left to be chosen in shuffle
CARD deck[52]; // deck of 52 unique cards
for (col = 0; col < MAXCOL; col++) // clear the deck
for (pos = 0; pos < MAXPOS; pos++)
card[col][pos] = EMPTY;
/* shuffle cards */
for (i = 0; i < 52; i++) // put unique card in each deck loc.
deck[i] = i;
srand(gamenumber); // gamenumber is seed for rand()
for (i = 0; i < 52; i++)
{
j = rand() % wLeft;
card[(i%8)+1][i/8] = deck[j];
deck[j] = deck[--wLeft];
}

6861

被折叠的 条评论
为什么被折叠?



