integrate with dm
[unix-history] / usr / src / games / cribbage / cards.c
CommitLineData
6cca9b39
KM
1/*
2 * Copyright (c) 1980 Regents of the University of California.
3 * All rights reserved. The Berkeley software License Agreement
4 * specifies the terms and conditions for redistribution.
5 */
6
7#ifndef lint
8static char sccsid[] = "@(#)cards.c 5.1 (Berkeley) %G%";
9#endif not lint
f6684a08
KA
10
11#include <stdio.h>
12#include "deck.h"
13
14
15/*
16 * initialize a deck of cards to contain one of each type
17 */
18
19makedeck( d )
20
21 CARD d[];
22{
23 register int i, j, k;
24 long time();
25
26 i = time( (long *) 0 );
27 i = ( (i&0xff) << 8 ) | ( (i >> 8)&0xff ) | 1;
28 srand( i );
29 k = 0;
30 for( i = 0; i < RANKS; i++ ) {
31 for( j = 0; j < SUITS; j++ ) {
32 d[k].suit = j;
33 d[k++].rank = i;
34 }
35 }
36}
37
38
39
40/*
41 * given a deck of cards, shuffle it -- i.e. randomize it
42 * see Knuth, vol. 2, page 125
43 */
44
45shuffle( d )
46
47 CARD d[];
48{
49 register int j, k;
50 CARD c;
51
52 for( j = CARDS; j > 0; --j ) {
53 k = ( rand() >> 4 ) % j; /* random 0 <= k < j */
54 c = d[j - 1]; /* exchange (j - 1) and k */
55 d[j - 1] = d[k];
56 d[k] = c;
57 }
58}
59
60
61
62/*
63 * return true if the two cards are equal...
64 */
65
66eq( a, b )
67
68 CARD a, b;
69{
70 return( ( a.rank == b.rank ) && ( a.suit == b.suit ) );
71}
72
73
74
75/*
76 * isone returns TRUE if a is in the set of cards b
77 */
78
79isone( a, b, n )
80
81 CARD a, b[];
82 int n;
83{
84 register int i;
85
86 for( i = 0; i < n; i++ ) {
87 if( eq( a, b[i] ) ) return( TRUE );
88 }
89 return( FALSE );
90}
91
92
93
94/*
95 * remove the card a from the deck d of n cards
96 */
97
98remove( a, d, n )
99
100 CARD a, d[];
101 int n;
102{
103 register int i, j;
104
105 j = 0;
106 for( i = 0; i < n; i++ ) {
107 if( !eq( a, d[i] ) ) d[j++] = d[i];
108 }
5836528f 109 if( j < n ) d[j].suit = d[j].rank = EMPTY;
f6684a08
KA
110}
111
112
113
114/*
f0a7338d
KA
115 * sorthand:
116 * Sort a hand of n cards
f6684a08 117 */
f0a7338d
KA
118sorthand(h, n)
119register CARD h[];
120int n;
f6684a08 121{
f0a7338d 122 register CARD *cp, *endp;
f6684a08
KA
123 CARD c;
124
f0a7338d
KA
125 for (endp = &h[n]; h < endp - 1; h++)
126 for (cp = h + 1; cp < endp; cp++)
127 if ((cp->rank < h->rank) ||
128 (cp->rank == h->rank && cp->suit < h->suit)) {
129 c = *h;
130 *h = *cp;
131 *cp = c;
f6684a08 132 }
f6684a08
KA
133}
134