training

Code I wrote during training
git clone git://git.bitsmanent.org/training
Log | Files | Refs | README

poker_mod.c (1682B)


      1 /* Fig. 7.24: fig07_24.c
      2    Card shuffling dealing program */
      3 #include <stdio.h>
      4 #include <stdlib.h>
      5 #include <time.h>
      6 
      7 /* prototypes */
      8 void shuffleAndDeal( int wDeck[][ 13 ], int n_cards,
      9               const char *wFace[], const char *wSuit[] );
     10 
     11 int main(void)
     12 {
     13    /* initialize suit array */
     14    const char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };
     15    
     16    /* initialize face array */
     17    const char *face[ 13 ] = 
     18       { "Ace", "Deuce", "Three", "Four", 
     19         "Five", "Six", "Seven", "Eight",
     20         "Nine", "Ten", "Jack", "Queen", "King" };
     21 
     22    /* initialize deck array */
     23    int deck[ 4 ][ 13 ] = { { 0 } };
     24 
     25    srand( time( 0 ) ); /* seed random-number generator */
     26 
     27    shuffleAndDeal( deck, 5, face, suit );
     28 
     29    return 0; /* indicates successful termination */
     30 
     31 } /* end main */
     32 
     33 
     34 
     35 /* shuffle and deal cards in deck */
     36 void shuffleAndDeal( int wDeck[][ 13 ], int n_cards,
     37               const char *wFace[], const char *wSuit[] )
     38 {
     39    int row;    /* row number */
     40    int column; /* column number */
     41    int card;   /* counter */
     42 
     43    /* for each of the 52 cards, choose slot of deck randomly */
     44    for ( card = 1; card <= 26; card++ ) {
     45 
     46       /* choose new random location until unoccupied slot found */
     47       do {
     48          row = rand() % 4;
     49          column = rand() % 13;
     50       } while( wDeck[ row ][ column ] != 0 ); /* end do...while */
     51 
     52       /* place card number in chosen slot of deck */
     53       wDeck[ row ][ column ] = card;
     54 
     55       if(n_cards) {
     56          printf( "%5s of %-8s%c", wFace[ column ], wSuit[ row ],
     57             card % 2 == 0 ? '\n' : '\t' );
     58 	 --n_cards;
     59       }
     60 
     61    } /* end for */
     62    printf("\n");
     63 
     64 } /* end function shuffleAndDeal */
     65