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