training

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

poker2.c (21878B)


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