Computer Programming Contest Preparation

ToolBox - Source for: 103/10315/a.c



/home/toolbox/public_html/solutions/103/10315/a.c
    1 #include <stdio.h>
    2 #include <string.h>
    3 #include <sys/types.h>
    4 #include <sys/stat.h>
    5 #include <fcntl.h>
    6 #include <stdint.h>
    7 #include <math.h>
    8 #include <stdlib.h>
    9 
   10 #define TRUE  (1 == 1)
   11 #define FALSE (1 != 1)
   12 
   13 #define DEBUG if (FALSE)
   14 #define DEBUG1 if (FALSE)
   15 
   16 #define NCARDS 52
   17 #define NSUITS 4
   18 
   19 typedef struct
   20 {
   21     int suits[4];
   22     int values[13];
   23     int highCard;
   24     int pairs;
   25     int triple;
   26     int quad;
   27     int flush;
   28     int straight;
   29     int total;
   30 } evalStruct;
   31 
   32 char cards[] = "23456789TJQKA";
   33 int values[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
   34 char suits[] = "CDHS";
   35 
   36 int blackHand[5];
   37 int whiteHand[5];
   38 evalStruct white;
   39 evalStruct black;
   40 
   41 /*
   42  *  Author:
   43  *    Date:
   44  * Purpose:
   45  * Problem:
   46  */
   47 
   48 /*
   49  * This template reads data until a terminating value is reached.
   50  */
   51 
   52 
   53 static int max (int i, int j)
   54 {
   55     /* FUNCTION max */
   56     return (i < j ? j : i);
   57 } /* FUNCTION max */
   58 
   59 int rank_card(char value, char suit)
   60 {
   61     /* FUNCTION rank_card */
   62     /* adapted from Programming Challenges */
   63     int k; /* counters */
   64     int i = -1;
   65     int j = -1;
   66     int toReturn;
   67 
   68     for (k=0; (strlen(cards)>k) && (-1 == i); k++)
   69         {
   70             /* for */
   71             if (value == cards[k])
   72                 {
   73                     i = values[k];
   74                 }
   75         } /* for */
   76     for (k=0; (strlen(suits)>k) && (-1 == j); k++)
   77         {
   78             /* for */
   79             if (suit == suits[k])
   80                 {
   81                     j = k;
   82                 }
   83         } /* for */
   84     DEBUG printf("%c %c - %d %d %d\n",value, suit, i, j, i * NSUITS + j);
   85     return (i * NSUITS + j);
   86 } /* FUNCTION rank_card */
   87 
   88 char suit(int card)
   89 {
   90     /* FUNCTION suit */
   91     /* adapted from Programming Challenges */
   92     return (suits[card % NSUITS]);
   93 } /* FUNCTION suit */
   94 
   95 int suitIndex(int card)
   96 {
   97     /* FUNCTION suit */
   98     /* adapted from Programming Challenges */
   99     return (card % NSUITS);
  100 } /* FUNCTION suit */
  101 
  102 char cardIndex(int crd)
  103 {
  104     /* FUNCTION card */
  105     /* adapted from Programming Challenges */
  106     return (values[crd/NSUITS]);
  107 } /* FUNCTION card */
  108 
  109 void init()
  110 {
  111     /* FUNCTION init */
  112     int i;
  113 
  114     for (i=0; 4>i; i++)
  115         {
  116             /* for */
  117             white.suits[i] = 0;
  118             white.values[i] = 0;
  119             black.suits[i] = 0;
  120             black.values[i] = 0;
  121         } /* for */
  122     for (i=4; 13>i; i++)
  123         {
  124             /* for */
  125             white.values[i] = 0;
  126             black.values[i] = 0;
  127         } /* for */
  128 } /* FUNCTION init */
  129 
  130 void dump()
  131 {
  132     /* FUNCTION dump */
  133     int i;
  134 
  135     printf("black: ");
  136     for (i = 0; i < 5; i ++)
  137         {
  138             printf("   %d", blackHand[i]);
  139         }
  140     printf("\n");
  141     printf("white: ");
  142     for (i = 0; i < 5; i ++)
  143         {
  144             printf("   %d", whiteHand[i]);
  145         }
  146     printf("\n");
  147 } /* FUNCTION dump */
  148 
  149 void dump1()
  150 {
  151     /* FUNCTION dump1 */
  152     int i;
  153 
  154     printf("black:\n");
  155     printf("pairs:%d\n", black.pairs);
  156     printf("triple:%d\n", black.triple);
  157     printf("quad:%d\n", black.quad);
  158     printf("straight:%d\n", black.straight);
  159     printf("flush:%d\n", black.flush);
  160     for (i = 0; i < 4; i ++)
  161         {
  162             printf("   %d", black.suits[i]);
  163         }
  164     printf("\n");
  165     for (i = 0; i < 13; i ++)
  166         {
  167             printf("   %d", black.values[i]);
  168         }
  169     printf("\n");
  170     printf("white:\n");
  171     printf("pairs:%d\n", white.pairs);
  172     printf("triple:%d\n", white.triple);
  173     printf("quad:%d\n", white.quad);
  174     printf("straight:%d\n", white.straight);
  175     printf("flush:%d\n", white.flush);
  176     for (i = 0; i < 4; i ++)
  177         {
  178             printf("   %d", white.suits[i]);
  179         }
  180     printf("\n");
  181     for (i = 0; i < 13; i ++)
  182         {
  183             printf("   %d", white.values[i]);
  184         }
  185     printf("\n");
  186 } /* FUNCTION dump1 */
  187 
  188 int getInput()
  189 {
  190     /* FUNCTION getInput */
  191     int dataReadFlag = TRUE;
  192     char buffer[5];
  193     int i;
  194 
  195     if (EOF == scanf(" %s ", buffer))
  196         {
  197             /* at EOF */
  198             dataReadFlag = FALSE;
  199         } /* at EOF */
  200     else
  201         {
  202             /* reading in the rest */
  203             blackHand[0] = rank_card(buffer[0], buffer[1]);
  204             for (i = 1; i < 5; i ++)
  205                 {
  206                     /* input loop */
  207                     scanf(" %s ", buffer);
  208                     blackHand[i] = rank_card(buffer[0], buffer[1]);
  209                 } /* input loop */
  210             for (i = 0; i < 5; i ++)
  211                 {
  212                     /* input loop */
  213                     scanf(" %s ", buffer);
  214                     whiteHand[i] = rank_card(buffer[0], buffer[1]);
  215                 } /* input loop */
  216 
  217         } /* reading in the rest */
  218 
  219     return (dataReadFlag);
  220 } /* FUNCTION getInput */
  221 
  222 int highCard(int hand[5])
  223 {
  224     /* FUNCTION highCard */
  225     int i;
  226     int toReturn;
  227 
  228     toReturn = hand[0];
  229     for (i=1; 5>i; i++)
  230         {
  231             /* for */
  232             toReturn = max(toReturn, hand[i]);
  233         } /* for */
  234     return toReturn;
  235 } /* FUNCTION highCard */
  236 
  237 int detTest(int * ary, int count)
  238 {
  239     /* FUNCTION detTest */
  240     int i;
  241     int toReturn = 0;
  242 
  243     for (i = 0; 13 > i; i ++)
  244         {
  245             /* for loop */
  246             if (ary[i] == count)
  247                 {
  248                     /* found a match */
  249                     toReturn++;
  250                 } /* found a match */
  251         } /* for loop */
  252     return toReturn;
  253 } /* FUNCTION detTest */
  254 
  255 int detFlush(int * ary)
  256 {
  257     /* FUNCTION detFlush */
  258     int i;
  259     int toReturn = 0;
  260 
  261     for ( i = 0; 4 > i; i ++)
  262         {
  263             /* for */
  264             if (5 == ary[i])
  265                 {
  266                     /* flush found */
  267                     toReturn = 1;
  268                 } /* flush found */
  269         } /* for */
  270     return toReturn;
  271 } /* FUNCTION detFlush */
  272 
  273 
  274 int detStraight(int * ary)
  275 {
  276     /* FUNCTION detStraight */
  277     int i;
  278     int toReturn = -1;
  279     int count = 0;
  280 
  281     i = 0;
  282     while (-1 == toReturn)
  283         {
  284             /* while */
  285             if (ary[i] == 1)
  286                 {
  287                     count ++;
  288                     if (5 == count)
  289                         {
  290                             /* straight found */
  291                             toReturn = 1;
  292                         } /* straight found */
  293                 }
  294             else
  295                 {
  296                     if (0 < count)
  297                         {
  298                             /* bail out */
  299                             toReturn = 0;
  300                         } /* bail out */
  301                 }
  302             i++;
  303         } /* while */
  304     return toReturn;
  305 } /* FUNCTION detStraight */
  306 
  307 
  308 void evaluateHand(evalStruct *eval, int hand[5])
  309 {
  310     /* FUNCTION evaluateHand */
  311     int i;
  312 
  313     for (i=0; 5>i; i++)
  314         {
  315             /* for */
  316             eval->suits[suitIndex(hand[i])]++;
  317             eval->values[cardIndex(hand[i])]++;
  318         } /* for */
  319     /* determine highcard */
  320     eval->highCard = highCard(hand);
  321     /* determine number of pairs */
  322     eval->pairs = detTest(eval->values, 2);
  323     /* determine 3 of a kind */
  324     eval->triple = detTest(eval->values, 3);
  325     /* determine 4 of a kind */
  326     eval->quad = detTest(eval->values, 4);
  327     /* determine flush */
  328     eval->flush = detFlush(eval->suits);
  329     /* determine straight */
  330     eval->straight = detStraight(eval->values);
  331     if ((1 == eval->straight) && (1 == eval->flush))
  332         {
  333             /* straight flush */
  334             eval->total = 52*8;
  335         } /* straight flush */
  336     else if (1 == eval->quad)
  337         {
  338             /* 4 of a kind */
  339             eval->total = 52*7;
  340         } /* 4 of a kind */
  341     else if ((1 == eval->triple) && (1 == eval->pairs))
  342         {
  343             /* full house(terrible show!) */
  344             eval->total = 52*6;
  345         } /* full house(terrible show!) */
  346     else if (1 == eval->flush)
  347         {
  348             /* flush */
  349             eval->total = 52*5;
  350         } /* flush */
  351     else if (1 == eval->straight)
  352         {
  353             /* straight */
  354             eval->total = 52*4;
  355         } /* straight */
  356     else if (1 == eval->triple)
  357         {
  358             /* 3 of a kind */
  359             eval->total = 52*3;
  360         } /* 3 of a kind */
  361     else if (2 == eval->pairs)
  362         {
  363             /* 2 pair */
  364             eval->total = 52*2;
  365         } /* 2 pair */
  366     else if (1 == eval->pairs)
  367         {
  368             /* 1 pair */
  369             eval->total = 52*1;
  370         } /* 1 pair */
  371     else
  372         {
  373             /* high card.  You lose! */
  374             eval->total = 52*0;
  375         } /* high card.  You lose! */
  376     eval->total += eval->highCard/4;
  377 } /* FUNCTION evaluateHand */
  378 
  379 void process()
  380 {
  381     /* FUNCTION process */
  382     init();
  383     DEBUG1 printf ("entering process\n");
  384     evaluateHand(&black, blackHand);
  385     evaluateHand(&white, whiteHand);
  386     if (white.total > black.total)
  387         {
  388             /* white wins */
  389             printf("White wins.\n");
  390         } /* white wins */
  391     else if (black.total > white.total)
  392         {
  393             /* black wins */
  394             printf("Black wins.\n");
  395         } /* black wins */
  396     else
  397         {
  398             /* tie */
  399             printf("Tie.\n");
  400         } /* tie */
  401 
  402     DEBUG dump1();
  403     DEBUG dump();
  404 } /* FUNCTION process */
  405 
  406 int main ()
  407 {
  408     /* main */
  409     int moreToDo;
  410 
  411     moreToDo = getInput();
  412     while (moreToDo)
  413         {
  414             /* while */
  415             process();
  416             moreToDo = getInput();
  417         } /* while */
  418 
  419     return EXIT_SUCCESS;
  420 } /* main */
  421