/* This program prints the chance of a straight flush, the chance of a four-of-a-kind, etc. It looks at all possible poker hands, and uses the value_hand() function from poker3 to evaluate each hand. Beware that this program takes a little while to run. It performs a huge number of computations, more than you would ever want to perform by hand. Debugging this type of program can be difficult. If the computer has performed one million operations and obtained a strange result, how do you track down the problem? The trick is to try again and look at the variables after half a million operations; if something strange has happened, focus on the first half million operations; if nothing strange has happened, focus on the second half million operations. Optional challenge: How can this program be made faster? Feel free to work together on this challenge. */ #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'; } } void say(double countthis,double counthands) { printf("%7.0f",countthis); printf(" hands ("); printf("%9.6f",100 * countthis / counthands); printf("%%) "); } double counthands = 0; double count0 = 0; double count1 = 0; double count2 = 0; double count3 = 0; double count4 = 0; double count5 = 0; double count6 = 0; double count7 = 0; double count8 = 0; int main(void) { int c0; /* card #0, between 0 and 51: 0 means 2C, 1 means 3C, etc. */ int c1; int c2; int c3; int c4; char x0; char y0; char x1; char y1; char x2; char y2; char x3; char y3; char x4; char y4; long long v; for (c0 = 0;c0 < 52;++c0) { x0 = rank(c0); y0 = suit(c0); for (c1 = 0;c1 < c0;++c1) { x1 = rank(c1); y1 = suit(c1); for (c2 = 0;c2 < c1;++c2) { x2 = rank(c2); y2 = suit(c2); for (c3 = 0;c3 < c2;++c3) { x3 = rank(c3); y3 = suit(c3); for (c4 = 0;c4 < c3;++c4) { x4 = rank(c4); y4 = suit(c4); /* This is a good spot for a breakpoint. What do you think the cards will be? */ ++counthands; v = value_hand(x0,y0,x1,y1,x2,y2,x3,y3,x4,y4); 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; } } } } } say(count8,counthands); printf("straight flush\n"); say(count7,counthands); printf("four of a kind\n"); say(count6,counthands); printf("full house\n"); say(count5,counthands); printf("flush\n"); say(count4,counthands); printf("straight\n"); say(count3,counthands); printf("three of a kind\n"); say(count2,counthands); printf("two pair\n"); say(count1,counthands); printf("pair\n"); say(count0,counthands); printf("high card\n"); return 0; }