function to find lowest card not working correctly

I tweaked my code a little bit and now for some reason my function is no longer returning the lowest card. I still need to overload < operator for CardTemplate. If overloading the operator will help solve the problem I will work on that instead.

My class for CardTemplate:
1
2
3
4
5
6
7
8
9
10
11
class CardTemplate
{
public:
    //Add another int for cardStrength and number all card based on str 0-51. 0 = 3 diamond and 51 = 2 spade
    std::string suit;
    int suitStrength;
    int cardNum;
    
    //potentially add string for 11+ cardNum instead of ints

};


My function:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
CardTemplate searchLowestCard(std::vector<CardTemplate> playerHand)
{
    //set lowest card to test against
    CardTemplate lowestCard = playerHand[0];
    //loop to see who has the lowest card
    for(int i = 0; i < playerHand.size(); i++)
    {
        std::cout << "Hand: " << playerHand[i].suit << " : " << playerHand[i].cardNum << std::endl;
        if(playerHand[i].cardNum < lowestCard.cardNum && playerHand[i].suitStrength < lowestCard.suitStrength)
        {
            lowestCard = playerHand[i];
            std::cout << "Low Card: " << lowestCard.suit << " : " << lowestCard.cardNum << std::endl;
        }
    }
    return lowestCard;
}
On line 9 you have two less than expressions. Both must be true in order to set the lowest card. What if one of them is equal and only the other is less?
to sort custom objects with multiple data members where more than one member can influence the ordering, you need to rank the priority of the various data-members towards the ordering of the objects (similar to words in a dictionary - lexicographic sort - 'a' comes first, but for 2 words both beginning with 'a' check the second letter and so on)
one implementation for operator < of Card objects was given below link, lines 29-33, where card suit precedes card rank but you can have it the other way round if you wish:
http://www.cplusplus.com/forum/beginner/211044/#msg989293
My so the card game I'm coding for is deuces. The suitStrength determines which cardNum out of the suits is stronger. For example if someone played a 4 Diamond it could be beat by a 4 club but not a 3 club. Hope that somewhat explained it.

I changed the code to see if it would work but line 9 was actually suppose to be: if(playerHand[i].cardNum < lowestCard.cardNum && playerHand[i].suitStrength <= lowestCard.suitStrength) Still getting the issue. It picks the 2nd lowest card instead every time. I went through the code to see if i maybe initlaized the club suitStrength wrong but it wasnt and everything is pointing to this function and i do believe it is line 9.

The out put i get for this function is this:
Hand: Heart : 5
Low Card: Heart : 5
Hand: Heart : 11
Hand: Spade : 13
Hand: Clover : 3
Low Card: Clover : 3
Hand: Diamond : 3
Hand: Spade : 12
Hand: Diamond : 14
Hand: Clover : 10
Hand: Spade : 5
Hand: Diamond : 6
Hand: Clover : 14
Hand: Clover : 11
Hand: Clover : 6
Hand: Diamond : 15
Hand: Diamond : 8
Hand: Heart : 6
Hand: Clover : 13


As you can see the 3 clover gets selected but right after that is the three diamond the true lowest card in the game but it skips over it? Diamond has suitStrength = 0 while Clover has suitStrength = 1

I'll check out that link thank you for the feedback! So this would this be solved by overloading < operator for CardTemplate? Also it turns out the bug was in a different function and not the one i posted in previously in the link lol. Lines 29-33 is where the overload function is right?
Last edited on
As you can see the 3 clover gets selected but right after that is the three diamond the true lowest card in the game but it skips over it?

The only time a "lowest" card can be chosen is when the value of the card being considered is less than the value of the card currently marked as lowest.
if(playerHand[i].cardNum < lowestCard.cardNum...)

So if playerHand[i].cardNum is 3 and lowestCard.cardNum is 3, this condition is false and suitStrength never comes into play.

Try if (playerHand[i].cardNum <= lowestCardNum...)
Still not getting the lowest card but the logic does seem to make more sense since there should be no duplicate cards in the deck.

I ran the function twice in the program for two different hand from the same deck split 3 ways. Output is below with some skips noted.

My output is after making the changes to: if(playerHand[i].cardNum <= lowestCard.cardNum && playerHand[i].suitStrength <= lowestCard.suitStrength)

New output is:
Checking for who has lowest card 
Hand: Clover : 6
Low Card: Clover : 6
Hand: Heart : 14
Hand: Diamond : 15
Hand: Heart : 12
Hand: Heart : 8
Hand: Clover : 11
Hand: Spade : 6
Hand: Spade : 9
Hand: Diamond : 11
Hand: Diamond : 4
Low Card: Diamond : 4
Hand: Clover : 7
Hand: Spade : 8
Hand: Diamond : 8
Hand: Clover : 10
Hand: Heart : 11
Hand: Heart : 3 <-----------(Heart three is < Diamond 4 but it skipped it)
Hand: Diamond : 14
END OF SEARCH
Lowest card is: Diamond : 4


Hand: Clover : 12
Low Card: Clover : 12
Hand: Diamond : 9
Low Card: Diamond : 9
Hand: Spade : 3 <----------(First of many skips)
Hand: Clover : 14
Hand: Heart : 10
Hand: Spade : 15
Hand: Heart : 13
Hand: Spade : 7 <----------(Skip)
Hand: Clover : 9 
Hand: Heart : 6 <-----------(Skip)
Hand: Diamond : 6 <-------(Success?)
Low Card: Diamond : 6 
Hand: Diamond : 12
Hand: Heart : 9
Hand: Clover : 5 <----------(Skip)
Hand: Heart : 5 <-----------(Skip)
Hand: Spade : 14
Hand: Diamond : 13
END OF SEARCH
Lowest card is: Diamond : 6


Any ideas? I want to work on overloading but i felt there was a flaw in my if statement logic.
Last edited on
Friend gave me an idea of placing a variable into class CardTemplate called int cardStrength and label each card from weakest to highest (0-51) and use that in my if statements. I can use suitStrength for comparing card when it comes to making 5 card hands like a straight or royal flush or possibly getting rid of it completely. I have a feeling this would work better. What do you guys thing?

There is in fact an error with my logic. This if statement isn't written correctly to get the lowest card. Any tips on how to get it to get to the lowest card? To clarify from strongest to weakest the cards go 3 Diamond, 3 Club, 3 Heart, 3 Spade, 4 Diamond, 4 Club, 4 Heart, 4 Spade, 5 Diamond, 5 Club... and so on.

I think i finally figured out it was if(playerHand[i].cardNum < lowestCard.cardNum || (playerHand[i].suitStrength <= lowestCard.suitStrength && playerHand[i].cardNum <= lowestCard.cardNum)) not sure if it works every time tho. So far it is!
Last edited on
Topic archived. No new replies allowed.