Card Library?

I looked on the library page and didn’t see one, but does anyone have a
library/GPL’d code for creating card games? (Solitaire, blackjack, etc).
Something that handles the shuffling, displaying, etc of a deck of cards?

Thanks in advance,
Buck========================================================
There is no spoon.
ICQ UIN:48214438
Wesley (Buck) Lemke
http://bucklemke.com
mailto:buck at bucklemke.com

Card games are all so different from each other. I guess, though, what
you want is some kind of ‘sprite library’ style library, but specifically
for cards (eg, it would have blitting, drag-and-drop, and that’s about it)

As for shuffling, I usually do something lame like:

int deck[52];
int used[52];

for (i = 0; i < 52; i++)
used[i] = 0;

for (i = 0; i < 52; i++)
{
do
{
pick = (rand() % 52);
}
while (used[pick]);

card[i] = pick;
}

What you’ll end up with is an array (“deck[]”) of numbers representing
different cards. (How they’re represented by you is up to you, but
it’s simple enough to go ‘A 2 3 4 5 6 7 8 9 T J Q K’ for each of the
four suits.)

In other words:

suit = deck[which] / 13; /* 0, 1, 2 or 3 /
value = deck[which] % 13; /
0, 1, 2 … 12 */

Then you could do something like:

printf(“The %s of %s”, value_names[value], suit_names[suit]);

and have a pair of const. string arrays:

char * value_names[13] = {
“Ace, 2, 3, 4, 5, 6, 7, 8, 9, Ten, Jack, Queen, King”
};

char * suit_names[4] = {
“Spades, Diamonds, Clubs, Hearts”
};

Enjoy!

-bill!On Mon, Sep 10, 2001 at 10:19:08AM -0500, Wesley Lemke wrote:

I looked on the library page and didn’t see one, but does anyone have a
library/GPL’d code for creating card games? (Solitaire, blackjack, etc).
Something that handles the shuffling, displaying, etc of a deck of cards?

William Kendrick wrote:

for (i = 0; i < 52; i++)
used[i] = 0;

for (i = 0; i < 52; i++)
{
do
{
pick = (rand() % 52);
}
while (used[pick]);

card[i] = pick;
}

you’ve forgot an assignment to used[i] somewhere. Even correcting that
you are left with an O(N^2) algorithm (on the average; worst case is
that it never terminates, if your random number generator is good
enough). Use the standard array shuffle algorithm instead (which is
O(N))

Anyway this is not the place to discuss card games implementations
(the right place is somewhere in outer Mongolia. Card games invariably
suck)