string StraightFlashPokerDecisionMaker::decideOrDelegate(PokerHand& a, PokerHand& b){ string response(""); bool aHasStraightFlash = hasFlash(a) && hasStraight(a); bool bHasStraightFlash = hasFlash(b) && hasStraight(b); if(aHasStraightFlash && !bHasStraightFlash){ stringstream ss; ss << a.owner() << " wins. - with straight flash"; response = ss.str(); } else if (!aHasStraightFlash && bHasStraightFlash){ stringstream ss; ss << b.owner() << " wins. - with straight flash"; response = ss.str(); } return response; }
void customTest() { vector<Card> cardSet(7); cardSet[0] = Card(1,Card::Club); cardSet[1] = Card(2,Card::Club); cardSet[2] = Card(3,Card::Club); cardSet[3] = Card(4,Card::Club); cardSet[4] = Card(5,Card::Heart); cardSet[5] = Card(6,Card::Club); cardSet[6] = Card(7,Card::Club); vector<Card> otherSet(7); otherSet[0] = Card(1,Card::Spade); otherSet[1] = Card(2,Card::Spade); otherSet[2] = Card(2,Card::Heart); otherSet[3] = Card(3,Card::Spade); otherSet[4] = Card(4,Card::Spade); otherSet[5] = Card(5,Card::Spade); otherSet[6] = Card(5,Card::Heart); for (int i = 0; i < 7; i++) cout<<cardSet[i].toString()<<' '; cout<<endl; PokerHand result = Judge::determineHand(cardSet); cout<<result.toString()<<endl; for (int i = 0; i < 7; i++) cout<<otherSet[i].toString()<<' '; cout<<endl; PokerHand resultTwo = Judge::determineHand(otherSet); cout<<resultTwo.toString()<<endl; if (result == resultTwo) cout<<"deuce!"<<endl; else if (result < resultTwo) cout<<"The second card set is higher ranked."<<endl; else if (result > resultTwo) cout<<"The first card set is higher ranked."<<endl; return; }
int main(int argc,char **argv) { int m; int n; int o; int p; int q; int r; PokerHand hand; int hand_counts[NUM_HAND_TYPES]; double pct; time_t start_time; time_t end_time; time(&start_time); for (n = 0; n < NUM_HAND_TYPES; n++) hand_counts[n] = 0; for (r = 0; r < POKER_52_5_PERMUTATIONS; r++) { get_permutation_instance_five( NUM_CARDS_IN_DECK, &m,&n,&o,&p,&q,r); hand.NewCards(m,n,o,p,q); hand.Evaluate(); hand_counts[hand.GetHandType()]++; } time(&end_time); for (n = NUM_HAND_TYPES - 1; (n >= 0); n--) { pct = (double)hand_counts[n] / (double)POKER_52_5_PERMUTATIONS; printf("%s %9d %9.6lf\n",hand_type_abbrevs[n],hand_counts[n],pct); } printf("============\n"); printf(" %9d\n",POKER_52_5_PERMUTATIONS); printf("\ncomputation time: %d seconds\n",end_time - start_time); return 0; }
void PokerHand::sortEval () const { PokerHand oldh = *this; PokerHand newh; PokerEvaluation eval = cardSet().evaluateHigh (); Rank r; bool dominor = true; Rank straighttop = Rank::Five(); oldh.sortRanks (); switch (eval.type ()) { // these types have major only case ONE_PAIR: case THREE_OF_A_KIND: case FOUR_OF_A_KIND: dominor = false; // these have major and minor case TWO_PAIR: case FULL_HOUSE: r = eval.majorRank (); while (oldh.cardSet().contains (r)) { Card c = oldh.cardSet().find (r); newh.append (c); oldh.remove (c); } if (dominor) { r = eval.minorRank (); while (oldh.cardSet().contains (r)) { Card c = oldh.cardSet().find (r); newh.append (c); oldh.remove (c); } } // we pass through here, no break case NO_PAIR: case THREE_FLUSH: case THREE_STRAIGHT_FLUSH: case FLUSH: newh.append (oldh); break; // straights may require the ace to swing low case THREE_STRAIGHT: straighttop = Rank::Three(); case STRAIGHT: case STRAIGHT_FLUSH: newh = oldh; r = eval.majorRank (); if (r == straighttop) { Card c = newh.cardSet().find (Rank::Ace()); newh.remove (c); newh.append (c); } break; } for (int i=0; i<_ncards; i++) _cards[i] = newh._cards[i]; }
int main(int argc,char **argv) { int m; int n; int curr_arg; bool bBinFile; char *binfile_name; int cards[NUM_CARDS_IN_HAND]; char card_string[3]; PokerHand hand; struct hand_and_type *hands_and_types; int fhndl; if (argc > 2) { printf(usage); return 1; } bBinFile = false; for (curr_arg = 1; curr_arg < argc; curr_arg++) { if (!strncmp(argv[curr_arg],"-binfile",8)) { bBinFile = true; binfile_name = &argv[curr_arg][8]; } else break; } if (bBinFile) { hands_and_types = (struct hand_and_type *)malloc(sizeof (struct hand_and_type) * POKER_52_5_PERMUTATIONS); if (hands_and_types == NULL) { printf("malloc of hands_and_types failed\n"); return 3; } } card_string[2] = 0; for (m = 0; m < POKER_52_5_PERMUTATIONS; m++) { get_permutation_instance_five( NUM_CARDS_IN_DECK, &cards[0],&cards[1],&cards[2],&cards[3],&cards[4],m); hand.NewCards(cards[0],cards[1],cards[2],cards[3],cards[4]); hand.Evaluate(); if (bBinFile) { for (n = 0; n < NUM_CARDS_IN_HAND; n++) hands_and_types[m].cards[n] = (char)cards[n]; hands_and_types[m].hand_type = (char)hand.GetHandType(); hands_and_types[m].ix = m; } else cout << hand << endl; } if (bBinFile) { if ((fhndl = open(binfile_name, O_CREAT | O_EXCL | O_BINARY | O_WRONLY, S_IREAD | S_IWRITE)) == -1) { printf(couldnt_open,binfile_name); return 4; } write(fhndl,hands_and_types,sizeof (struct hand_and_type) * POKER_52_5_PERMUTATIONS); close(fhndl); free(hands_and_types); } return 0; }
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; }
void randomTest() { srand(time(0)); vector<Card> cards(52); for (int i = 0; i < 52; i++) { cards[i] = Card(1 + i % 13, static_cast<Card::Suit> (1 + i % 4) ); } vector<Card> cardSet(7); bool same = false; do { same = false; for (int i = 0; i < 7; i++) { cardSet[i] = cards[rand() % 52]; cout<<cardSet[i].toString()<<' '; } cout<<endl; for (int j = 0; j < 6; j++) for (int k = j + 1; k < 7; k++) if (cardSet[j].getValue() == cardSet[k].getValue() && cardSet[j].getSuit() == cardSet[k].getSuit()) same = true; }while(same); PokerHand result = Judge::determineHand(cardSet); cout<<result.toString()<<endl; vector<Card> otherSet(7); do { same = false; for (int i = 0; i < 7; i++) { otherSet[i] = cards[rand() % 52]; cout<<otherSet[i].toString()<<' '; } cout<<endl; for (int j = 0; j < 6; j++) for (int k = j + 1; k < 7; k++) if (otherSet[j].getValue() == otherSet[k].getValue() && otherSet[j].getSuit() == otherSet[k].getSuit()) same = true; }while(same); PokerHand resultTwo = Judge::determineHand(otherSet); cout<<resultTwo.toString()<<endl; if (result == resultTwo) cout<<"deuce!"<<endl; else if (result < resultTwo) cout<<"The second card set is higher ranked."<<endl; else if (result > resultTwo) cout<<"The first card set is higher ranked."<<endl; return; }