Everything compiles fine but it does not identify a straight. It will find straight flush though. A hand consists of 5 objects of the class CARDS which has 3 int variables(Value, Suit, Position). When I send a straight through it it goes all the way through to else and returns "Error!" I cannot figure out why please help me out. I went over the code many times but nothing stands out, it may just need a fresh set of eyes. If it is too confusing to figure out quickly tell me and I will add comments to explain everything.

string GetHandName(HAND5 hand){
        bool Sorting = true;
        CARD Move;
        string HandName;
        //sort from smallest to biggest
        while (Sorting){
            if (hand.Card[0].Value > hand.Card[1].Value){
                Move = hand.Card[0];
                hand.Card[0] = hand.Card[1];
                hand.Card[1] = Move;
            }
            else if (hand.Card[1].Value > hand.Card[2].Value){
                Move = hand.Card[1];
                hand.Card[1] = hand.Card[2];
                hand.Card[2] = Move;
            }
            else if (hand.Card[2].Value > hand.Card[3].Value){
                Move = hand.Card[2];
                hand.Card[2] = hand.Card[3];
                hand.Card[3] = Move;
            }
            else if (hand.Card[3].Value > hand.Card[4].Value){
                Move = hand.Card[3];
                hand.Card[3] = hand.Card[4];
                hand.Card[4] = Move;
            }
            else{Sorting = false;}
        }
        //check for straight flush and 5 high exception (A,2,3,4,5)
        if (hand.Card[0].Value == hand.Card[1].Value - 1 &&
            hand.Card[1].Value == hand.Card[2].Value - 1 &&
            hand.Card[2].Value == hand.Card[3].Value - 1 &&
            hand.Card[3].Value == hand.Card[4].Value - 1){
            if (hand.Card[0].Suit == hand.Card[1].Suit &&
                hand.Card[0].Suit == hand.Card[2].Suit &&
                hand.Card[0].Suit == hand.Card[3].Suit &&
                hand.Card[0].Suit == hand.Card[4].Suit){
                if (hand.Card[4].Value == 13){HandName = "Royal Flush!!";}
                else {
                    HandName = "Straight Flush, ";
                    HandName = HandName + GetCardValueL(hand.Card[4]);
                    HandName.append(" High");
                    return HandName;
                }
            }
        }
        else if (hand.Card[0].Value == hand.Card[1].Value - 1 &&
                 hand.Card[1].Value == hand.Card[2].Value - 1 &&
                 hand.Card[2].Value == hand.Card[3].Value - 1 &&
                 hand.Card[4].Value == Ace){
                     if (hand.Card[0].Suit == hand.Card[1].Suit &&
                         hand.Card[0].Suit == hand.Card[2].Suit &&
                         hand.Card[0].Suit == hand.Card[3].Suit &&
                         hand.Card[0].Suit == hand.Card[4].Suit){
                             HandName = "Straight Flush, ";
                             HandName = HandName + GetCardValueL(hand.Card[5]);
                             HandName.append(" High");
                             return HandName;
            }
        }


        //check for four of a kind
        else if ((hand.Card[0].Value == hand.Card[1].Value &&
                  hand.Card[0].Value == hand.Card[2].Value &&
                  hand.Card[0].Value == hand.Card[3].Value) ||
                 (hand.Card[1].Value == hand.Card[2].Value &&
                  hand.Card[1].Value == hand.Card[3].Value &&
                  hand.Card[1].Value == hand.Card[4].Value)){
            HandName = "Four of a Kind, ";
            HandName = HandName + GetCardValueL(hand.Card[3]);
            HandName.append("'s");
            return HandName;
        }

        //check for full house
        else if (hand.Card[0].Value == hand.Card[1].Value &&
                  hand.Card[2].Value == hand.Card[3].Value &&
                  hand.Card[2].Value == hand.Card[4].Value){
                      HandName = "Full House, ";
                      HandName = HandName + GetCardValueL(hand.Card[2]);
                      HandName.append("'s Full of ");
                      HandName = HandName + GetCardValueL(hand.Card[0]);
                      HandName.append("'s");
                      return HandName;
        }
        else if (hand.Card[0].Value == hand.Card[1].Value &&
                 hand.Card[0].Value == hand.Card[2].Value &&
                 hand.Card[3].Value == hand.Card[4].Value){
                     HandName = "Full House, ";
                      HandName = HandName + GetCardValueL(hand.Card[0]);
                      HandName.append("'s Full of ");
                      HandName = HandName + GetCardValueL(hand.Card[3]);
                      HandName.append("'s");
                      return HandName;
        }

        //check for flush
        else if (hand.Card[0].Suit == hand.Card[1].Suit &&
                 hand.Card[0].Suit == hand.Card[2].Suit &&
                 hand.Card[0].Suit == hand.Card[3].Suit &&
                 hand.Card[0].Suit == hand.Card[4].Suit){
            HandName = "Flush, ";
            HandName = HandName + GetCardValueL(hand.Card[4]);
            HandName.append(" High");
            return HandName;
        }

        //check for straight and 5 high exception (A,2,3,4,5)
        else if (hand.Card[0].Value == hand.Card[1].Value - 1 &&
                hand.Card[1].Value == hand.Card[2].Value - 1 &&
                hand.Card[2].Value == hand.Card[3].Value - 1 &&
                hand.Card[3].Value == hand.Card[4].Value - 1){
                    HandName = "Straight, ";
                    HandName = HandName + GetCardValueL(hand.Card[4]);
                    HandName.append(" High");
                    return HandName;
        }

        else if (hand.Card[0].Value == hand.Card[1].Value - 1 &&
                 hand.Card[1].Value == hand.Card[2].Value - 1 &&
                 hand.Card[2].Value == hand.Card[3].Value - 1 &&
                 hand.Card[4].Value == Ace){HandName = "Straight, Five High"; return HandName;}

        //check for three of a kind
        else if ((hand.Card[0].Value == hand.Card[1].Value &&
                  hand.Card[0].Value == hand.Card[2].Value) ||
                 (hand.Card[1].Value == hand.Card[2].Value &&
                  hand.Card[1].Value == hand.Card[3].Value) ||
                 (hand.Card[2].Value == hand.Card[3].Value &&
                  hand.Card[2].Value == hand.Card[4].Value)){
            HandName = "Three of a Kind, ";
            HandName = HandName + GetCardValueL(hand.Card[2]);
            HandName.append("'s");
            return HandName;
        }

        //check for two pair
        else if ((hand.Card[0].Value == hand.Card[1].Value &&
                  hand.Card[2].Value == hand.Card[3].Value) ||
                 (hand.Card[0].Value == hand.Card[1].Value &&
                  hand.Card[3].Value == hand.Card[4].Value)){
                      HandName = "Two Pair, ";
                      HandName = HandName + GetCardValueL(hand.Card[3]);
                      HandName.append("'s and ");
                      HandName = HandName + GetCardValueL(hand.Card[0]);
                      HandName.append("'s");
                      return HandName;
        }

        else if (hand.Card[1].Value == hand.Card[2].Value &&
                 hand.Card[3].Value == hand.Card[4].Value){
                     HandName = "Two Pair, ";
                     HandName = HandName + GetCardValueL(hand.Card[4]);
                     HandName.append("'s and ");
                     HandName = HandName + GetCardValueL(hand.Card[1]);
                     HandName.append("'s");
                     return HandName;
        }

        //check for pair
        else if (hand.Card[0].Value == hand.Card[1].Value ||
                 hand.Card[1].Value == hand.Card[2].Value){
                     HandName = "Pair of ";
                     HandName = HandName + GetCardValueL(hand.Card[1]);
                     HandName.append("'s");
        }

        else if (hand.Card[2].Value == hand.Card[3].Value ||
                 hand.Card[3].Value == hand.Card[4].Value){
                     HandName = "Pair of ";
                     HandName = HandName + GetCardValueL(hand.Card[3]);
                     HandName.append("'s");
        }
        else {
            HandName = GetCardValueL(hand.Card[4]);
            HandName.append(" ");
            HandName = HandName + GetCardValueL(hand.Card[3]);
            HandName.append(" High");
            return HandName;
        }


        return "Error!";

    }

Recommended Answers

All 2 Replies

I think what's happening is when you send in an "ordinary" straight, it gets caught by the striaght flush if test. It doesn't pass the test for flush, and then falls out of the entire if/else block.

I think it would better to simply test first for straight. If a straight, test that for flush, if that passes test for royal.

Also, your test for the Ace-thru-5 is incorrect. It will classify any run of 4 cards, plus an ace, as the A-5 sequence.

So, overall logic might go something like:

if(  is straight )
 {
       if( all same suit )
       {
            if ( high card is ace )
                  royal flush
            else
                  straight flush
         }
    else
        plain straight
  }

  else if( values 2-5 )
  {
      if ( high card is ace )
      {
             if( all same suit )
                 straight flush
             else
                 5 high straight
         }
         else
             junk hand, ace high
  }

else if (   //continue tests

Thank you, I think this will fix it. I will probably do a quick fix instead of rewriting it. I am going to just merge the two if statements on the straight flush so it wont go inside the if statement just for straight and that should fix it, if not then I will rewrite.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.