Making Decisions / Validation Routines
Feb 11, 2019 at 8:04am UTC
Any suggestions on how to organize these bool function calls so that they will cout the best hand. For example, if a hand returns true for both a pair and a two pair, it should cout only two pair. If a hand return true for pair, three of a kind, and full house, it should cout only full house.
I am also trying to get this validation routine right for user input into an array.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
if (containsHighCard(hand)) {
cout << "High Card!" << endl;
}
if (containsPair(hand)) {
cout << "Contains pair!" << endl;
}
if (containsTwoPair(hand)) {
cout << "Two pair!" << endl;
}
if (containsThreeOfaKind(hand)) {
cout << "Three of a kind!" << endl;
}
if (containsFourOfaKind(hand)) {
cout << "Four of a kind!" << endl;
}
if (containsStraight(hand)) {
cout << "Straight!" << endl;
}
if (containsFullHouse(hand)) {
cout << "Full House!" << endl;
}
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
cout << "Enter five numeric cards, no face cards. Use 2 - 9." << endl;
for (int count = 0; count < NUM_CARDS; count++) {
cout << "Card " << count + 1 << ": " ;
cin >> hand[count];
while (hand[count] < 2 || hand[count] > 9) {
cout << "Please enter 2 - 9." << endl;
for (int count = 0; count < NUM_CARDS; count++) {
cout << "Card " << count + 1 << ": " ;
cin >> hand[count];
}
}
}
Last edited on Feb 11, 2019 at 8:08am UTC
Feb 11, 2019 at 8:28am UTC
You need to arrange in best to worst order.
1 2 3 4 5 6 7
if (containsFullHouse(hand)) {
cout << "Full House!" << endl;
} else
if (containsStraight(hand)) {
cout << "Straight!" << endl;
} else
// etc down to high card
Feb 11, 2019 at 9:44am UTC
That makes sense.
What do you think about the second box of code for the validation routine?
Feb 11, 2019 at 10:50am UTC
Well the inner for (int count = 0; count < NUM_CARDS; count++) is just plain wrong.
It's a loop to re-enter some given hand[count], not repeat the whole process over again.
Feb 11, 2019 at 5:04pm UTC
You should write a routine that classifies the hand. That way you can compare hands, which I suspect you'll have to do later on.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
class HandClassification {
public :
enum {HighCard, Pair, TwoPair, ThreeOfKind,
Straight, Flush, FourOfKind, StraightFlush } type {HighCard};
unsigned highCard {0}; // the high card for tie breaker.
};
HandClassification classifyHand(int hand[5])
{
HandClassification result;
if (containsStraightFlush(hand)) {
result.type = HandClassification::StraightFlush;
result.highCard = *max_element(hand, hand+5);
} else if (containsFourOfKind(hand)) {
result.type = HandClassification::FourOfKind;
result.highCard = *max_element(hand, hand+5);
}
// etc
return result
}
Feb 14, 2019 at 9:04am UTC
Thank you for your help.
Topic archived. No new replies allowed.