/* In draw poker, you start with a 5-card hand. You select 2, 3, 4, or 5 good cards to keep, and discard the rest. You then draw 3, 2, 1, or 0 cards from the rest of the deck to build a new 5-card hand. You want the final hand to be as good as possible. This program reads a 5-card poker hand, the same way as poker1. For each set of 3 cards from the hand, it computes the number of ways to draw a straight flush, a four-of-a-kind, etc. Make sure you understand these language features: variables defined outside functions initialization of variables inside functions: int count0 = 0 what happens when the same variable name is used in several functions Exercise: Modify this program to also handle sets of 2, 4, and 5 cards. */ #include 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; } char rank(int c) { switch (c % 13) { case 0: return '2'; case 1: return '3'; case 2: return '4'; case 3: return '5'; case 4: return '6'; case 5: return '7'; case 6: return '8'; case 7: return '9'; case 8: return 'T'; case 9: return 'J'; case 10: return 'Q'; case 11: return 'K'; case 12: return 'A'; } } char suit(int c) { switch (c / 13) { case 0: return 'C'; case 1: return 'D'; case 2: return 'H'; case 3: return 'S'; } } char x0; char y0; /* original card #0 */ char x1; char y1; /* original card #1 */ char x2; char y2; /* original card #2 */ char x3; char y3; /* original card #3 */ char x4; char y4; /* original card #4 */ void keep3(char r0,char s0,char r1,char s1,char r2,char s2) { int c3; char r3; char s3; /* ``larger'' card drawn */ int c4; char r4; char s4; /* ``smaller'' card drawn */ long long v; int count0 = 0; int count1 = 0; int count2 = 0; int count3 = 0; int count4 = 0; int count5 = 0; int count6 = 0; int count7 = 0; int count8 = 0; for (c3 = 0;c3 < 52;++c3) { r3 = rank(c3); s3 = suit(c3); if ((r3 == x0) && (s3 == y0)) continue; if ((r3 == x1) && (s3 == y1)) continue; if ((r3 == x2) && (s3 == y2)) continue; if ((r3 == x3) && (s3 == y3)) continue; if ((r3 == x4) && (s3 == y4)) continue; /* the larger card drawn is none of the original cards */ for (c4 = 0;c4 < c3;++c4) { r4 = rank(c4); s4 = suit(c4); if ((r4 == x0) && (s4 == y0)) continue; if ((r4 == x1) && (s4 == y1)) continue; if ((r4 == x2) && (s4 == y2)) continue; if ((r4 == x3) && (s4 == y3)) continue; if ((r4 == x4) && (s4 == y4)) continue; /* the smaller card drawn is none of the original cards */ v = value_hand(r0,s0,r1,s1,r2,s2,r3,s3,r4,s4); if (v >= 80000000000LL) ++count8; else if (v >= 70000000000LL) ++count7; else if (v >= 60000000000LL) ++count6; else if (v >= 50000000000LL) ++count5; else if (v >= 40000000000LL) ++count4; else if (v >= 30000000000LL) ++count3; else if (v >= 20000000000LL) ++count2; else if (v >= 10000000000LL) ++count1; else ++count0; } } printf("%c%c ",r0,s0); printf("%c%c ",r1,s1); printf("%c%c: ",r2,s2); printf("%5d ",count0); printf("%5d ",count1); printf("%5d ",count2); printf("%5d ",count3); printf("%5d ",count4); printf("%5d ",count5); printf("%5d ",count6); printf("%5d ",count7); printf("%5d\n",count8); } int main(void) { scanf(" %c%c",&x0,&y0); scanf(" %c%c",&x1,&y1); scanf(" %c%c",&x2,&y2); scanf(" %c%c",&x3,&y3); scanf(" %c%c",&x4,&y4); printf(" "); printf(" "); printf(" "); printf(" high "); printf(" pair "); printf("2pair "); printf("three "); printf(" strt "); printf("flush "); printf("fullh "); printf(" four "); printf("sflsh\n"); keep3(x0,y0,x1,y1,x2,y2); keep3(x0,y0,x1,y1,x3,y3); keep3(x0,y0,x1,y1,x4,y4); keep3(x0,y0,x2,y2,x3,y3); keep3(x0,y0,x2,y2,x4,y4); keep3(x0,y0,x3,y3,x4,y4); keep3(x1,y1,x2,y2,x3,y3); keep3(x1,y1,x2,y2,x4,y4); keep3(x1,y1,x3,y3,x4,y4); keep3(x2,y2,x3,y3,x4,y4); return 0; }