training

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

poker3.c (21873B)


      1 /* Exercise 7.16 */
      2 
      3 /* Fig. 7.24: fig07_24.c
      4    Card shuffling dealing program */
      5 #include <stdio.h>
      6 #include <stdlib.h>
      7 #include <time.h>
      8 
      9 #define CARDS 5
     10 
     11 /* Points setting *
     12  * NOTE: the points *must* have an offset >= 15 and P_PAIR
     13  *       must should be >= 15 too to works properly. Else,
     14  *	 the game will sh0w only INVALID results and wrong
     15  *	 winners.
     16  */
     17 #define OFFSET 15 
     18 
     19 /* I'll tweak these, maybe.. a day :-) */
     20 #define P_PAIR 		OFFSET
     21 #define P_DOUBLE	P_PAIR + OFFSET
     22 #define P_TRIS		P_DOUBLE + OFFSET
     23 #define P_SCALE		P_TRIS + OFFSET
     24 #define P_FULL		P_SCALE + OFFSET
     25 #define P_POKER		P_FULL + OFFSET
     26 #define P_COLOR		P_POKER + OFFSET
     27 #define P_SFLUSH	P_COLOR + OFFSET
     28 #define P_RFLUSH	P_SFLUSH + OFFSET
     29 
     30 /* prototypes */
     31 void shuffle( int wDeck[][ 13 ] );
     32 
     33 void deal( int wDeck[][ 13 ], const char *wFace[], const char *wSuit[],
     34            int cards[][CARDS][2], int player, int wCards, int show);
     35 
     36 int getp(int cards[][CARDS][2], int player, const char *wFace[],
     37          const char *wSuit[], int n_cards, int *r_suit, int *r_face,
     38 	 int type[], int show);
     39 
     40 void order(int a[][CARDS][2], int p, int size);
     41 void rswap(int *a, int *b);
     42 
     43 int chcs(int cards[][CARDS][2], int player, int type[], const char *wFace[], 
     44          const char *wSuit[], int wDeck[][13], int n_cards);
     45 
     46 int chch(int cards[][CARDS][2], int n_cards, int player, int wDeck[][13],
     47          const char *wFace[], const char *wSuit[], int kind, 
     48 	 int r_face, int r_suit, int show);
     49 
     50 void p_points(int kind, int face, int suit,
     51               const char *wFace[], const char *wSuit[], int show);
     52 
     53 int main(void)
     54 {
     55    int i, type[2], winner[3] = { 0 }, points[2] = { 0 };
     56    int p[2][2] = { { 0 } };
     57    int cards[2][CARDS][2] = { { { 0 } } };
     58    int c; /* change tracer */
     59    int theface[2], thesuit;
     60 
     61    /* initialize suit array */
     62    const char *suit[ 4 ] = { "Hearts", "Diamonds", "Clubs", "Spades" };
     63    
     64    /* initialize face array */
     65    const char *face[ 13 ] = 
     66       { "Ace", "Deuce", "Three", "Four", 
     67         "Five", "Six", "Seven", "Eight",
     68         "Nine", "Ten", "Jack", "Queen", "King" };
     69 
     70    /* initialize deck array */
     71    int deck[ 4 ][ 13 ] = {
     72       { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 },
     73       { 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26 },
     74       { 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39 },
     75       { 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52 }
     76    };
     77 
     78 
     79    if(CARDS > 52 / 2) {
     80       printf("Too many cards: redefine the CARDS constant.\n");
     81       exit(-1);
     82    }
     83 
     84    srand( time( 0 ) ); /* seed random-number generator */
     85 
     86    shuffle( deck );
     87 
     88    /* S t a r t */
     89    printf("GaMe STaRTeD! (%d cards for player)\n\n", CARDS);
     90 
     91    for(i = 0; i < 2; i++) {
     92       printf("*** ");
     93       if(!i)
     94 	 printf("Computer");
     95       else
     96 	 printf("Player %d", i);
     97 
     98       printf("'s cards..\n");
     99 
    100       printf("-----------------------------------------\n\n");
    101 
    102       deal( deck, face, suit, cards, i, CARDS, !i ? 0 : 1 );
    103 
    104       /* p[i][0] == points
    105        * p[i][1] == suit of highest card (whitin the highest kind)
    106        * i == the player
    107        * p[0][?] == the computer 
    108        */
    109       if(i) printf("\n");
    110       p[i][0] = getp(cards, i, face, suit, CARDS, &p[i][1],
    111 		     &theface[i], &type[i], i);
    112 
    113       if(i) {
    114          thesuit = p[1][1]; /* take the user's suit */
    115 
    116          printf("\n: Score: %d ", p[i][0]);
    117 	 printf("(suit points reserved: %d)", 4 - p[i][1]);
    118       }
    119 
    120       printf("\n-----------------------------------------\n");
    121 
    122       /* so */
    123       points[i] = p[i][0] - p[i][1]; /* points - seed(0 highest, 3 lowest) */
    124       printf("\n\n");
    125    }
    126 
    127    printf("Now is time to change the cards..\n");
    128    printf("Press a key to continue.\n");
    129    getchar(); 
    130 
    131    /* Player change the cards */
    132    c = chch(cards, CARDS, 1, deck, face, suit, type[1],
    133 	    theface[1], thesuit, 1);
    134    if(c) {
    135       printf("Player 1 has changed %d %s.\n\n", c, c > 1 ? "cards" : "card" );
    136 
    137       p[1][0] = getp(cards, 1, face, suit, CARDS, &p[1][1],
    138 		     &theface[1], &type[1], 1);
    139    }
    140 
    141    /* Computer change the cards */
    142    c = chcs(cards, 0, &type[0], face, suit, deck, CARDS);
    143    if(c) {
    144       printf("Computer has changed %d %s.\n\n", c, c > 1 ? "cards" : "card" );
    145 
    146       p[0][0] = getp(cards, 0, face, suit, CARDS,
    147 		     &p[0][1], &theface[0], &type[0], 0);
    148    }
    149 
    150    printf("\n\n");
    151 
    152 
    153    /* F i n i s h */
    154 
    155    /* Check the winner and print the classifies */
    156    for(i = 0; i < 2; i++) {
    157       if(p[i][0] > winner[0]) {
    158 	 winner[0] = p[i][0];
    159 	 winner[1] = p[i][1];
    160 	 winner[3] = i;
    161       }
    162       else if(p[i][0] == winner[0]) { /* Pair: check the suit */ 
    163 	 if(p[i][1] < winner[1]) {
    164 	    winner[0] = p[i][0];
    165 	    winner[1] = p[i][1];
    166 	    winner[3] = i;
    167 	 }
    168       }
    169    } /* end for (i) */
    170 
    171    printf("Press a key to know the winner\n"
    172 	  "or type ^C if you have fear :-)\n");
    173 
    174    getchar(); getchar();
    175    printf("\n");
    176 
    177    /* Show the cards */
    178    for(i = 1; i >= 0; i--) {
    179       if(!i)
    180 	 printf("Computer");
    181       else
    182 	 printf("Player 1");
    183       printf("'s cards:\n");
    184 
    185       printf("-------------------------------");
    186       printf("-----------------------------\n");
    187 
    188       for(c = 0; c < CARDS; c++) {
    189          printf("%15s of %-8s", face[cards[i][c][0]], suit[cards[i][c][1]]);
    190          printf("%c", !(c % 2) ? '\n' : '\t');
    191       }
    192 
    193       printf("\n\n");
    194       p_points(type[i], theface[i], p[i][1], face, suit, 1);
    195 
    196       printf("\n\tTotal points: %d (suit %d)\n", p[i][0], p[i][1]);
    197       printf("-------------------------------");
    198       printf("-----------------------------\n");
    199       printf("\n\n");
    200    }
    201 
    202 
    203    printf("\n\tC l a s s i f i e s\n\n");
    204 
    205    printf("%16s%9s\n", "Player", "Points");
    206    printf("%16s%9d\n", "PC", p[0][0]);
    207    printf("%16d%9d\n", 1, p[1][0]);
    208 
    209    printf("\n");
    210    
    211    if(!winner[3])
    212       printf("* Computer won with %d points.\n", winner[0]);
    213    else
    214       printf("* Player %d won with %d points.\n", winner[3], winner[0]);
    215 
    216    printf("\n");
    217    return 0; /* indicates successful termination */
    218 
    219 } /* end main */
    220 
    221 /* shuffle cards in deck */
    222 void shuffle( int wDeck[][ 13 ] )
    223 {
    224    int row;
    225    int column;
    226 
    227    for(row = 0; row < 4; row++) {
    228       for(column = 0; column < 13; column++) {
    229 	 rswap(&wDeck[row][column], &wDeck[rand() % 4][rand() % 13]); 
    230       }
    231    }
    232 
    233 } /* end function shuffle */
    234 
    235 /* deal cards in deck */
    236 void deal( int wDeck[][ 13 ], const char *wFace[], const char *wSuit[],
    237            int cards[][CARDS][2], int player, int wCards, int show)
    238 {
    239    int card;   /* card counter */
    240    int row;    /* row counter */
    241    int column; /* column counter */
    242    int i = 0, found;
    243 
    244    if(show == 2) {
    245       printf("\n\n===> ");
    246       if(!player)
    247 	 printf("Computer");
    248       else
    249 	 printf("Player %d", player);
    250 
    251       printf(" is changing cards..\n\n");
    252    }
    253 
    254    /* deal each of the 52 cards */
    255    for ( card = 1; card <= 52; card++ ) {
    256       found = 0; /* found the card? :-) */
    257 
    258       /* loop through rows of wDeck */
    259       for ( row = 0; row <= 3 && !found; row++ ) {
    260 
    261          /* loop through columns of wDeck for current row */
    262          for ( column = 0; column <= 12 && !found; column++ ) {
    263             /* if slot contains current card, display card */
    264             if ( wDeck[ row ][ column ] == card ) {
    265 
    266 	       if(show == 2) {
    267 		  if(!player) {
    268 		     printf("\tHIDDEN => ");
    269 		  }
    270 		  else {
    271 		     printf( "%5s of %-8s => ", wFace[cards[player][i][0]],
    272 	                wSuit[cards[player][i][1]]);
    273 		  }
    274 	       }
    275 
    276                cards[player][i][0] = column;
    277 	       cards[player][i][1] = row;
    278 
    279 	       if(!show)
    280 	          printf("\tHIDDEN");
    281 	       else
    282 		  printf( "%5s of %-8s", wFace[ column ], wSuit[ row ]);
    283 
    284 	       ++i;
    285 
    286 	       if(show != 2) printf("%c", !(card % 2) ? '\n' : '\t');
    287 	       else printf("\n");
    288 
    289 	       wDeck[row][column] = 0;
    290 
    291                /* dealing only "wCards" cards */
    292 	       if(i == wCards) {
    293 		  printf("\n");
    294 		  return;
    295 	       }
    296 
    297 	       found = 1;
    298 
    299 
    300             } /* end if */
    301 
    302          } /* end for */
    303 
    304       } /* end for */
    305 
    306    } /* end for */
    307 
    308 } /* end function deal */
    309 
    310 /* Get the points of a player */
    311 int getp(int cards[][CARDS][2], int player, const char *wFace[],
    312          const char *wSuit[], int n_cards, int *r_suit, int *r_face,
    313          int *type, int show)
    314 {
    315    int i, points;
    316    int scale = 0, n;
    317    int o[13][2] = { { 0 } }; /* occurrences */
    318 
    319    int kind = 0, face, suit;
    320    /* 0 = nothing
    321     * 1 = Pair 
    322     * 2 = Double
    323     * 3 = Tris
    324     * 4 = scale
    325     * 5 = Full
    326     * 6 = Poker
    327     * 7 = Color
    328     * 8 = straight flush
    329     * 9 = royal flush
    330     */
    331 
    332    /* Inizitialize the o[0,12][1] to 4. Note that 4 is
    333     * a value (random) greather then highest suit (3). */
    334    for(i = 0; i < 13; i++)
    335       o[i][1] = 4;
    336 
    337    /* count the occurrences of each card's face and
    338     * save to the current index the highest suit */
    339    for(i = 0; i < n_cards; i++) {
    340       ++o[cards[player][i][0]][0];
    341 
    342       /* store the index of the highest suit */
    343       if(o[cards[player][i][0]][1] > cards[player][i][1])
    344          o[cards[player][i][0]][1] = cards[player][i][1];
    345 
    346    }
    347 
    348    /* Ok, now don't forget this:
    349     *
    350     * o[i][0] == the occurrences
    351     * o[i][1] == the suit 
    352     * i == the face
    353     */
    354 
    355    /* check if there is a pair/double/tris/full/poker */
    356    for(i = 0; i < 13; i++) {
    357       switch(o[i][0]) {
    358 	 case 2:
    359 	    /* if this is the 1st pair */
    360 	    if(!kind) /* is a pair */
    361 	       kind = 1; 
    362 
    363 	    /* else could be a double */
    364 	    else if(kind == 1)
    365 	       kind = 2;
    366 
    367 	    else /* else is full */
    368 	       kind = 3;
    369 
    370 	    break;
    371 	 case 3:
    372 	    /* If is the current game is a pair */
    373 	    if(kind == 1)
    374 	       kind = 5; /* then become "full" */
    375 
    376 	    else /* else is tris */
    377 	       kind = 3;
    378 
    379 	    break;
    380 	 case 4:
    381 	    /* Oh, this is Poker! :-) */
    382 	    kind = 6;
    383 	    break;
    384 	 default:
    385 	    /* !o[i][0] */
    386 	    ;
    387 
    388       } /* end switch (o[i]) */
    389 
    390       if( o[i][0] > 1 && (kind != 2 || (kind == 2 &&
    391 	  (!face ? 13 : face) < (!i ? 13 : i) )) ) {
    392 
    393 	 /* If is double and the current pair is less
    394 	  * then previous, i don't store its face/suit */
    395 
    396          face = i;
    397          suit = o[i][1];
    398       }
    399 
    400    } /* end for (i) */
    401 
    402 
    403    /* Here i've checked: pair, double, tris, full and poker.
    404     * "face" and "suit" contains the highest value of the current
    405     * "kind". The suit is required if there are an "equal" result
    406     * for change (increment or decrement) the current value of
    407     * each one basing on the suit. 
    408     * Missing: scale, color and "all cards are not equals". */
    409 
    410 
    411    /* if all cards are different store the highest of these */
    412    if(kind == 0 && !scale) {
    413       for(i = 0, face = -1; i < n_cards; i++) {
    414 
    415 	 /* search the highest */
    416 	 if((!face ? 13 : face) < (!cards[player][i][0] ? 13 :
    417 	    cards[player][i][0]) ) {
    418 	    face = cards[player][i][0];
    419 	    suit = cards[player][i][1];
    420 	 }
    421 
    422 	 /* if are equal, store the suit */
    423 	 else if(face == cards[player][i][0]) {
    424 	    if(suit < cards[player][i][1]) {
    425 	       suit = cards[player][i][1];
    426 	    }
    427 	 }
    428 	       
    429       }
    430    }
    431 
    432    /* check the color */
    433    for(i = 0, n = 4; i < n_cards; i++) {
    434       if(! (n == 4) ) {
    435          if(cards[player][i][1] != n)
    436             n = - 1;
    437       }
    438       else
    439 	 n = cards[player][i][1];
    440    }
    441    if(n != -1) /* live enjoy: life's colored :-) */
    442       kind = 7;
    443 
    444    /* check the scale / *_flush */
    445    order(cards, player, n_cards);
    446 
    447    for(i = n = 1; i < n_cards; i++) {
    448       if(cards[i - 1][0] + 1 != cards[i][0]) {
    449 	 n = 0;
    450 	 break;
    451       }
    452    }
    453    if(n) { /* This is scale / *_flush */
    454 
    455       if(kind == 7) { /* *_flush */
    456 	 if(face == 0) { /* royal flush */
    457 	    kind = 9;
    458 	 }
    459 
    460 	 else { /* straight flush */
    461             kind = 8;
    462 	 }
    463       }
    464       else { /* just scale */
    465          kind = 4;
    466       }
    467    }
    468 
    469    *r_suit = suit;
    470    *r_face = face;
    471    *type = kind;
    472 
    473    /* Assign the points */
    474    switch(kind) {
    475       case 0: /* Nothing */
    476 
    477 	 points = face;
    478 	 break;
    479       case 1: /* Pair */
    480 
    481 	 points = P_PAIR + face;
    482 	 break;
    483       case 2: /* Double */
    484 
    485 	 points = P_DOUBLE + face;
    486 	 break;
    487       case 3: /* Tris */
    488 
    489 	 points = P_TRIS + face;
    490 	 break;
    491       case 4: /* Scale */
    492 
    493 	 points = P_SCALE + face;
    494 	 break;
    495       case 5: /* Full */
    496 
    497 	 points = P_FULL + face;
    498 	 break;
    499       case 6: /* Poker */
    500 
    501 	 points = P_POKER + face;
    502 	 break;
    503       case 7: /* Color */
    504 
    505 	 points = P_COLOR + face;
    506 	 break;
    507       case 8: /* Straight flush */
    508 
    509 	 points = P_SFLUSH + face;
    510 	 break;
    511       case 9: /* Royal flush */
    512 
    513 	 points = P_RFLUSH + face;
    514 	 break;
    515       default: /* (?) */
    516 	 printf("### error in function getp()\n");
    517 	 exit(-1);
    518 
    519    } /* end switch(kind) */
    520 
    521    /* Notice the points to the user */
    522    p_points(kind, face, suit, wFace, wSuit, show);
    523 
    524    if(!face) points += 13; /* add the Ace's points */
    525 
    526    return points + 1; /* + 1 == [1,13] rather then [0,12] */
    527 } /* eof getp() */
    528 
    529 /* Order an array in descending mode */
    530 void order(int a[][CARDS][2], int p, int size)
    531 {
    532    int i;
    533 
    534    for(i = 1; i < size; i++) {
    535       if(a[p][i - 1][0] > a[p][i][0]) {
    536 	 /* swap the face */
    537 	 rswap(&a[p][i][0], &a[p][i - 1][0]);
    538 
    539 	 /* swap the suit */
    540 	 rswap(&a[p][i][1], &a[p][i - 1][1]);
    541       }
    542    } /* end for (i) */
    543 
    544 } /* eof order() */
    545 
    546 /* Change the card (without user interaction) */
    547 int chcs(int cards[][CARDS][2], int player, int type[], const char *wFace[],
    548          const char *wSuit[], int wDeck[][13], int n_cards)
    549 {
    550    int i, ret;
    551 
    552    switch(type[player]) {
    553       case 9: /* Royal flush */
    554       case 8: /* Straight flush */
    555 
    556 	 ret = 0;
    557 	 break;
    558       case 7: /* Color */
    559 	 /* Try to do a Royal flush */
    560 	 ret = 0;
    561 
    562 	 for(i = 0; i < n_cards; i++) {
    563 	    if(cards[player][i][0] + 1 == cards[player][i + 1][0]) {
    564 	       ++ret;
    565 	    }
    566 	 }
    567 
    568          /* if miss only a card to Royal Flush */
    569 	 if(ret == 3) {
    570 	    /* risk: change one card */
    571 
    572 	    if(cards[player][0][0] + 1 == cards[player][1][0]) {
    573 	       rswap(&cards[player][0][0], &cards[player][n_cards - 1][0]);
    574 	       rswap(&cards[player][0][1], &cards[player][n_cards - 1][1]);
    575 	    }
    576 
    577 	    ret = 1;
    578 	 }
    579 	 else /* do nothing */
    580 	    ret = 0;
    581 
    582          break;
    583 
    584       case 5: /* Full */
    585       case 4: /* Scale */
    586 
    587 	 /* Stay */
    588 	 ret = 0;
    589 
    590 	 break;
    591 
    592       case 6: /* Poker */
    593 	 /* Change a card */
    594          ret = 1;
    595 
    596          /* cards is ordered so the "intruder"
    597 	  * have to be at begin or at end */
    598 
    599 	 if(cards[n_cards - 1][0] != cards[2][0]) {
    600 
    601 	    /* copy [0] to [n_cards - 1] */
    602 
    603 	    /* the face */
    604 	    rswap(&cards[player][n_cards - 1][0], &cards[player][0][0]);
    605 
    606 	    /* the suit */
    607 	    rswap(&cards[player][n_cards - 1][1], &cards[player][0][1]);
    608 	 }
    609 
    610 	 /* else cards[player][n_cards - 1][0] <=> cards[player][2][0] */
    611 	  
    612 	 /* and insert a new card on [0] */
    613 	 deal( wDeck, wFace, wSuit, cards, player, ret, 2);
    614 
    615 	 break;
    616       case 2: /* Double */
    617 	 /* change 1 card */
    618 	 ret = 1;
    619 
    620          /* cards is ordered so the "intruder"
    621 	  * have to be at begin or at end */
    622 
    623 	  if(cards[player][n_cards - 1][0] != cards[player][5][1] &&
    624 	     cards[player][n_cards - 1][0] != cards[player][3][0]) {
    625 
    626 		/* the face */
    627 		rswap(&cards[player][n_cards - 1][0], &cards[player][0][0]);
    628 
    629 		/* the suit */
    630 		rswap(&cards[player][n_cards - 1][1], &cards[player][0][1]);
    631 	  }
    632 
    633 	  /* else cards[0][0] is already free */
    634 	 deal( wDeck, wFace, wSuit, cards, player, ret, 2);
    635 
    636 	 break;
    637       case 3: /* Tris */
    638          /* change 2 cards */
    639 	 ret = 2;
    640 
    641 	 /* cards is ordered so the "intruderS"
    642 	  * DON'T have to be at middle */
    643 
    644 	 for(i = 0; i < n_cards / 2; i++) {
    645 	    if(cards[i][0] == cards[2][0])
    646 	    break;
    647 	 }
    648 
    649 	 if(!i) {
    650 	    /* the face */
    651 	    rswap(&cards[player][n_cards - 1][0], &cards[player][0][0]);
    652 	    rswap(&cards[player][n_cards - 2][0], &cards[player][1][0]);
    653 
    654             /* the suit */
    655 	    rswap(&cards[player][n_cards - 1][1], &cards[player][0][1]);
    656 	    rswap(&cards[player][n_cards - 2][1], &cards[player][1][1]);
    657 	 }
    658 	 else if(i == 1) {
    659 	    /* the face */
    660 	    rswap(&cards[player][n_cards - 1][0], &cards[player][2][0]);
    661 
    662 	    /* the suit */
    663 	    rswap(&cards[player][n_cards - 1][1], &cards[player][2][1]);
    664 	 }
    665 
    666          /* insert 2 cards on [0] and [1] */
    667 	 deal( wDeck, wFace, wSuit, cards, player, ret, 2);
    668 
    669 	 break;
    670       case 1: /* Pair */
    671 	 /* change 3 cards */
    672 	 ret = 3;
    673 
    674 	 for(i = 0; i < n_cards; i++) {
    675 	    if(cards[player][i][0] == cards[player][i + 1][0])
    676 	       break;
    677 	 }
    678 
    679 	 /* The face */
    680 	 rswap(&cards[player][n_cards - 1][0], &cards[player][i][0]);
    681 	 rswap(&cards[player][n_cards - 2][0], &cards[player][i+1][0]);
    682 
    683          /* The suit */
    684          rswap(&cards[player][n_cards - 1][1], &cards[player][i][1]);
    685 	 rswap(&cards[player][n_cards - 2][1], &cards[player][i+1][1]);
    686 
    687          deal( wDeck, wFace, wSuit, cards, player, ret, 2);
    688 
    689 	 break;
    690       case 0: /* Nothing */
    691 	 /* Change all cards */
    692 	 ret = 0;
    693 
    694 	 /* If i have a Jack, a Queen, a King or an Ace, hold it */
    695 	 for(i = 0; i < n_cards; i++) {
    696 	    /* 9 == Ten */
    697 	    if( (!cards[player][i][0] ? 13 : cards[player][i][0]) > 9 &&
    698 		(!cards[player][i][0] ? 13 : cards[player][i][0]) > ret) {
    699 		    ret = (!cards[player][i][0] ? 13 : cards[player][i][0]);
    700 
    701 		    rswap(&cards[player][0][0], &cards[player][i][0]);
    702 		    rswap(&cards[player][0][1], &cards[player][i][1]);
    703 	    }
    704 	 }
    705 	 rswap(&cards[player][0][0], &cards[player][n_cards - 1][0]);
    706 	 rswap(&cards[player][0][1], &cards[player][n_cards - 1][1]);
    707 
    708 	 /* else change all cards */
    709 
    710          if(!ret)
    711 	    ret = n_cards;
    712 	 else
    713 	    ret = n_cards - 1;
    714 
    715 	 deal( wDeck, wFace, wSuit, cards, player, ret, 2);
    716 
    717 	 break;
    718       default: /* (?) */
    719 	 printf("### error in function chcs()\n");
    720 	 exit(-1);
    721 
    722    } /* end switch (type[player]) */
    723 
    724    return ret;
    725 } /* eof chcs() */
    726 
    727 /* Swap two variable for reference */
    728 void rswap(int *a, int *b)
    729 {
    730    int hold;
    731 
    732    hold = *a;
    733    *a = *b;
    734    *b = hold;
    735 } /* eof rswap() */
    736 
    737 /* Change the cards (with user interaction) */
    738 int chch(int cards[][CARDS][2], int n_cards, int player, int wDeck[][13],
    739          const char *wFace[], const char *wSuit[], int kind, 
    740 	 int r_face, int r_suit, int show)
    741 {
    742    int i, j, ret = 0;
    743    int changes[CARDS] = { 0 };
    744 
    745    while(ret != EOF) {
    746 
    747       printf("\n\n\t\t..:: CHANGING THE CARDS ::..\n");
    748 
    749       /* Check if something is changed.. */
    750       for(i = 0, ret = 0; i < n_cards; i++) {
    751 	 if(changes[i]) {
    752 	    ++ret;
    753 	 }
    754       }
    755 
    756       if(ret) { /* ..if yes show it.. */
    757 
    758          printf("\nHeld cards:\n\n");
    759          for(i = 0, ret = 0; i < n_cards; i++) {
    760 	    if(changes[i]) {
    761 
    762                printf( "\t[%d] %5s of %-8s", i, 
    763 	          wFace[cards[player][i][0]], wSuit[cards[player][i][1]]);
    764                printf("%c", !(++ret % 2) ? '\n' : '\t');
    765 
    766 	    }
    767          }
    768          printf("\n\n");
    769 
    770       }
    771       else {
    772          /* ..else do nothing. */
    773 	 printf("\n\n===> No held cards!\n\n");
    774       }
    775 
    776       if(ret == n_cards) {
    777 	 printf("You are holding all cards!\n\n\n");
    778       }
    779       else {
    780          printf("\nYour current cards are:\n\n");
    781          for(i = 0, ret = 0; i < n_cards; i++) {
    782 	    if(!changes[i]) {
    783                printf( "\t[%d] %5s of %-8s", i, 
    784 	          wFace[cards[player][i][0]], wSuit[cards[player][i][1]]);
    785                printf("%c", !(++ret % 2) ? '\t' : '\n');
    786 	    }
    787 
    788          }
    789 	 printf("\n\n");
    790       }
    791 
    792       p_points(kind, r_face, r_suit, wFace, wSuit, show);
    793 
    794       while(ret != EOF) {
    795 
    796          printf("\n\n");
    797          printf("What card(s) you would to hold? (%d to confirm)\n"
    798 		"Selection: ", EOF);
    799 
    800          scanf("%d", &ret);
    801 
    802 	 if( (ret >= 0 && ret < n_cards) || ret == EOF)
    803 	    break;
    804 
    805 	 else {
    806 	    printf("\nInvalid selection.");
    807 	    continue;
    808 	 }
    809       }
    810 
    811       if(ret != EOF) {
    812          /* If is selected, then unselect else select */
    813          changes[ret] = !changes[ret] ? 1 : 0;
    814       }
    815 
    816    } /* end while (ret) */
    817 
    818    /* count the cards that will be changed (ret) */
    819    for(i = 0, ret = 0; i < n_cards; i++) {
    820       if(!changes[i])
    821 	 ++ret;
    822    }
    823 
    824    /* put the card to change at begin of the array */
    825    for(i = 0; i < n_cards; i++) {
    826       if(changes[i]) {
    827 	 for(j = n_cards - 1; j > i; j--) {
    828 	    if(!changes[j]) {
    829 	       rswap(&cards[player][i][0], &cards[player][j][0]);
    830 	       rswap(&cards[player][i][1], &cards[player][j][1]);
    831 	    }
    832 	 }
    833       }
    834    } /* end for (i) */
    835 
    836    if(ret) deal( wDeck, wFace, wSuit, cards, player, ret, 2);
    837 
    838    return ret;
    839 } /* eof chch() */
    840 
    841 /* Print a message(s) (the points) basing on the "kind" */
    842 void p_points(int kind, int face, int suit,
    843               const char *wFace[], const char *wSuit[], int show)
    844 {
    845    if(show) printf(": ");
    846 
    847    switch(kind) {
    848       case 0: /* Nothing */
    849          if(show)
    850             printf("All cards are differents!\n"
    851                    ": You could win with the %s of %s\n",
    852                       wFace[face], wSuit[suit]);
    853 
    854          break;
    855       case 1: /* Pair */
    856          if(show)
    857             printf("Oh, it is a pair of %ss!\n", wFace[face]);
    858 
    859          break;
    860       case 2: /* Double */
    861          if(show)
    862             printf("Double to %ss\n", wFace[face]);
    863 
    864          break;
    865       case 3: /* Tris */
    866          if(show)
    867             printf("This is tris of %ss!\n", wFace[face]);
    868 
    869          break;
    870       case 4: /* Scale */
    871          if(show)
    872             printf("Yeah, you have done a scale to %s\n", wFace[face]);
    873 
    874          break;
    875       case 5: /* Full */
    876          if(show)
    877             printf("*** FULL of %ss!\n", wFace[face]);
    878 
    879          break;
    880       case 6: /* Poker */
    881          if(show)
    882             printf("Wow, it's Poker of %ss!\n", wFace[face]);
    883 
    884          break;
    885       case 7: /* Color */
    886          if(show)
    887             printf("%d points of color (%s)\n", P_COLOR, wSuit[suit]);
    888 
    889          break;
    890       case 8: /* Straight flush */
    891          if(show)
    892             printf("* Straight flush to %s*\n", wFace[face]);
    893 
    894          break;
    895       case 9: /* Royal flush */
    896          if(show)
    897             printf("*| Royal Flush (%s) |*\n", wSuit[suit]);
    898 
    899          break;
    900       default: /* (?) */
    901          printf("### error in function p_points()\n");
    902          exit(-1);
    903 
    904    } /* end switch(kind) */
    905 
    906 } /* eof p_points() */
    907