int PokerHand::cmp(PokerHand &otherHand) { if (m_rank < otherHand.getRank()) { return -1; } else if (m_rank > otherHand.getRank()) { return 1; } else { //It's tied... woah boy... switch (m_rank) { case RoyalFlush: // Since we aren't ranking by suit as well, all Royal Flushes are tied. return 0; // No need for breaks since we already returned. case StraightFlush: if (otherHand.m_cards[FIVE - 1].points() < m_cards[FIVE - 1].points()) { return 1; } else if (otherHand.m_cards[FIVE - 1].points() > m_cards[FIVE - 1].points()) { return -1; } else { return 0; } break; case FourOfAKind: if (otherHand.m_quadrupletPoints < m_quadrupletPoints) { return 1; } else if (otherHand.m_quadrupletPoints > m_quadrupletPoints) { return -1; } else { if (otherHand.m_lastCardPoints < m_lastCardPoints) { return 1; } else if (otherHand.m_lastCardPoints > m_lastCardPoints) { return -1; } else { return 0; } } case FullHouse: if (otherHand.m_tripletPoints < m_tripletPoints) { return 1; } else if (otherHand.m_tripletPoints > m_tripletPoints) { return -1; } else { if (otherHand.m_firstPairPoints < m_firstPairPoints) { return 1; } else if (otherHand.m_firstPairPoints > m_firstPairPoints) { return -1; } else { return 0; } } case Flush: for (int i = FIVE - 1; i >= 0; i--) { if (otherHand.m_cards[i].points() < m_cards[i].points()) { return 1; } else if (otherHand.m_cards[i].points() > m_cards[i].points()) { return -1; } } // If all of them are the same. return 0; case Straight: if (otherHand.m_cards[FIVE - 1].points() < m_cards[FIVE - 1].points()) { return 1; } else if (otherHand.m_cards[FIVE - 1].points() > m_cards[FIVE - 1].points()) { return -1; } else { return 0; } case ThreeOfAKind: if (otherHand.m_tripletPoints < m_tripletPoints) { return 1; } else if (otherHand.m_tripletPoints > m_tripletPoints) { return -1; } else { for (int i = FIVE - 4; i >= 0; i--) { if (otherHand.m_cards[i].points() < m_cards[i].points()) { return 1; } else if (otherHand.m_cards[i].points() > m_cards[i].points()) { return -1; } } return 0; } case TwoPair: if (otherHand.m_firstPairPoints < m_firstPairPoints) { return 1; } else if (otherHand.m_firstPairPoints > m_firstPairPoints) { return -1; } else { if (otherHand.m_secondPairPoints < m_secondPairPoints) { return 1; } else if (otherHand.m_secondPairPoints > m_secondPairPoints) { return -1; } else { if (otherHand.m_lastCardPoints < m_lastCardPoints) { return 1; } else if (otherHand.m_lastCardPoints > m_lastCardPoints) { return -1; } else { return 0; } } } case OnePair: if (otherHand.m_firstPairPoints < m_firstPairPoints) { return 1; } else if (otherHand.m_firstPairPoints > m_firstPairPoints) { return -1; } else { for (int i = FIVE - 3; i >= 0; i--) { if (otherHand.m_cards[i].points() < m_cards[i].points()) { return 1; } else if (otherHand.m_cards[i].points() > m_cards[i].points()) { return -1; } } return 0; } case HighCard: for (int i = FIVE - 1; i >= 0; i--) { if (otherHand.m_cards[i].points() < m_cards[i].points()) { return 1; } else if (otherHand.m_cards[i].points() > m_cards[i].points()) { return -1; } } return 0; case NoRank: // Indeterminate return 0; default: return 0; } } }
int main() { cout << "ALL FUNCTIONS SHOULD WORK." << endl; // Call the card constructors cout << "Calling card constructors." << endl; Card cFour(Card::Clubs, 4); Card dFour(Card::Diamonds, 4); Card sTen(Card::Spades, 10); Card hJack(Card::Hearts, 11); Card hQueen(Card::Hearts, 12); Card cQueen(Card::Clubs, 12); Card sQueen(Card::Spades, 12); Card hKing(Card::Hearts, 13); Card hAce(Card::Hearts, 14); Card defaultConstruct; cout << endl; cout << "Calling card print methods." << endl; sQueen.print(); cFour.print(); cQueen.print(); dFour.print(); hQueen.print(); defaultConstruct.print(); cout << endl; cout << "Printing cards using .suit() and .points()" << endl; cout << pointsToText(sQueen.points()) << " of " << suitToText(sQueen.suit()) << endl; cout << pointsToText(cFour.points()) << " of " << suitToText(cFour.suit()) << endl; cout << pointsToText(cQueen.points()) << " of " << suitToText(cQueen.suit()) << endl; cout << pointsToText(dFour.points()) << " of " << suitToText(dFour.suit()) << endl; cout << pointsToText(hQueen.points()) << " of " << suitToText(hQueen.suit()) << endl; cout << endl; cout << "Creating hand and empty hand using constructors" << endl; PokerHand emptyHand; PokerHand fullHouseQ4(sQueen, cFour, cQueen, dFour, hQueen); PokerHand twoPairQ4A(cFour, hQueen, cQueen, dFour, hAce); PokerHand twoPairQ4J(dFour, hJack, hQueen, cQueen, cFour); PokerHand highAKQ104(dFour, hQueen, hKing, sTen, hAce); PokerHand highAKJ104(hKing, sTen, hAce, hJack, cFour); cout << endl; cout << "Printing cards using PokerHand::printCards()" << endl; cout << "Empty Hand:" << endl; emptyHand.printCards(); cout << "Full House Q-4:" << endl; fullHouseQ4.printCards(); cout << "High AKJ104:" << endl; highAKJ104.printCards(); cout << endl; cout << "Printing rank using PokerHand::printRank()" << endl; emptyHand.printRank(); fullHouseQ4.printRank(); cout << endl; // All the PokerHand::is...() functions are called in PokerHand::getRank() when it ran in the constructor. cout << "Print rank from getRank()" << endl; cout << rankToText(emptyHand.getRank()) << endl; cout << rankToText(fullHouseQ4.getRank()) << endl; cout << endl; cout << "Print is___() method results" << endl; cout << "isRoyalFlush(): " << fullHouseQ4.isRoyalFlush() << endl; cout << "isStraightFlush(): " << fullHouseQ4.isStraightFlush() << endl; cout << "isFourOfAKind(): " << fullHouseQ4.isFourOfAKind() << endl; cout << "isFullHouse(): " << fullHouseQ4.isFullHouse() << endl; cout << "isFlush(): " << fullHouseQ4.isFlush() << endl; cout << "isStraight(): " << fullHouseQ4.isStraight() << endl; cout << "isThreeOfAKind(): " << fullHouseQ4.isThreeOfAKind() << endl; cout << "isTwoPair(): " << fullHouseQ4.isTwoPair() << endl; cout << "isOnePair(): " << fullHouseQ4.isOnePair() << endl; cout << "isHighCard(): " << fullHouseQ4.isHighCard() << endl; cout << "These were called in getRank() when it first ran with NoRank called from the constructor." << endl; cout << endl; cout << "A couple comparisons."; cout << "Empty Host vs. Q-4 FullHouse: " << resultToText(emptyHand.cmp(fullHouseQ4)) << endl; cout << "Q-4-A 2P Host vs. Q-4 FullHouse: " << resultToText(twoPairQ4A.cmp(fullHouseQ4)) << endl; cout << "Q-4 FH Host vs. Q-4-J 2P: " << resultToText(fullHouseQ4.cmp(twoPairQ4J)) << endl; cout << "Q-4-A 2P Host vs. Q-4-J 2P: " << resultToText(twoPairQ4A.cmp(twoPairQ4J)) << endl; cout << "A-K-Q-10-4 High Host vs. A-K-J-10-4 High: " << resultToText(highAKQ104.cmp(highAKJ104)) << endl; cout << endl; return 0; }