/* This has the same effect as poker2, but the internal structure of the program is very different. This program has a value_hand() function to calculate the value of a hand and a value_print() function to print the value readably. Make sure you understand these language features: defining functions using functions functions returning void long long LL for a long long constant switch() with case and break what happens at the end of a case when there's no break or return */ #include /* v = value_hand(r0,s0,...,r4,s4) Input: r0, s0, ..., r4, s4 represent a 5-card hand. r0 and s0 are the rank and suit of card #0: e.g., '7' and 'C'. Output: v represents the value of that hand for poker. Higher values are better hands. Here's how hand values work: 907060503LL means high-card: 9, 7, 6, 5, 3. 10006090503LL means pair: two 6's, 9, 5, 3. 20000060309LL means two-pair: two 6's, two 3's, 9. 30000060903LL means three-of-a-kind: three 6's, 9, 3. 40000000009LL means straight: 9, 8, 7, 6, 5. 50907060503LL means flush: 9, 7, 6, 5, 3, all of the same suit. 60000000609LL means full-house: three 6's, two 9's. 70000000609LL means four-of-a-kind: four 6's, 9. 80000000009LL means straight-flush: 9, 8, 7, 6, 5, all of the same suit. 90000000006LL means five-of-a-kind: five 6's. J, Q, K, A are represented as 11, 12, 13, 14. */ long long value_hand( char r0,char s0, char r1,char s1, char r2,char s2, char r3,char s3, char r4,char s4) { long long result; int flush; /* we have a flush (all cards in the same suit) */ int have2; /* number of cards with rank 2 */ int have3; int have4; int have5; int have6; int have7; int have8; int have9; int haveT; int haveJ; int haveQ; int haveK; int haveA; char straight; /* the highest rank in a straight; 0 if not straight */ int pairs; int threes; int fours; int fives; flush = (s0 == s1 && s1 == s2 && s2 == s3 && s3 == s4); have2 = (r0 == '2') + (r1 == '2') + (r2 == '2') + (r3 == '2') + (r4 == '2'); have3 = (r0 == '3') + (r1 == '3') + (r2 == '3') + (r3 == '3') + (r4 == '3'); have4 = (r0 == '4') + (r1 == '4') + (r2 == '4') + (r3 == '4') + (r4 == '4'); have5 = (r0 == '5') + (r1 == '5') + (r2 == '5') + (r3 == '5') + (r4 == '5'); have6 = (r0 == '6') + (r1 == '6') + (r2 == '6') + (r3 == '6') + (r4 == '6'); have7 = (r0 == '7') + (r1 == '7') + (r2 == '7') + (r3 == '7') + (r4 == '7'); have8 = (r0 == '8') + (r1 == '8') + (r2 == '8') + (r3 == '8') + (r4 == '8'); have9 = (r0 == '9') + (r1 == '9') + (r2 == '9') + (r3 == '9') + (r4 == '9'); haveT = (r0 == 'T') + (r1 == 'T') + (r2 == 'T') + (r3 == 'T') + (r4 == 'T'); haveJ = (r0 == 'J') + (r1 == 'J') + (r2 == 'J') + (r3 == 'J') + (r4 == 'J'); haveQ = (r0 == 'Q') + (r1 == 'Q') + (r2 == 'Q') + (r3 == 'Q') + (r4 == 'Q'); haveK = (r0 == 'K') + (r1 == 'K') + (r2 == 'K') + (r3 == 'K') + (r4 == 'K'); haveA = (r0 == 'A') + (r1 == 'A') + (r2 == 'A') + (r3 == 'A') + (r4 == 'A'); straight = 0; if (haveA && have2 && have3 && have4 && have5) straight = 5; if (have2 && have3 && have4 && have5 && have6) straight = 6; if (have3 && have4 && have5 && have6 && have7) straight = 7; if (have4 && have5 && have6 && have7 && have8) straight = 8; if (have5 && have6 && have7 && have8 && have9) straight = 9; if (have6 && have7 && have8 && have9 && haveT) straight = 10; if (have7 && have8 && have9 && haveT && haveJ) straight = 11; if (have8 && have9 && haveT && haveJ && haveQ) straight = 12; if (have9 && haveT && haveJ && haveQ && haveK) straight = 13; if (haveT && haveJ && haveQ && haveK && haveA) straight = 14; if (straight && flush) return 80000000000LL + straight; if (straight) return 40000000000LL + straight; result = 0; fives = 0; if (haveA == 5) { result = result * 100 + 14; ++fives; } if (haveK == 5) { result = result * 100 + 13; ++fives; } if (haveQ == 5) { result = result * 100 + 12; ++fives; } if (haveJ == 5) { result = result * 100 + 11; ++fives; } if (haveT == 5) { result = result * 100 + 10; ++fives; } if (have9 == 5) { result = result * 100 + 9; ++fives; } if (have8 == 5) { result = result * 100 + 8; ++fives; } if (have7 == 5) { result = result * 100 + 7; ++fives; } if (have6 == 5) { result = result * 100 + 6; ++fives; } if (have5 == 5) { result = result * 100 + 5; ++fives; } if (have4 == 5) { result = result * 100 + 4; ++fives; } if (have3 == 5) { result = result * 100 + 3; ++fives; } if (have2 == 5) { result = result * 100 + 2; ++fives; } fours = 0; if (haveA == 4) { result = result * 100 + 14; ++fours; } if (haveK == 4) { result = result * 100 + 13; ++fours; } if (haveQ == 4) { result = result * 100 + 12; ++fours; } if (haveJ == 4) { result = result * 100 + 11; ++fours; } if (haveT == 4) { result = result * 100 + 10; ++fours; } if (have9 == 4) { result = result * 100 + 9; ++fours; } if (have8 == 4) { result = result * 100 + 8; ++fours; } if (have7 == 4) { result = result * 100 + 7; ++fours; } if (have6 == 4) { result = result * 100 + 6; ++fours; } if (have5 == 4) { result = result * 100 + 5; ++fours; } if (have4 == 4) { result = result * 100 + 4; ++fours; } if (have3 == 4) { result = result * 100 + 3; ++fours; } if (have2 == 4) { result = result * 100 + 2; ++fours; } threes = 0; if (haveA == 3) { result = result * 100 + 14; ++threes; } if (haveK == 3) { result = result * 100 + 13; ++threes; } if (haveQ == 3) { result = result * 100 + 12; ++threes; } if (haveJ == 3) { result = result * 100 + 11; ++threes; } if (haveT == 3) { result = result * 100 + 10; ++threes; } if (have9 == 3) { result = result * 100 + 9; ++threes; } if (have8 == 3) { result = result * 100 + 8; ++threes; } if (have7 == 3) { result = result * 100 + 7; ++threes; } if (have6 == 3) { result = result * 100 + 6; ++threes; } if (have5 == 3) { result = result * 100 + 5; ++threes; } if (have4 == 3) { result = result * 100 + 4; ++threes; } if (have3 == 3) { result = result * 100 + 3; ++threes; } if (have2 == 3) { result = result * 100 + 2; ++threes; } pairs = 0; if (haveA == 2) { result = result * 100 + 14; ++pairs; } if (haveK == 2) { result = result * 100 + 13; ++pairs; } if (haveQ == 2) { result = result * 100 + 12; ++pairs; } if (haveJ == 2) { result = result * 100 + 11; ++pairs; } if (haveT == 2) { result = result * 100 + 10; ++pairs; } if (have9 == 2) { result = result * 100 + 9; ++pairs; } if (have8 == 2) { result = result * 100 + 8; ++pairs; } if (have7 == 2) { result = result * 100 + 7; ++pairs; } if (have6 == 2) { result = result * 100 + 6; ++pairs; } if (have5 == 2) { result = result * 100 + 5; ++pairs; } if (have4 == 2) { result = result * 100 + 4; ++pairs; } if (have3 == 2) { result = result * 100 + 3; ++pairs; } if (have2 == 2) { result = result * 100 + 2; ++pairs; } if (haveA == 1) result = result * 100 + 14; if (haveK == 1) result = result * 100 + 13; if (haveQ == 1) result = result * 100 + 12; if (haveJ == 1) result = result * 100 + 11; if (haveT == 1) result = result * 100 + 10; if (have9 == 1) result = result * 100 + 9; if (have8 == 1) result = result * 100 + 8; if (have7 == 1) result = result * 100 + 7; if (have6 == 1) result = result * 100 + 6; if (have5 == 1) result = result * 100 + 5; if (have4 == 1) result = result * 100 + 4; if (have3 == 1) result = result * 100 + 3; if (have2 == 1) result = result * 100 + 2; if (fives) return 90000000000LL + result; if (fours) return 70000000000LL + result; if (threes && pairs) return 60000000000LL + result; if (flush) return 50000000000LL + result; if (threes) return 30000000000LL + result; if (pairs > 1) return 20000000000LL + result; if (pairs) return 10000000000LL + result; return result; } void rank_print(int n) { switch (n) { case 14: printf("A"); return; case 13: printf("K"); return; case 12: printf("Q"); return; case 11: printf("J"); return; case 10: printf("T"); return; case 9: printf("9"); return; case 8: printf("8"); return; case 7: printf("7"); return; case 6: printf("6"); return; case 5: printf("5"); return; case 4: printf("4"); return; case 3: printf("3"); return; case 2: printf("2"); return; default: printf("?"); return; /* should never happen */ } } void value_print(long long v) { if (v == 80000000014LL) { printf("royal flush.\n"); return; } switch (v / 10000000000LL) { case 9: printf("five "); rank_print(v % 100); printf("'s.\n"); return; case 8: printf("straight flush, "); rank_print(v % 100); printf(" high.\n"); return; case 7: printf("four "); rank_print((v / 100) % 100); printf("'s"); break; case 6: printf("three "); rank_print((v / 100) % 100); printf("'s. pair of "); rank_print(v % 100); printf("'s. full house. \n"); return; case 4: printf("straight, "); rank_print(v % 100); printf(" high.\n"); return; case 3: printf("three "); rank_print((v / 10000) % 100); printf("'s. "); rank_print((v / 100) % 100); break; case 2: printf("pair of "); rank_print((v / 10000) % 100); printf("'s. pair of "); rank_print((v / 100) % 100); printf("'s"); break; case 1: printf("pair of "); rank_print((v / 1000000) % 100); printf("'s. "); rank_print((v / 10000) % 100); printf(". "); rank_print((v / 100) % 100); break; case 5: printf("flush. "); /* fall through */ default: rank_print((v / 100000000) % 100); printf(". "); rank_print((v / 1000000) % 100); printf(". "); rank_print((v / 10000) % 100); printf(". "); rank_print((v / 100) % 100); } printf(". "); rank_print(v % 100); printf(". \n"); } int main(void) { char x0; char y0; char x1; char y1; char x2; char y2; char x3; char y3; char x4; char y4; for (;;) { if (scanf(" %c%c",&x0,&y0) < 2) break; if (scanf(" %c%c",&x1,&y1) < 2) break; if (scanf(" %c%c",&x2,&y2) < 2) break; if (scanf(" %c%c",&x3,&y3) < 2) break; if (scanf(" %c%c",&x4,&y4) < 2) break; printf("%c%c ",x0,y0); printf("%c%c ",x1,y1); printf("%c%c ",x2,y2); printf("%c%c ",x3,y3); printf("%c%c: ",x4,y4); value_print(value_hand(x0,y0,x1,y1,x2,y2,x3,y3,x4,y4)); } return 0; }