Programming Challenges – 포커 패 (Poker Hands)

https://polyploid.net/blog/?p=jpmq65qs https://www.psicologialaboral.net/2024/08/07/imeipkyd 문제 <- 클릭

별로 어렵진 않은데 시간이 많이 걸리는 예제 였다. 입출력이 까답롭지 않아서 인지 금방 성공 하긴 했지만 시간 날때 좀더 다듬고 싶다.

https://udaan.org/zzgxqkw7n.php

#include #include #include #include #include #include struct _Card { int pattern; int value; bool operator==(const _Card& rhs) { return (*this).value == rhs.value; } }; typedef struct _Card Card; typedef std::vector CardSet; class CompareValue { public: bool operator()(const Card& lhs, const Card& rhs) { return lhs.value > rhs.value; } }; class PokerCardSet { private: enum { CLUB, DIAMOND, HEART, SPADE, TEN = 10, JACK = 11, QUEEN = 12, KING = 13, ACE = 14 }; enum { BLACK, WHITE }; CardSet cardSet; int score; void setCard(char value, char pattern) { Card card; switch (pattern) { case 'C': card.pattern = CLUB; break; case 'D': card.pattern = DIAMOND; break; case 'H': card.pattern = HEART; break; case 'S': card.pattern = SPADE; break; default: card.pattern = pattern - '0'; break; } switch (value) { case 'T': card.value = TEN; break; case 'J': card.value = JACK; break; case 'Q': card.value = QUEEN; break; case 'K': card.value = KING; break; case 'A': card.value = ACE; break; default: card.value = value - '0'; break; } cardSet.push_back(card); } int highCard() { return score = highCard(cardSet); } int highCard(CardSet& cardSet) { std::sort(cardSet.begin(), cardSet.end(), CompareValue()); int sumCardValue = 0; int digit = 0; for (CardSet::reverse_iterator it = cardSet.rbegin(); it != cardSet.rend(); ++it) { sumCardValue += (*it).value * std::pow((double) 15, (double) digit++); } return sumCardValue; } int onePair() { int compareCounter = 0; Card currentCard; int pairCounter = 0; CardSet diffrentSet; CardSet sameSet; CardSet returnCardSet; std::sort(cardSet.begin(), cardSet.end(), CompareValue()); for (CardSet::iterator it1 = cardSet.begin(); it1 != cardSet.end(); ++it1) { compareCounter = 0; currentCard = *it1; for (CardSet::iterator it2 = cardSet.begin(); it2 != cardSet.end(); ++it2) { if (*it1 == *it2) ++compareCounter; } if (compareCounter == 2) { sameSet.push_back(currentCard); pairCounter++; } else if (compareCounter == 1) { diffrentSet.push_back(currentCard); } } diffrentSet.insert(diffrentSet.begin(), sameSet.begin(), sameSet.end()); if (pairCounter == 2) return score = 2000000 + highCard(diffrentSet); else return 0; } int twoPair() { int compareCounter = 0; Card currentCard; int pairCounter = 0; CardSet returnCardSet; std::sort(cardSet.begin(), cardSet.end(), CompareValue()); Card diffrentCard; for (CardSet::iterator it1 = cardSet.begin(); it1 != cardSet.end(); ++it1) { compareCounter = 0; for (CardSet::iterator it2 = cardSet.begin(); it2 != cardSet.end(); ++it2) { if (*it1 == *it2) { ++compareCounter; currentCard = *it1; } else { diffrentCard = *it1; } } if (compareCounter == 2) { returnCardSet.push_back(currentCard); pairCounter++; } } returnCardSet.push_back(diffrentCard); if (pairCounter == 4) return score = 3000000 + highCard(returnCardSet); else return 0; } int threeOfAKind() { int compareCounter = 0; int currentCard = 0; for (CardSet::iterator it1 = cardSet.begin(); it1 != cardSet.end(); ++it1) { compareCounter = 0; currentCard = (*it1).value; for (CardSet::iterator it2 = cardSet.begin(); it2 != cardSet.end(); ++it2) { if (*it1 == *it2) ++compareCounter; } if (compareCounter == 3) { return score = 4000000 + currentCard; } } return 0; } int straight() { std::sort(cardSet.begin(), cardSet.end(), CompareValue()); CardSet::iterator it; it = cardSet.begin(); int value = (*it++).value; int bigCard = value; for (; it != cardSet.end(); ++it) { if (--value != (*it).value) return 0; } return score = 5000000 + bigCard; } int flush() { CardSet::iterator it; it = cardSet.begin(); int pattern = (*it++).pattern; for (; it != cardSet.end(); ++it) { if (pattern != (*it).pattern) return 0; } return score = 6000000 + highCard(cardSet); } int fullHouse() { int threeCard = 0; int twoCard = 0; int compareCounter = 0; int currentCard = 0; for (CardSet::iterator it1 = cardSet.begin(); it1 != cardSet.end(); ++it1) { compareCounter = 0; currentCard = (*it1).value; for (CardSet::iterator it2 = cardSet.begin(); it2 != cardSet.end(); ++it2) { if (*it1 == *it2) { ++compareCounter; } } if (compareCounter == 3) { threeCard = currentCard; } if (compareCounter == 2) { twoCard = currentCard; } } if (threeCard && twoCard) return score = 7000000 + threeCard; else return 0; } int fourOfAKind() { for (CardSet::iterator it1 = cardSet.begin(); it1 != cardSet.end(); ++it1) { int compareCounter = 0; int returnCard = (*it1).value; for (CardSet::iterator it2 = cardSet.begin(); it2 != cardSet.end(); ++it2) { if (*it1 == *it2) ++compareCounter; } if (compareCounter == 4) return score = 8000000 + returnCard; } return 0; } int straightFlush() { CardSet::iterator it; it = cardSet.begin(); int pattern = (*it).pattern; for (; it != cardSet.end(); ++it) { if (pattern != (*it).pattern) return 0; } std::sort(cardSet.begin(), cardSet.end(), CompareValue()); it = cardSet.begin(); int value = (*it).value; int bigCard = value; ++it; for (; it != cardSet.end(); ++it) { if (--value != (*it).value) return 0; } return score = 9000000 + bigCard; } void getScore() { if (straightFlush()) return; else if (fourOfAKind()) return; else if (fullHouse()) return; else if (flush()) return; else if (straight()) return; else if (threeOfAKind()) return; else if (twoPair()) return; else if (onePair()) return; else if (highCard()) return; else return; } public: PokerCardSet() : score(0) { } PokerCardSet(std::string str) { setCards(str); } void setCards(std::string str) { std::stringstream ss(str); std::string strCard; for (int i = 0; i < 5; ++i) { ss >> strCard; setCard(strCard[0], strCard[1]); } getScore(); } bool operator>(const PokerCardSet& rhs) { return (*this).score > rhs.score; } bool operator<(const PokerCardSet& rhs) { return (*this).score < rhs.score; } bool operator==(const PokerCardSet& rhs) { return (*this).score == rhs.score; } }; class Poker { PokerCardSet blackSet; PokerCardSet whiteSet; public: Poker(std::string str) { blackSet.setCards(str.substr(0, 14)); whiteSet.setCards(str.substr(15, str.length())); } void play() { if (blackSet > whiteSet) std::cout << "Black wins." << std::endl; else if (whiteSet > blackSet) std::cout << "White wins." << std::endl; else if (blackSet == whiteSet) std::cout << "Tie." << std::endl; } }; int main() { std::string buffer; while (true) { std::getline(std::cin, buffer); if (buffer.empty()) break; Poker poker(buffer); poker.play(); } }

답글 남기기

https://transculturalexchange.org/jf0o1exi1 이메일 주소는 공개되지 않습니다. https://solomedicalsupply.com/2024/08/07/qwmknufs7f0 필수 필드는 *로 표시됩니다

https://www.clawscustomboxes.com/6xg4zgg

https://oevenezolano.org/2024/08/vph7udpq You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

https://mandikaye.com/blog/a3d0oog

https://nedediciones.com/uncategorized/jif5wk9j

https://sugandhmalhotra.com/2024/08/07/phb7u159e

https://inteligencialimite.org/2024/08/07/jxexqwl

https://nedediciones.com/uncategorized/e2dtzg05

https://udaan.org/yircy92o7.php

https://www.completerehabsolutions.com/blog/ogms33bj7p

https://www.clawscustomboxes.com/g80qe6w

이 사이트는 스팸을 줄이는 아키스밋을 사용합니다. 댓글이 어떻게 처리되는지 알아보십시오.

https://www.completerehabsolutions.com/blog/m30q77y9