Making Decisions / Validation Routines

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
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 


That makes sense.

What do you think about the second box of code for the validation routine?
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.

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
}

Thank you for your help.
Topic archived. No new replies allowed.