コード例 #1
0
 void PokerHand::rankSort(Hand& hand) // array of cards making up a hand
 {
   std::sort(hand.begin(), hand.end(),
             [](const PlayingCard& firstCard,
                const PlayingCard& secondCard) -> bool
             {
               return firstCard.getRank() < secondCard.getRank();
             });
 }
コード例 #2
0
  bool PokerHand::isStraight(Hand& hand, // array of cards making up a hand
                             bool& isStraightAceLow) // output variable,
                                                     //true if the straight has a low ace
  {
    // does this hand contain an ace
    PokerHand::rankSort(hand);
    Hand::iterator aceLocation;
    aceLocation = std::find_if (hand.begin(), hand.end(),
                                [](PlayingCard& card) -> bool
                                {
                                  return card.getRank() == PlayingCard::Rank::ACE;
                                });
    bool hasAce = hand.end() != aceLocation;

    Hand::iterator it;
    bool hasStraight;
    // determines if the hand is a straight
    if (hasAce)
    {
      it = hand.begin();
      bool straightLowAce = it++->getRank() == PlayingCard::Rank::TWO &&
                            it++->getRank() == PlayingCard::Rank::THREE &&
                            it++->getRank() == PlayingCard::Rank::FOUR &&
                            it->getRank() == PlayingCard::Rank::FIVE;

      it = hand.begin();
      bool straightHighAce = it++->getRank() == PlayingCard::Rank::TEN &&
                             it++->getRank() == PlayingCard::Rank::JACK &&
                             it++->getRank() == PlayingCard::Rank::QUEEN &&
                             it->getRank() == PlayingCard::Rank::KING;

      // is there a straight involving a low valued ace
      // note: this value is returned by reference
      isStraightAceLow = straightLowAce; 
      
      hasStraight = straightLowAce || straightHighAce;
    }
    else
    {
      it = hand.begin();
      PlayingCard::Rank currentRank = it++->getRank();
      
      PlayingCard::Rank nextRank;
      PlayingCard::Rank predictedRank;
      hasStraight = true;
      do
      {
        // increment the current rank to the next one
        int valuePredictedRank = static_cast<int>(currentRank)+1;
        predictedRank = static_cast<PlayingCard::Rank>(valuePredictedRank);
        
        nextRank = it++->getRank();
        
        hasStraight = (nextRank == predictedRank) && hasStraight;
        
        currentRank = nextRank;
      } while (hasStraight && it != hand.end() );
    }
    
    return hasStraight;
  }