Annotation of 43BSDReno/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.
                      4:  *
                      5:  * Redistribution and use in source and binary forms are permitted
                      6:  * provided that: (1) source distributions retain this entire copyright
                      7:  * notice and comment, and (2) distributions including binaries display
                      8:  * the following acknowledgement:  ``This product includes software
                      9:  * developed by the University of California, Berkeley and its contributors''
                     10:  * in the documentation or other materials provided with the distribution
                     11:  * and in all advertising materials mentioning features or use of this
                     12:  * software. Neither the name of the University nor the names of its
                     13:  * contributors may be used to endorse or promote products derived
                     14:  * from this software without specific prior written permission.
                     15:  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
                     16:  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
                     17:  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
                     18:  */
                     19: 
                     20: #ifndef lint
                     21: static char sccsid[] = "@(#)cards.c    5.4 (Berkeley) 6/1/90";
                     22: #endif /* not lint */
                     23: 
                     24: #include       <stdio.h>
                     25: #include       "deck.h"
                     26: 
                     27: 
                     28: /*
                     29:  * initialize a deck of cards to contain one of each type
                     30:  */
                     31: 
                     32: makedeck( d )
                     33: 
                     34:     CARD       d[];
                     35: {
                     36:        register  int           i, j, k;
                     37:        long                    time();
                     38: 
                     39:        i = time( (long *) 0 );
                     40:        i = ( (i&0xff) << 8 ) | ( (i >> 8)&0xff ) | 1;
                     41:        srand( i );
                     42:        k = 0;
                     43:        for( i = 0; i < RANKS; i++ )  {
                     44:            for( j = 0; j < SUITS; j++ )  {
                     45:                d[k].suit = j;
                     46:                d[k++].rank = i;
                     47:            }
                     48:        }
                     49: }
                     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: 
                     58: shuffle( d )
                     59: 
                     60:     CARD       d[];
                     61: {
                     62:        register  int           j, k;
                     63:        CARD                    c;
                     64: 
                     65:        for( j = CARDS; j > 0; --j )  {
                     66:            k = ( rand() >> 4 ) % j;            /* random 0 <= k < j */
                     67:            c = d[j - 1];                       /* exchange (j - 1) and k */
                     68:            d[j - 1] = d[k];
                     69:            d[k] = c;
                     70:        }
                     71: }
                     72: 
                     73: 
                     74: 
                     75: /*
                     76:  * return true if the two cards are equal...
                     77:  */
                     78: 
                     79: eq( a, b )
                     80: 
                     81:     CARD               a, b;
                     82: {
                     83:        return(  ( a.rank == b.rank )  &&  ( a.suit == b.suit )  );
                     84: }
                     85: 
                     86: 
                     87: 
                     88: /*
                     89:  * isone returns TRUE if a is in the set of cards b
                     90:  */
                     91: 
                     92: isone( a, b, n )
                     93: 
                     94:     CARD               a, b[];
                     95:     int                        n;
                     96: {
                     97:        register  int           i;
                     98: 
                     99:        for( i = 0; i < n; i++ )  {
                    100:            if(  eq( a, b[i] )   )  return( TRUE );
                    101:        }
                    102:        return( FALSE );
                    103: }
                    104: 
                    105: 
                    106: 
                    107: /*
                    108:  * remove the card a from the deck d of n cards
                    109:  */
                    110: 
                    111: remove( a, d, n )
                    112: 
                    113:     CARD               a, d[];
                    114:     int                        n;
                    115: {
                    116:        register  int           i, j;
                    117: 
                    118:        j = 0;
                    119:        for( i = 0; i < n; i++ )  {
                    120:            if(  !eq( a, d[i] )  )  d[j++] = d[i];
                    121:        }
                    122:        if(  j < n  )  d[j].suit = d[j].rank = EMPTY;
                    123: }
                    124: 
                    125: 
                    126: 
                    127: /*
                    128:  * sorthand:
                    129:  *     Sort a hand of n cards
                    130:  */
                    131: sorthand(h, n)
                    132: register CARD          h[];
                    133: int                    n;
                    134: {
                    135:        register CARD           *cp, *endp;
                    136:        CARD                    c;
                    137: 
                    138:        for (endp = &h[n]; h < endp - 1; h++)
                    139:            for (cp = h + 1; cp < endp; cp++)
                    140:                if ((cp->rank < h->rank) ||
                    141:                     (cp->rank == h->rank && cp->suit < h->suit)) {
                    142:                    c = *h;
                    143:                    *h = *cp;
                    144:                    *cp = c;
                    145:                }
                    146: }
                    147: 

unix.superglobalmegacorp.com

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