Commit | Line | Data |
---|---|---|
6cca9b39 KM |
1 | /* |
2 | * Copyright (c) 1980 Regents of the University of California. | |
bf870064 KB |
3 | * All rights reserved. |
4 | * | |
5 | * Redistribution and use in source and binary forms are permitted | |
b8c620d6 KB |
6 | * provided that the above copyright notice and this paragraph are |
7 | * duplicated in all such forms and that any documentation, | |
8 | * advertising materials, and other materials related to such | |
9 | * distribution and use acknowledge that the software was developed | |
10 | * by the University of California, Berkeley. The name of the | |
11 | * University may not be used to endorse or promote products derived | |
12 | * from this software without specific prior written permission. | |
13 | * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR | |
14 | * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED | |
15 | * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE. | |
6cca9b39 KM |
16 | */ |
17 | ||
18 | #ifndef lint | |
b8c620d6 | 19 | static char sccsid[] = "@(#)cards.c 5.3 (Berkeley) %G%"; |
bf870064 | 20 | #endif /* not lint */ |
f6684a08 KA |
21 | |
22 | #include <stdio.h> | |
23 | #include "deck.h" | |
24 | ||
25 | ||
26 | /* | |
27 | * initialize a deck of cards to contain one of each type | |
28 | */ | |
29 | ||
30 | makedeck( d ) | |
31 | ||
32 | CARD d[]; | |
33 | { | |
34 | register int i, j, k; | |
35 | long time(); | |
36 | ||
37 | i = time( (long *) 0 ); | |
38 | i = ( (i&0xff) << 8 ) | ( (i >> 8)&0xff ) | 1; | |
39 | srand( i ); | |
40 | k = 0; | |
41 | for( i = 0; i < RANKS; i++ ) { | |
42 | for( j = 0; j < SUITS; j++ ) { | |
43 | d[k].suit = j; | |
44 | d[k++].rank = i; | |
45 | } | |
46 | } | |
47 | } | |
48 | ||
49 | ||
50 | ||
51 | /* | |
52 | * given a deck of cards, shuffle it -- i.e. randomize it | |
53 | * see Knuth, vol. 2, page 125 | |
54 | */ | |
55 | ||
56 | shuffle( d ) | |
57 | ||
58 | CARD d[]; | |
59 | { | |
60 | register int j, k; | |
61 | CARD c; | |
62 | ||
63 | for( j = CARDS; j > 0; --j ) { | |
64 | k = ( rand() >> 4 ) % j; /* random 0 <= k < j */ | |
65 | c = d[j - 1]; /* exchange (j - 1) and k */ | |
66 | d[j - 1] = d[k]; | |
67 | d[k] = c; | |
68 | } | |
69 | } | |
70 | ||
71 | ||
72 | ||
73 | /* | |
74 | * return true if the two cards are equal... | |
75 | */ | |
76 | ||
77 | eq( a, b ) | |
78 | ||
79 | CARD a, b; | |
80 | { | |
81 | return( ( a.rank == b.rank ) && ( a.suit == b.suit ) ); | |
82 | } | |
83 | ||
84 | ||
85 | ||
86 | /* | |
87 | * isone returns TRUE if a is in the set of cards b | |
88 | */ | |
89 | ||
90 | isone( a, b, n ) | |
91 | ||
92 | CARD a, b[]; | |
93 | int n; | |
94 | { | |
95 | register int i; | |
96 | ||
97 | for( i = 0; i < n; i++ ) { | |
98 | if( eq( a, b[i] ) ) return( TRUE ); | |
99 | } | |
100 | return( FALSE ); | |
101 | } | |
102 | ||
103 | ||
104 | ||
105 | /* | |
106 | * remove the card a from the deck d of n cards | |
107 | */ | |
108 | ||
109 | remove( a, d, n ) | |
110 | ||
111 | CARD a, d[]; | |
112 | int n; | |
113 | { | |
114 | register int i, j; | |
115 | ||
116 | j = 0; | |
117 | for( i = 0; i < n; i++ ) { | |
118 | if( !eq( a, d[i] ) ) d[j++] = d[i]; | |
119 | } | |
5836528f | 120 | if( j < n ) d[j].suit = d[j].rank = EMPTY; |
f6684a08 KA |
121 | } |
122 | ||
123 | ||
124 | ||
125 | /* | |
f0a7338d KA |
126 | * sorthand: |
127 | * Sort a hand of n cards | |
f6684a08 | 128 | */ |
f0a7338d KA |
129 | sorthand(h, n) |
130 | register CARD h[]; | |
131 | int n; | |
f6684a08 | 132 | { |
f0a7338d | 133 | register CARD *cp, *endp; |
f6684a08 KA |
134 | CARD c; |
135 | ||
f0a7338d KA |
136 | for (endp = &h[n]; h < endp - 1; h++) |
137 | for (cp = h + 1; cp < endp; cp++) | |
138 | if ((cp->rank < h->rank) || | |
139 | (cp->rank == h->rank && cp->suit < h->suit)) { | |
140 | c = *h; | |
141 | *h = *cp; | |
142 | *cp = c; | |
f6684a08 | 143 | } |
f6684a08 KA |
144 | } |
145 |