159264Sbostic /*-
2*60777Sbostic * Copyright (c) 1980, 1993
3*60777Sbostic * The Regents of the University of California. All rights reserved.
433707Sbostic *
542575Sbostic * %sccs.include.redist.c%
621503Smckusick */
77705Sarnold
821503Smckusick #ifndef lint
9*60777Sbostic static char sccsid[] = "@(#)cards.c 8.1 (Berkeley) 05/31/93";
1033707Sbostic #endif /* not lint */
1121503Smckusick
1259264Sbostic #include <curses.h>
1359264Sbostic #include <stdio.h>
1459264Sbostic #include <stdlib.h>
1559264Sbostic #include <time.h>
167705Sarnold
1759264Sbostic #include "deck.h"
1859264Sbostic #include "cribbage.h"
197705Sarnold
2059264Sbostic
217705Sarnold /*
2259264Sbostic * Initialize a deck of cards to contain one of each type.
237705Sarnold */
2459264Sbostic void
makedeck(d)2559264Sbostic makedeck(d)
2659264Sbostic CARD d[];
277705Sarnold {
2859264Sbostic register int i, j, k;
297705Sarnold
3059264Sbostic i = time(NULL);
3159264Sbostic i = ((i & 0xff) << 8) | ((i >> 8) & 0xff) | 1;
3259264Sbostic srand(i);
337705Sarnold k = 0;
3459264Sbostic for (i = 0; i < RANKS; i++)
3559264Sbostic for (j = 0; j < SUITS; j++) {
3659264Sbostic d[k].suit = j;
3759264Sbostic d[k++].rank = i;
3859264Sbostic }
397705Sarnold }
407705Sarnold
417705Sarnold /*
4259264Sbostic * Given a deck of cards, shuffle it -- i.e. randomize it
4359264Sbostic * see Knuth, vol. 2, page 125.
447705Sarnold */
4559264Sbostic void
shuffle(d)4659264Sbostic shuffle(d)
4759264Sbostic CARD d[];
487705Sarnold {
4959264Sbostic register int j, k;
5059264Sbostic CARD c;
517705Sarnold
5259264Sbostic for (j = CARDS; j > 0; --j) {
5359264Sbostic k = (rand() >> 4) % j; /* random 0 <= k < j */
5459264Sbostic c = d[j - 1]; /* exchange (j - 1) and k */
5559264Sbostic d[j - 1] = d[k];
5659264Sbostic d[k] = c;
577705Sarnold }
587705Sarnold }
597705Sarnold
607705Sarnold /*
617705Sarnold * return true if the two cards are equal...
627705Sarnold */
6359264Sbostic int
eq(a,b)6459264Sbostic eq(a, b)
6559264Sbostic CARD a, b;
667705Sarnold {
6759264Sbostic return ((a.rank == b.rank) && (a.suit == b.suit));
687705Sarnold }
697705Sarnold
707705Sarnold /*
717705Sarnold * isone returns TRUE if a is in the set of cards b
727705Sarnold */
7359264Sbostic int
isone(a,b,n)7459264Sbostic isone(a, b, n)
7559264Sbostic CARD a, b[];
7659264Sbostic int n;
777705Sarnold {
7859264Sbostic register int i;
797705Sarnold
8059264Sbostic for (i = 0; i < n; i++)
8159264Sbostic if (eq(a, b[i]))
8259264Sbostic return (TRUE);
8359264Sbostic return (FALSE);
847705Sarnold }
857705Sarnold
867705Sarnold /*
877705Sarnold * remove the card a from the deck d of n cards
887705Sarnold */
8959264Sbostic void
cremove(a,d,n)9059264Sbostic cremove(a, d, n)
9159264Sbostic CARD a, d[];
9259264Sbostic int n;
937705Sarnold {
9459264Sbostic register int i, j;
957705Sarnold
9659264Sbostic for (i = j = 0; i < n; i++)
9759264Sbostic if (!eq(a, d[i]))
9859264Sbostic d[j++] = d[i];
9959264Sbostic if (j < n)
10059264Sbostic d[j].suit = d[j].rank = EMPTY;
1017705Sarnold }
1027705Sarnold
1037705Sarnold /*
1048008Sarnold * sorthand:
1058008Sarnold * Sort a hand of n cards
1067705Sarnold */
10759264Sbostic void
sorthand(h,n)1088008Sarnold sorthand(h, n)
10959264Sbostic register CARD h[];
11059264Sbostic int n;
1117705Sarnold {
11259264Sbostic register CARD *cp, *endp;
11359264Sbostic CARD c;
1147705Sarnold
1158008Sarnold for (endp = &h[n]; h < endp - 1; h++)
11659264Sbostic for (cp = h + 1; cp < endp; cp++)
11759264Sbostic if ((cp->rank < h->rank) ||
11859264Sbostic (cp->rank == h->rank && cp->suit < h->suit)) {
11959264Sbostic c = *h;
12059264Sbostic *h = *cp;
12159264Sbostic *cp = c;
12259264Sbostic }
1237705Sarnold }
124