Annotation of 43BSD/games/cribbage/cards.c, revision 1.1.1.1

1.1       root        1: /*
                      2:  * Copyright (c) 1980 Regents of the University of California.
                      3:  * All rights reserved.  The Berkeley software License Agreement
                      4:  * specifies the terms and conditions for redistribution.
                      5:  */
                      6: 
                      7: #ifndef lint
                      8: static char sccsid[] = "@(#)cards.c    5.1 (Berkeley) 5/30/85";
                      9: #endif not lint
                     10: 
                     11: #include       <stdio.h>
                     12: #include       "deck.h"
                     13: 
                     14: 
                     15: /*
                     16:  * initialize a deck of cards to contain one of each type
                     17:  */
                     18: 
                     19: makedeck( d )
                     20: 
                     21:     CARD       d[];
                     22: {
                     23:        register  int           i, j, k;
                     24:        long                    time();
                     25: 
                     26:        i = time( (long *) 0 );
                     27:        i = ( (i&0xff) << 8 ) | ( (i >> 8)&0xff ) | 1;
                     28:        srand( i );
                     29:        k = 0;
                     30:        for( i = 0; i < RANKS; i++ )  {
                     31:            for( j = 0; j < SUITS; j++ )  {
                     32:                d[k].suit = j;
                     33:                d[k++].rank = i;
                     34:            }
                     35:        }
                     36: }
                     37: 
                     38: 
                     39: 
                     40: /*
                     41:  * given a deck of cards, shuffle it -- i.e. randomize it
                     42:  * see Knuth, vol. 2, page 125
                     43:  */
                     44: 
                     45: shuffle( d )
                     46: 
                     47:     CARD       d[];
                     48: {
                     49:        register  int           j, k;
                     50:        CARD                    c;
                     51: 
                     52:        for( j = CARDS; j > 0; --j )  {
                     53:            k = ( rand() >> 4 ) % j;            /* random 0 <= k < j */
                     54:            c = d[j - 1];                       /* exchange (j - 1) and k */
                     55:            d[j - 1] = d[k];
                     56:            d[k] = c;
                     57:        }
                     58: }
                     59: 
                     60: 
                     61: 
                     62: /*
                     63:  * return true if the two cards are equal...
                     64:  */
                     65: 
                     66: eq( a, b )
                     67: 
                     68:     CARD               a, b;
                     69: {
                     70:        return(  ( a.rank == b.rank )  &&  ( a.suit == b.suit )  );
                     71: }
                     72: 
                     73: 
                     74: 
                     75: /*
                     76:  * isone returns TRUE if a is in the set of cards b
                     77:  */
                     78: 
                     79: isone( a, b, n )
                     80: 
                     81:     CARD               a, b[];
                     82:     int                        n;
                     83: {
                     84:        register  int           i;
                     85: 
                     86:        for( i = 0; i < n; i++ )  {
                     87:            if(  eq( a, b[i] )   )  return( TRUE );
                     88:        }
                     89:        return( FALSE );
                     90: }
                     91: 
                     92: 
                     93: 
                     94: /*
                     95:  * remove the card a from the deck d of n cards
                     96:  */
                     97: 
                     98: remove( a, d, n )
                     99: 
                    100:     CARD               a, d[];
                    101:     int                        n;
                    102: {
                    103:        register  int           i, j;
                    104: 
                    105:        j = 0;
                    106:        for( i = 0; i < n; i++ )  {
                    107:            if(  !eq( a, d[i] )  )  d[j++] = d[i];
                    108:        }
                    109:        if(  j < n  )  d[j].suit = d[j].rank = EMPTY;
                    110: }
                    111: 
                    112: 
                    113: 
                    114: /*
                    115:  * sorthand:
                    116:  *     Sort a hand of n cards
                    117:  */
                    118: sorthand(h, n)
                    119: register CARD          h[];
                    120: int                    n;
                    121: {
                    122:        register CARD           *cp, *endp;
                    123:        CARD                    c;
                    124: 
                    125:        for (endp = &h[n]; h < endp - 1; h++)
                    126:            for (cp = h + 1; cp < endp; cp++)
                    127:                if ((cp->rank < h->rank) ||
                    128:                     (cp->rank == h->rank && cp->suit < h->suit)) {
                    129:                    c = *h;
                    130:                    *h = *cp;
                    131:                    *cp = c;
                    132:                }
                    133: }
                    134: 

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.