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