Home | History | Annotate | Line # | Download | only in cribbage
cards.c revision 1.5.2.1
      1  1.5.2.1  wrstuden /*	$NetBSD: cards.c,v 1.5.2.1 1999/12/27 18:28:59 wrstuden 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.4     lukem #include <sys/cdefs.h>
     37      1.1       cgd #ifndef lint
     38      1.4     lukem #if 0
     39      1.3       cgd static char sccsid[] = "@(#)cards.c	8.1 (Berkeley) 5/31/93";
     40      1.4     lukem #else
     41  1.5.2.1  wrstuden __RCSID("$NetBSD: cards.c,v 1.5.2.1 1999/12/27 18:28:59 wrstuden Exp $");
     42      1.4     lukem #endif
     43      1.1       cgd #endif /* not lint */
     44      1.1       cgd 
     45      1.3       cgd #include <curses.h>
     46      1.3       cgd #include <stdio.h>
     47      1.3       cgd #include <stdlib.h>
     48      1.3       cgd #include <time.h>
     49      1.3       cgd 
     50      1.3       cgd #include "deck.h"
     51      1.3       cgd #include "cribbage.h"
     52      1.1       cgd 
     53      1.1       cgd 
     54      1.1       cgd /*
     55      1.3       cgd  * Initialize a deck of cards to contain one of each type.
     56      1.1       cgd  */
     57      1.3       cgd void
     58      1.3       cgd makedeck(d)
     59      1.3       cgd 	CARD    d[];
     60      1.1       cgd {
     61      1.4     lukem 	int i, j, k;
     62      1.1       cgd 
     63      1.3       cgd 	i = time(NULL);
     64      1.3       cgd 	i = ((i & 0xff) << 8) | ((i >> 8) & 0xff) | 1;
     65      1.3       cgd 	srand(i);
     66      1.1       cgd 	k = 0;
     67      1.3       cgd 	for (i = 0; i < RANKS; i++)
     68      1.3       cgd 		for (j = 0; j < SUITS; j++) {
     69      1.3       cgd 			d[k].suit = j;
     70      1.3       cgd 			d[k++].rank = i;
     71      1.3       cgd 		}
     72      1.1       cgd }
     73      1.1       cgd 
     74      1.1       cgd /*
     75      1.3       cgd  * Given a deck of cards, shuffle it -- i.e. randomize it
     76      1.3       cgd  * see Knuth, vol. 2, page 125.
     77      1.1       cgd  */
     78      1.3       cgd void
     79      1.3       cgd shuffle(d)
     80      1.3       cgd 	CARD d[];
     81      1.3       cgd {
     82      1.4     lukem 	int j, k;
     83      1.3       cgd 	CARD c;
     84      1.3       cgd 
     85      1.3       cgd 	for (j = CARDS; j > 0; --j) {
     86      1.3       cgd 		k = (rand() >> 4) % j;		/* random 0 <= k < j */
     87      1.3       cgd 		c = d[j - 1];			/* exchange (j - 1) and k */
     88      1.3       cgd 		d[j - 1] = d[k];
     89      1.3       cgd 		d[k] = c;
     90      1.1       cgd 	}
     91      1.1       cgd }
     92      1.1       cgd 
     93      1.1       cgd /*
     94      1.1       cgd  * return true if the two cards are equal...
     95      1.1       cgd  */
     96      1.3       cgd int
     97      1.3       cgd eq(a, b)
     98      1.3       cgd 	CARD a, b;
     99      1.1       cgd {
    100      1.3       cgd 	return ((a.rank == b.rank) && (a.suit == b.suit));
    101      1.1       cgd }
    102      1.1       cgd 
    103      1.1       cgd /*
    104  1.5.2.1  wrstuden  * is_one returns TRUE if a is in the set of cards b
    105      1.1       cgd  */
    106      1.3       cgd int
    107  1.5.2.1  wrstuden is_one(a, b, n)
    108      1.5       jsm 	CARD a;
    109      1.5       jsm 	const CARD b[];
    110      1.3       cgd 	int n;
    111      1.3       cgd {
    112      1.4     lukem 	int i;
    113      1.3       cgd 
    114      1.3       cgd 	for (i = 0; i < n; i++)
    115      1.3       cgd 		if (eq(a, b[i]))
    116      1.3       cgd 			return (TRUE);
    117      1.3       cgd 	return (FALSE);
    118      1.1       cgd }
    119      1.1       cgd 
    120      1.1       cgd /*
    121      1.1       cgd  * remove the card a from the deck d of n cards
    122      1.1       cgd  */
    123      1.3       cgd void
    124      1.3       cgd cremove(a, d, n)
    125      1.3       cgd 	CARD a, d[];
    126      1.3       cgd 	int n;
    127      1.3       cgd {
    128      1.4     lukem 	int i, j;
    129      1.3       cgd 
    130      1.3       cgd 	for (i = j = 0; i < n; i++)
    131      1.3       cgd 		if (!eq(a, d[i]))
    132      1.3       cgd 			d[j++] = d[i];
    133      1.3       cgd 	if (j < n)
    134      1.3       cgd 		d[j].suit = d[j].rank = EMPTY;
    135      1.1       cgd }
    136      1.1       cgd 
    137      1.1       cgd /*
    138      1.1       cgd  * sorthand:
    139      1.1       cgd  *	Sort a hand of n cards
    140      1.1       cgd  */
    141      1.3       cgd void
    142      1.1       cgd sorthand(h, n)
    143      1.4     lukem 	CARD h[];
    144      1.3       cgd 	int n;
    145      1.1       cgd {
    146      1.4     lukem 	CARD *cp, *endp;
    147      1.3       cgd 	CARD c;
    148      1.1       cgd 
    149      1.1       cgd 	for (endp = &h[n]; h < endp - 1; h++)
    150      1.3       cgd 		for (cp = h + 1; cp < endp; cp++)
    151      1.3       cgd 			if ((cp->rank < h->rank) ||
    152      1.3       cgd 			    (cp->rank == h->rank && cp->suit < h->suit)) {
    153      1.3       cgd 				c = *h;
    154      1.3       cgd 				*h = *cp;
    155      1.3       cgd 				*cp = c;
    156      1.3       cgd 			}
    157      1.1       cgd }
    158