xref: /openbsd/games/cribbage/cards.c (revision 3d8817e4)
1 /*	$OpenBSD: cards.c,v 1.5 2009/10/27 23:59:24 deraadt 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 <curses.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <time.h>
37 
38 #include "deck.h"
39 #include "cribbage.h"
40 
41 
42 /*
43  * Initialize a deck of cards to contain one of each type.
44  */
45 void
46 makedeck(CARD d[])
47 {
48 	int i, j, k;
49 
50 	i = time(NULL);
51 	i = ((i & 0xff) << 8) | ((i >> 8) & 0xff) | 1;
52 	srand(i);
53 	k = 0;
54 	for (i = 0; i < RANKS; i++)
55 		for (j = 0; j < SUITS; j++) {
56 			d[k].suit = j;
57 			d[k++].rank = i;
58 		}
59 }
60 
61 /*
62  * Given a deck of cards, shuffle it -- i.e. randomize it
63  * see Knuth, vol. 2, page 125.
64  */
65 void
66 shuffle(CARD d[])
67 {
68 	int j, k;
69 	CARD c;
70 
71 	for (j = CARDS; j > 0; --j) {
72 		k = (rand() >> 4) % j;		/* random 0 <= k < j */
73 		c = d[j - 1];			/* exchange (j - 1) and k */
74 		d[j - 1] = d[k];
75 		d[k] = c;
76 	}
77 }
78 
79 /*
80  * return true if the two cards are equal...
81  */
82 int
83 eq(CARD a, CARD b)
84 {
85 	return ((a.rank == b.rank) && (a.suit == b.suit));
86 }
87 
88 /*
89  * isone returns TRUE if a is in the set of cards b
90  */
91 int
92 isone(CARD a, CARD b[], int n)
93 {
94 	int i;
95 
96 	for (i = 0; i < n; i++)
97 		if (eq(a, b[i]))
98 			return (TRUE);
99 	return (FALSE);
100 }
101 
102 /*
103  * remove the card a from the deck d of n cards
104  */
105 void
106 cremove(CARD a, CARD d[], int n)
107 {
108 	int i, j;
109 
110 	for (i = j = 0; i < n; i++)
111 		if (!eq(a, d[i]))
112 			d[j++] = d[i];
113 	if (j < n)
114 		d[j].suit = d[j].rank = EMPTY;
115 }
116 
117 /*
118  * sorthand:
119  *	Sort a hand of n cards
120  */
121 void
122 sorthand(CARD h[], int n)
123 {
124 	CARD *cp, *endp;
125 	CARD c;
126 
127 	for (endp = &h[n]; h < endp - 1; h++)
128 		for (cp = h + 1; cp < endp; cp++)
129 			if ((cp->rank < h->rank) ||
130 			    (cp->rank == h->rank && cp->suit < h->suit)) {
131 				c = *h;
132 				*h = *cp;
133 				*cp = c;
134 			}
135 }
136