NetBSD/games/cribbage/cards.c

158 lines
3.6 KiB
C
Raw Normal View History

/* $NetBSD: cards.c,v 1.6 1999/09/30 18:01:32 jsm Exp $ */
1995-03-21 18:03:38 +03:00
/*-
* Copyright (c) 1980, 1993
* The Regents of the University of California. All rights reserved.
1993-03-21 12:45:37 +03:00
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the University of
* California, Berkeley and its contributors.
* 4. Neither the name of the University nor the names of its contributors
* may be used to endorse or promote products derived from this software
* without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
* ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE.
*/
1997-10-10 16:26:34 +04:00
#include <sys/cdefs.h>
1993-03-21 12:45:37 +03:00
#ifndef lint
1997-10-10 16:26:34 +04:00
#if 0
1995-03-21 18:03:38 +03:00
static char sccsid[] = "@(#)cards.c 8.1 (Berkeley) 5/31/93";
1997-10-10 16:26:34 +04:00
#else
__RCSID("$NetBSD: cards.c,v 1.6 1999/09/30 18:01:32 jsm Exp $");
1997-10-10 16:26:34 +04:00
#endif
1993-03-21 12:45:37 +03:00
#endif /* not lint */
1995-03-21 18:03:38 +03:00
#include <curses.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "deck.h"
#include "cribbage.h"
1993-03-21 12:45:37 +03:00
/*
1995-03-21 18:03:38 +03:00
* Initialize a deck of cards to contain one of each type.
1993-03-21 12:45:37 +03:00
*/
1995-03-21 18:03:38 +03:00
void
makedeck(d)
CARD d[];
1993-03-21 12:45:37 +03:00
{
1997-10-10 16:26:34 +04:00
int i, j, k;
1993-03-21 12:45:37 +03:00
1995-03-21 18:03:38 +03:00
i = time(NULL);
i = ((i & 0xff) << 8) | ((i >> 8) & 0xff) | 1;
srand(i);
1993-03-21 12:45:37 +03:00
k = 0;
1995-03-21 18:03:38 +03:00
for (i = 0; i < RANKS; i++)
for (j = 0; j < SUITS; j++) {
d[k].suit = j;
d[k++].rank = i;
}
1993-03-21 12:45:37 +03:00
}
/*
1995-03-21 18:03:38 +03:00
* Given a deck of cards, shuffle it -- i.e. randomize it
* see Knuth, vol. 2, page 125.
1993-03-21 12:45:37 +03:00
*/
1995-03-21 18:03:38 +03:00
void
shuffle(d)
CARD d[];
1993-03-21 12:45:37 +03:00
{
1997-10-10 16:26:34 +04:00
int j, k;
1995-03-21 18:03:38 +03:00
CARD c;
for (j = CARDS; j > 0; --j) {
k = (rand() >> 4) % j; /* random 0 <= k < j */
c = d[j - 1]; /* exchange (j - 1) and k */
d[j - 1] = d[k];
d[k] = c;
1993-03-21 12:45:37 +03:00
}
}
/*
* return true if the two cards are equal...
*/
1995-03-21 18:03:38 +03:00
int
eq(a, b)
CARD a, b;
1993-03-21 12:45:37 +03:00
{
1995-03-21 18:03:38 +03:00
return ((a.rank == b.rank) && (a.suit == b.suit));
1993-03-21 12:45:37 +03:00
}
/*
* is_one returns TRUE if a is in the set of cards b
1993-03-21 12:45:37 +03:00
*/
1995-03-21 18:03:38 +03:00
int
is_one(a, b, n)
CARD a;
const CARD b[];
1995-03-21 18:03:38 +03:00
int n;
1993-03-21 12:45:37 +03:00
{
1997-10-10 16:26:34 +04:00
int i;
1993-03-21 12:45:37 +03:00
1995-03-21 18:03:38 +03:00
for (i = 0; i < n; i++)
if (eq(a, b[i]))
return (TRUE);
return (FALSE);
1993-03-21 12:45:37 +03:00
}
/*
* remove the card a from the deck d of n cards
*/
1995-03-21 18:03:38 +03:00
void
cremove(a, d, n)
CARD a, d[];
int n;
1993-03-21 12:45:37 +03:00
{
1997-10-10 16:26:34 +04:00
int i, j;
1993-03-21 12:45:37 +03:00
1995-03-21 18:03:38 +03:00
for (i = j = 0; i < n; i++)
if (!eq(a, d[i]))
d[j++] = d[i];
if (j < n)
d[j].suit = d[j].rank = EMPTY;
1993-03-21 12:45:37 +03:00
}
/*
* sorthand:
* Sort a hand of n cards
*/
1995-03-21 18:03:38 +03:00
void
1993-03-21 12:45:37 +03:00
sorthand(h, n)
1997-10-10 16:26:34 +04:00
CARD h[];
1995-03-21 18:03:38 +03:00
int n;
1993-03-21 12:45:37 +03:00
{
1997-10-10 16:26:34 +04:00
CARD *cp, *endp;
1995-03-21 18:03:38 +03:00
CARD c;
1993-03-21 12:45:37 +03:00
for (endp = &h[n]; h < endp - 1; h++)
1995-03-21 18:03:38 +03:00
for (cp = h + 1; cp < endp; cp++)
if ((cp->rank < h->rank) ||
(cp->rank == h->rank && cp->suit < h->suit)) {
c = *h;
*h = *cp;
*cp = c;
}
1993-03-21 12:45:37 +03:00
}