Thread: So close to finish this C++ program, but I got stuck. Please, this is really urgent!

  1. #1
    Registered User
    Join Date
    Oct 2012
    Posts
    1

    So close to finish this C++ program, but I got stuck. Please, this is really urgent!

    Hello everyone.
    I am writing a mini poker evaluation program and it is almost done. I got it to compile and work, but I ran into an issue. My hand comparisons are not working properly. For instance if you had the following

    hand one 2clubs, 3clubs, 4clubs, 5clubs and 6clubs; compared against hand two which consists of 10spades, Jspades, QSpades, KSpades and AceSpades. Obviously hand two should be the winning hand, right?

    Well, my program tells me that hand one is the winner and that is because hand comparison is not done properly and I can't seem to find the issue. It is a multiple file program, but I will only post the code I think is causing the issue.

    Code:
    #include <algorithm>
    #include "evaluator.h"
    
    Evaluator::Evaluator(void)
    {
    }
    
    Evaluator::~Evaluator(void)
    {
    }
    
    SortedEvaluator::SortedEvaluator(void)
    {
    }
    
    SortedEvaluator::~SortedEvaluator(void)
    {
    }
    
    inline std::string SortedEvaluator::ToString(std::vector<const Card*> hand)
    {
        std::sort(hand.begin(), hand.end(), compare);
    
        cout << "\t";
    
        for(vector<const Card*>::const_iterator it = hand.begin(); it != hand.end(); ++it)
        {
            cout << (*it) -> ToString() << " ";        
        }
    
        return " ";
    }
    
    bool lessThan(const Card* c1, const Card* c2)
    {
        if(c1 -> GetPip() < c2 -> GetPip())
        {
            return true;
        }
        else
        {
            return false;
        }
    }
    
    inline int SortedEvaluator::compareHands(std::vector<const Card*> hand1, std::vector<const Card*> hand2)
    {
        std::sort(hand1.begin(), hand1.end(), compare);
        std::sort(hand2.begin(), hand2.end(), compare);
    
        //Straight Flush
        if(isFlush(hand1) == true && isStraight(hand1) == true && isFlush(hand2) == true && isStraight(hand2) == true)
        {
            return compKick(hand1, hand2);
        }
        else if(isFlush(hand1) == true && isStraight(hand1) == true && isStraight(hand2) == false)
        {
            return 1;    
        }
        else if((isStraight(hand1) == false) && (isFlush(hand2) == true && isStraight(hand2) == true))
        {
            return -1;    
        }
    
        //Four of a Kind
        else if(isQuad(hand1) && isQuad(hand2))
        {
            return compQuad(hand1, hand2);    
        }
        else if(isQuad(hand1) == true && isQuad(hand2) == false)
        {
            return 1;    
        }
        else if(isQuad(hand1) == false && isQuad(hand2) == true)
        {
            return -1;    
        }
    
        //Full House
        else if(isFullHouse(hand1) && isFullHouse(hand2))
        {
            return compFHouse(hand1, hand2);
        }
        else if(isFullHouse(hand1) == true && isFullHouse(hand2) == false)
        {
            return 1;    
        }
        else if(isFullHouse(hand1) == false && isFullHouse(hand2) == true)
        {
            return -1;    
        }
    
        //Flush
        else if(isFlush(hand1) == true && isFlush(hand2) == true) 
        {
            return compFlush(hand1, hand2);
        }
        else if(isFlush(hand1) == true && isFlush(hand2) == false)
        {
            return 1;    
        }
        else if(isFlush(hand1) == false && isFlush(hand2) == true)
        {
            return -1;    
        }
    
        //Straight
        else if(isStraight(hand1) && isStraight(hand2))
        {
            return compStraight(hand1, hand2);
        }
        else if(isStraight(hand1) == true && isStraight(hand2) == false)
        {
            return 1;    
        }
        else if(isStraight(hand1) == false && isStraight(hand2) == false)
        {
            return -1;    
        }
    
        //Trips
        else if(isTrips(hand1) && isTrips(hand2))
        {
            return compTrips(hand1, hand2);
        }
        else if(isTrips(hand1) == true && isTrips(hand2) == false)
        {
            return 1;    
        }
        else if(isTrips(hand1) == false && isTrips(hand2) == true)
        {
            return -1;    
        }
    
        //Two Pairs
        else if(isTPair(hand1) && isTPair(hand2))
        {
            return compTPair(hand1, hand2);
        }
        else if(isTPair(hand1) == true && isTPair(hand2) == false)
        {
            return 1;    
        }
        else if(isTPair(hand1) == false && isTPair(hand2) == true)
        {
            return -1;    
        }
    
        //Pair
        else if(isPair(hand1) && isPair(hand2))
        {
            return compPair(hand1, hand2);
        }
        else if(isPair(hand1) == true && isPair(hand2) == false)
        {
            return 1;    
        }
        else if(isPair(hand1) == false && isPair(hand2) == true)
        {
            return -1;    
        }
        else
        {
            compKick(hand1, hand2);
        } 
    
        return 0;
    }
    
    bool SortedEvaluator::isFlush(const vector<const Card*> hand)
    {
        bool Flush;
    
        cout << "isFlush() called\n";
    
        if(hand[0] -> GetSuit() == hand[1] -> GetSuit() && hand[1] -> GetSuit() == hand[2] -> GetSuit() &&
           hand[2] -> GetSuit() == hand[3] -> GetSuit() && hand[3] -> GetSuit() == hand[4] -> GetSuit())
        {
            Flush = true;
        }
        else
        {
            Flush = false;
        }
    
        return Flush;
    }
    
    bool SortedEvaluator::isStraight(const vector<const Card*> hand)
    {
        bool Straight = isWStraight(hand);
    
        if(Straight == true)
        {
            return true;
        }
    
        if(hand[0] -> GetSuit() + 4 == hand[1] -> GetSuit() && hand[1] -> GetSuit() + 3 == hand[2] -> GetSuit() &&
           hand[2] -> GetSuit() + 2 == hand[3] -> GetSuit() && hand[3] -> GetSuit() + 1 == hand[4] -> GetSuit())
        {
            Straight = true;
        }
        else
        {
            Straight = false;
        }
    
        cout << "isStraight() called\n";
        return Straight;
    }
    
    bool SortedEvaluator::isWStraight(const vector<const Card*> hand)
    {
        if(hand[0] -> GetPip() == Card::_2 && hand[1] -> GetPip() == Card::_3 &&
           hand[2] -> GetPip() == Card::_4 && hand[3] -> GetPip() == Card::_5 &&
           hand[4] -> GetPip() == Card::_A)
        {
            return true;
        }
    
        return false;
    }
    
    bool SortedEvaluator::isQuad(const vector<const Card*> hand)
    {
        bool Quad;
    
        if(hand[0] -> GetSuit() == hand[3] -> GetSuit() || hand[1] -> GetSuit() == hand[4] -> GetSuit())
        {
            Quad = true;
        }
        else
        {
            Quad = false;
        }
    
        return Quad;
    }
        
    bool SortedEvaluator::isFullHouse(const vector<const Card*> hand)
    {
        bool FHouse;
    
        if((hand[0] -> GetPip() == hand[1] -> GetSuit() && hand[2] -> GetPip() == hand[4] -> GetPip()) ||
           (hand[0] -> GetPip() == hand[2] -> GetSuit() && hand[3] -> GetPip() == hand[4] -> GetPip()))
        {
            FHouse = true;
        }
        else
        {
            FHouse = false;
        }
        
        return FHouse;
    }
    
    bool SortedEvaluator::isTrips(const vector<const Card*> hand)
    {
        bool Trips;
    
        if(hand[0] -> GetPip() == hand[2] -> GetPip() || 
           hand[1] -> GetPip() == hand[3] -> GetPip() ||
           hand[2] -> GetPip() == hand[4] -> GetPip())
        {
            Trips = true;
        }
        else
        {
            Trips = false;
        }
    
        return Trips;
    }
    
    bool SortedEvaluator::isTPair(const vector<const Card*> hand)
    {
        bool TPair;
    
        if((hand[0] -> GetPip() == hand[1] -> GetPip() && hand[2] -> GetPip() == hand[3] -> GetPip()) ||
           (hand[1] -> GetPip() == hand[2] -> GetPip() && hand[3] -> GetPip() == hand[4] -> GetPip()) ||
           (hand[0] -> GetPip() == hand[1] -> GetPip() && hand[3] -> GetPip() == hand[4] -> GetPip()))
        {
            TPair = true;
        }
        else
        {
            TPair = false;
        }
    
        return TPair;
    }
    
    bool SortedEvaluator::isPair(const vector<const Card*> hand)
    {
        bool Pair;
    
        if((hand[0] -> GetPip() == hand[1] -> GetPip()) || (hand[2] -> GetPip() == hand[3] -> GetPip()) ||
           (hand[1] -> GetPip() == hand[2] -> GetPip()) || (hand[3] -> GetPip() == hand[4] -> GetPip()))
        {
            Pair = true;
        }
        else
        {
            Pair = false;
        }
    
        return Pair;
    }
    
    //Hand Comparisons
    int SortedEvaluator::compKick(const vector<const Card*> h1, const vector<const Card*> h2)
    {
        int ret = 0;
    
        for(int i = 4; i >= 0; i--)
        {
            if(h1[i] -> GetPip() > h2[i] -> GetPip())
            {
                ret = 1;
                return ret;
            }
    
            if(h1[i] -> GetPip() < h2[i] -> GetPip())
            {
                ret = -1;
                return ret;
            }
        }
    
        return ret;
    }
    
    int SortedEvaluator::compFlush(const vector<const Card*> h1, const vector<const Card*> h2)
    {
        return compKick(h1, h2);
    }
    
    int SortedEvaluator::compStraight(const vector<const Card*> h1, const vector<const Card*> h2)
    {
        int ret = 0;
    
        if(isStraight(h1) == true && isStraight(h2) == true)
        {
            ret = 0;
            return ret;
        }
    
        if(isStraight(h1) == true && isStraight(h2) == false)
        {
            ret = 1;
            return ret;
        }
    
        if(isStraight(h1) == false && isStraight(h2) == true)
        {
            ret = -1;
            return ret;
        }
    
        if(isWStraight(h1) == true && isWStraight(h2) == true)
        {
            ret = 0;
            return ret;
        }
    
        if(isStraight(h1) == true && isWStraight(h2) == true)
        {
            ret = 1;
            return ret;
        }
    
        if(isWStraight(h1) == true && isStraight(h2) == true)
        {
            ret = -1;
            return ret;
        }
    
        return compKick(h1, h2);
    }
    
    int SortedEvaluator::compQuad(const vector<const Card*> h1, const vector<const Card*> h2)
    {
        int ret = 0;
    
        if(h1[2] -> GetPip() > h2[2] -> GetPip())
        {
            ret = 1;
            return ret;
        }
        if(h1[2] -> GetPip() < h2[2] -> GetPip())
        {
            ret = -1;
            return ret;
        }
    
        return compKick(h1, h2);
    }
    
    int SortedEvaluator::compFHouse(const vector<const Card*> h1, const vector<const Card*> h2)
    {
        int ret = 0;
    
        if(h1[2] -> GetPip() > h2[2] -> GetPip())
        {
            ret = 1;
            return ret;
        }
        if(h1[2] -> GetPip() < h2[2] -> GetPip())
        {
            ret = -1;
            return ret;
        }
    
        return compKick(h1, h2);
    }
    
    int SortedEvaluator::compTrips(const vector<const Card*> h1, const vector<const Card*> h2)
    {
        int ret = 0;
    
        if(h1[2] -> GetPip() > h2[2] -> GetPip())
        {
            ret = 1;
            return ret;
        }
        if(h1[2] -> GetPip() < h2[2] -> GetPip())
        {
            ret = -1;
            return ret;
        }
        
        return compKick(h1, h2);
    }
    
    int SortedEvaluator::compTPair(const vector<const Card*> h1, const vector<const Card*> h2)
    {
        int ret = 0;
    
        if(h1[1] -> GetPip() > h2[1] -> GetPip())
        {
            ret = 1;
            return ret;
        }
        if(h1[1] -> GetPip() < h2[1] -> GetPip())
        {
            ret = -1;
            return ret;
        }
        
        if(h1[3] -> GetPip() > h2[3] -> GetPip())
        {
            ret = 1;
            return ret;
        }
        if(h1[3] -> GetPip() < h2[3] -> GetPip())
        {
            ret = -1;
            return ret;
        }
    
        return compKick(h1, h2);
    }
    
    int SortedEvaluator::compPair(const vector<const Card*> h1, const vector<const Card*> h2)
    {
        int PipVal1 = 0;
        int PipVal2 = 0;
    
        int ret = 0;
    
        for(unsigned i = 4; h1[i] != h1[--i]; --i)
        {
            PipVal1 = h1[0] -> GetPip();
        }
    
        for(unsigned i = 4; h2[i] != h2[--i]; --i)
        {
            PipVal2 = h2[0] -> GetPip();
        }
    
        if(PipVal1 > PipVal2)
        {
            ret = 1;
            return ret;
        }
        if(PipVal1 < PipVal2)
        {
            ret = -1;
            return ret;
        }
    
        return compKick(h1, h2);
    }
    Thank you in advance.

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Post the other files! Otherwise it's essentially useless.

    A couple of points:

    Don't write == true and == false all the time. This
    Code:
        if(isFlush(hand1) == true && isStraight(hand1) == true && isFlush(hand2) == true && isStraight(hand2) == true)
    should be
    Code:
        if(isFlush(hand1) && isStraight(hand1) && isFlush(hand2) && isStraight(hand2))
    I mean, if it's true it's true, and that's enough for the &&. Why compare it to true? It's pointless and ugly.

    You have some weird spacing. I've never seen spaces around the -> operator. It's disconcerting.

    Here's an example re-write of one of your functions. Can't say I comprehend the logic, but:
    Code:
    bool SortedEvaluator::isFullHouse(const vector<const Card*> hand)
    {
        return (hand[0]->GetPip() == hand[1]->GetSuit() && hand[2]->GetPip() == hand[4]->GetPip()) ||
               (hand[0]->GetPip() == hand[2]->GetSuit() && hand[3]->GetPip() == hand[4]->GetPip());
    }
    EDIT: Looks like there might be something wrong here:
    Code:
        //Straight
        else if(isStraight(hand1) && isStraight(hand2))
        {
            return compStraight(hand1, hand2);
        }
        else if(isStraight(hand1) == true && isStraight(hand2) == false)
        {
            return 1;    
        }
        else if(isStraight(hand1) == false && isStraight(hand2) == false)
        {
            return -1;    
        }
    Should that last false be true?
    Last edited by oogabooga; 10-02-2012 at 06:39 PM.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Please consider whether you really need to store pointers in your vector or whether you could just store the structs directly.
    At the very least, please use a typedef for vector<const Card*>.

    Also, don't use (void) in C++. You only need to use ()
    Don't write pointless empty constructors and destructors.
    Last edited by iMalc; 10-02-2012 at 11:26 PM.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. please help me finish my Hangman program (C)
    By eddybro in forum C Programming
    Replies: 1
    Last Post: 11-30-2010, 08:52 AM
  2. Have a few minor problems trying to finish this program
    By kisiellll in forum C Programming
    Replies: 4
    Last Post: 02-22-2009, 07:00 PM
  3. How to Close Parent Program using child Program?
    By beon in forum Windows Programming
    Replies: 2
    Last Post: 11-30-2007, 10:24 PM
  4. Trying to finish up program.
    By gator6688 in forum C++ Programming
    Replies: 3
    Last Post: 10-01-2007, 12:23 AM
  5. why won't this program finish?
    By brianptodd in forum C++ Programming
    Replies: 2
    Last Post: 12-02-2002, 10:41 AM

Tags for this Thread