// poker.cpp -- Main program for the poker hand evaluator, // using the card class designed in card.h and implemented in // card.cpp #include #include "boolean.h" #include "card.h" void input (card &c1, card &c2, card &c3, card &c4, card &c5); boolean is_straight_flush(card c1, card c2, card c3, card c4, card c5); boolean is_four_of_kind (card c1, card c2, card c3, card c4, card c5); boolean is_full_house (card c1, card c2, card c3, card c4, card c5); boolean is_flush (card c1, card c2, card c3, card c4, card c5); boolean is_straight (card c1, card c2, card c3, card c4, card c5); boolean is_three_of_kind (card c1, card c2, card c3, card c4, card c5); boolean is_two_pair (card c1, card c2, card c3, card c4, card c5); boolean is_one_pair (card c1, card c2, card c3, card c4, card c5); void main() { card c1, c2, c3, c4, c5; char query; do { input(c1, c2, c3, c4, c5); cout << "Your hand is a "; // We call each of the evaluation functions in descending rank, // from straight flush down to one pair. This is so that we can // award the hand properly. For example, a three of a kind also // contains a pair, but must be identified as a three of a kind. if (is_straight_flush(c1, c2, c3, c4, c5)) cout << "straight flush." << endl; else if (is_four_of_kind(c1, c2, c3, c4, c5)) cout << "four of a kind." << endl; else if (is_full_house(c1, c2, c3, c4, c5)) cout << "full house." << endl; else if (is_flush(c1, c2, c3, c4, c5)) cout << "flush." << endl; else if (is_straight(c1, c2, c3, c4, c5)) cout << "straight." << endl; else if (is_three_of_kind(c1, c2, c3, c4, c5)) cout << "three of a kind." << endl; else if (is_two_pair(c1, c2, c3, c4, c5)) cout << "two pair." << endl; else if (is_one_pair(c1, c2, c3, c4, c5)) cout << "one pair." << endl; else cout << "lost cause -- you should fold or bluff." << endl << endl; cout << "Do you want to try another hand? (y/n) "; cin >> query; } while (query == 'y' || query == 'Y'); } void input(card &c1, card &c2, card &c3, card &c4, card &c5) { boolean good_input = FALSE; do { cout << "Please enter your 5 cards in a poker hand," << endl << "descending order of denomination:" << endl; cin >> c1 >> c2 >> c3 >> c4 >> c5; good_input = (c1 >= c2) && (c2 >= c3) && (c3 >= c4) && (c4 >= c5); if (! good_input) cout << "Your input is not in descending order. Please try again." << endl; } while (! good_input); } // A straight-flush means just that: the hand is both a straight and a flush. boolean is_straight_flush(card c1, card c2, card c3, card c4, card c5) { return (is_straight(c1, c2, c3, c4, c5) && is_flush(c1, c2, c3, c4, c5)); } // For the hand to contain four of a kind, either the four highest // cards are the same, or the 4 lowest are the same. boolean is_four_of_kind(card c1, card c2, card c3, card c4, card c5) { return (c1 == c4 || c2 == c5); } // For a full house, either the top 3 cards match denominations and the bottom // two match; or the top 2 match and the bottom 3 match. boolean is_full_house(card c1, card c2, card c3, card c4, card c5) { return (c1 == c3 && c4 == c5) || (c1 == c2 && c3 == c5); } // Flush means all cards have the same suit. We defined the ^ operator // to tell if two cards have the same suit, so we use it for all the // consecutive pairs of cards in the poker hand. boolean is_flush(card c1, card c2, card c3, card c4, card c5) { return (c1 ^ c2) && (c2 ^ c3) && (c3 ^ c4) && (c4 ^ c5); } // A straight means that the cards are in a perfect (descending) sequence. // We use the special % operator that tells if 2 cards differ by one // denomination. boolean is_straight(card c1, card c2, card c3, card c4, card c5) { return (c1 % c2) && (c2 % c3) && (c3 % c4) && (c4 % c5); } // For a 3 of a kind, we need to find 3 consecutive cards that have the // same denomination: the top 3, middle 3 or the bottom 3. boolean is_three_of_kind(card c1, card c2, card c3, card c4, card c5) { return (c1 == c3 || c2 == c4 || c3 == c5); } // For 2 pair, the card that doesn't match any of the others is either // the top card, the middle card or the bottom card. boolean is_two_pair(card c1, card c2, card c3, card c4, card c5) { return (c1 == c2 && c3 == c4) || (c1 == c2 && c4 == c5) || (c2 == c3 && c4 == c5); } // One pair means that any two consecutive cards are the same. boolean is_one_pair(card c1, card c2, card c3, card c4, card c5) { return (c1 == c2 || c2 == c3 || c3 == c4 || c4 == c5); }