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