cards.c revision 1.3 1 1.3 cgd /* $NetBSD: cards.c,v 1.3 1995/03/21 15:08:41 cgd Exp $ */
2 1.3 cgd
3 1.3 cgd /*-
4 1.3 cgd * Copyright (c) 1980, 1993
5 1.3 cgd * The Regents of the University of California. All rights reserved.
6 1.1 cgd *
7 1.1 cgd * Redistribution and use in source and binary forms, with or without
8 1.1 cgd * modification, are permitted provided that the following conditions
9 1.1 cgd * are met:
10 1.1 cgd * 1. Redistributions of source code must retain the above copyright
11 1.1 cgd * notice, this list of conditions and the following disclaimer.
12 1.1 cgd * 2. Redistributions in binary form must reproduce the above copyright
13 1.1 cgd * notice, this list of conditions and the following disclaimer in the
14 1.1 cgd * documentation and/or other materials provided with the distribution.
15 1.1 cgd * 3. All advertising materials mentioning features or use of this software
16 1.1 cgd * must display the following acknowledgement:
17 1.1 cgd * This product includes software developed by the University of
18 1.1 cgd * California, Berkeley and its contributors.
19 1.1 cgd * 4. Neither the name of the University nor the names of its contributors
20 1.1 cgd * may be used to endorse or promote products derived from this software
21 1.1 cgd * without specific prior written permission.
22 1.1 cgd *
23 1.1 cgd * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 1.1 cgd * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 1.1 cgd * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 1.1 cgd * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 1.1 cgd * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 1.1 cgd * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 1.1 cgd * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 1.1 cgd * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 1.1 cgd * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 1.1 cgd * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 1.1 cgd * SUCH DAMAGE.
34 1.1 cgd */
35 1.1 cgd
36 1.1 cgd #ifndef lint
37 1.3 cgd static char sccsid[] = "@(#)cards.c 8.1 (Berkeley) 5/31/93";
38 1.1 cgd #endif /* not lint */
39 1.1 cgd
40 1.3 cgd #include <curses.h>
41 1.3 cgd #include <stdio.h>
42 1.3 cgd #include <stdlib.h>
43 1.3 cgd #include <time.h>
44 1.3 cgd
45 1.3 cgd #include "deck.h"
46 1.3 cgd #include "cribbage.h"
47 1.1 cgd
48 1.1 cgd
49 1.1 cgd /*
50 1.3 cgd * Initialize a deck of cards to contain one of each type.
51 1.1 cgd */
52 1.3 cgd void
53 1.3 cgd makedeck(d)
54 1.3 cgd CARD d[];
55 1.1 cgd {
56 1.3 cgd register int i, j, k;
57 1.1 cgd
58 1.3 cgd i = time(NULL);
59 1.3 cgd i = ((i & 0xff) << 8) | ((i >> 8) & 0xff) | 1;
60 1.3 cgd srand(i);
61 1.1 cgd k = 0;
62 1.3 cgd for (i = 0; i < RANKS; i++)
63 1.3 cgd for (j = 0; j < SUITS; j++) {
64 1.3 cgd d[k].suit = j;
65 1.3 cgd d[k++].rank = i;
66 1.3 cgd }
67 1.1 cgd }
68 1.1 cgd
69 1.1 cgd /*
70 1.3 cgd * Given a deck of cards, shuffle it -- i.e. randomize it
71 1.3 cgd * see Knuth, vol. 2, page 125.
72 1.1 cgd */
73 1.3 cgd void
74 1.3 cgd shuffle(d)
75 1.3 cgd CARD d[];
76 1.3 cgd {
77 1.3 cgd register int j, k;
78 1.3 cgd CARD c;
79 1.3 cgd
80 1.3 cgd for (j = CARDS; j > 0; --j) {
81 1.3 cgd k = (rand() >> 4) % j; /* random 0 <= k < j */
82 1.3 cgd c = d[j - 1]; /* exchange (j - 1) and k */
83 1.3 cgd d[j - 1] = d[k];
84 1.3 cgd d[k] = c;
85 1.1 cgd }
86 1.1 cgd }
87 1.1 cgd
88 1.1 cgd /*
89 1.1 cgd * return true if the two cards are equal...
90 1.1 cgd */
91 1.3 cgd int
92 1.3 cgd eq(a, b)
93 1.3 cgd CARD a, b;
94 1.1 cgd {
95 1.3 cgd return ((a.rank == b.rank) && (a.suit == b.suit));
96 1.1 cgd }
97 1.1 cgd
98 1.1 cgd /*
99 1.1 cgd * isone returns TRUE if a is in the set of cards b
100 1.1 cgd */
101 1.3 cgd int
102 1.3 cgd isone(a, b, n)
103 1.3 cgd CARD a, b[];
104 1.3 cgd int n;
105 1.3 cgd {
106 1.3 cgd register int i;
107 1.3 cgd
108 1.3 cgd for (i = 0; i < n; i++)
109 1.3 cgd if (eq(a, b[i]))
110 1.3 cgd return (TRUE);
111 1.3 cgd return (FALSE);
112 1.1 cgd }
113 1.1 cgd
114 1.1 cgd /*
115 1.1 cgd * remove the card a from the deck d of n cards
116 1.1 cgd */
117 1.3 cgd void
118 1.3 cgd cremove(a, d, n)
119 1.3 cgd CARD a, d[];
120 1.3 cgd int n;
121 1.3 cgd {
122 1.3 cgd register int i, j;
123 1.3 cgd
124 1.3 cgd for (i = j = 0; i < n; i++)
125 1.3 cgd if (!eq(a, d[i]))
126 1.3 cgd d[j++] = d[i];
127 1.3 cgd if (j < n)
128 1.3 cgd d[j].suit = d[j].rank = EMPTY;
129 1.1 cgd }
130 1.1 cgd
131 1.1 cgd /*
132 1.1 cgd * sorthand:
133 1.1 cgd * Sort a hand of n cards
134 1.1 cgd */
135 1.3 cgd void
136 1.1 cgd sorthand(h, n)
137 1.3 cgd register CARD h[];
138 1.3 cgd int n;
139 1.1 cgd {
140 1.3 cgd register CARD *cp, *endp;
141 1.3 cgd CARD c;
142 1.1 cgd
143 1.1 cgd for (endp = &h[n]; h < endp - 1; h++)
144 1.3 cgd for (cp = h + 1; cp < endp; cp++)
145 1.3 cgd if ((cp->rank < h->rank) ||
146 1.3 cgd (cp->rank == h->rank && cp->suit < h->suit)) {
147 1.3 cgd c = *h;
148 1.3 cgd *h = *cp;
149 1.3 cgd *cp = c;
150 1.3 cgd }
151 1.1 cgd }
152