空当接龙牌局生成算法

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];
    }


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值