Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ///////////////////////////////////////////////////////////
- #include <iostream>
- #include <string>
- #include <vector>
- #include <sstream>
- #include <set>
- using namespace std;
- ///////////////////////////////////////////////////////////
- int getCardValue(char cardFirstChar)
- {
- int value;
- switch (cardFirstChar)
- {
- case 'T':
- {
- value = 10;
- break;
- }
- case 'J':
- {
- value = 11;
- break;
- }
- case 'Q':
- {
- value = 12;
- break;
- }
- case 'K':
- {
- value = 13;
- break;
- }
- case 'A':
- {
- value = 14;
- break;
- }
- default:
- {
- value = (int)cardFirstChar - '0';
- }
- }
- return value;
- }
- int getHighestCardIndex(vector<string> hand)
- {
- int highestCard = 0;
- int highestCardIndex = 0;
- for (int i = 0; i < hand.size(); i++)
- {
- int cardValue = getCardValue(hand.at(i)[0]);
- if (cardValue > highestCard)
- {
- highestCard = cardValue;
- highestCardIndex = i;
- }
- }
- return highestCardIndex;
- }
- int getHighestCardValue(vector<string> hand, int order)
- {
- set<int> handValues;
- for (int i = 0; i < hand.size(); i++)
- {
- handValues.insert(getCardValue(hand.at(i)[0]));
- }
- int index = 0;
- set<int>::iterator iter = handValues.end();
- while (iter != handValues.begin())
- {
- if (index == order)
- {
- iter--;
- return *iter;
- }
- else
- {
- index++;
- iter--;
- }
- }
- return 0;
- }
- int getLowestCardIndex(vector<string> hand)
- {
- int lowestCard = 99999;
- int lowestCardIndex = 0;
- for (int i = 0; i < hand.size(); i++)
- {
- int cardValue = getCardValue(hand.at(i)[0]);
- if (cardValue < lowestCard)
- {
- lowestCard = cardValue;
- lowestCardIndex = i;
- }
- }
- return lowestCardIndex;
- }
- bool areNumbersDistinct(vector<string> hand)
- {
- for (int i = 0; i < hand.size() - 1; i++)
- {
- for (int j = i + 1; j < hand.size(); j++)
- {
- if (hand.at(i)[0] == hand.at(j)[0])
- {
- return false;
- }
- }
- }
- return true;
- }
- bool areNumbersConsecutive(vector<string> hand)
- {
- return getCardValue(hand.at(getHighestCardIndex(hand))[0]) - getCardValue(hand.at(getLowestCardIndex(hand))[0]) + 1 == hand.size();
- }
- ///////////////////////////////////////////////////////////
- bool isPair(vector<string> hand)
- {
- for (int i = 0; i < hand.size() - 1; i++)
- {
- for (int j = i + 1; j < hand.size(); j++)
- {
- if (hand.at(i)[0] == hand.at(j)[0])
- {
- return true;
- }
- }
- }
- return false;
- }
- bool isPair(vector<string> hand, int excludedCardValue)
- {
- for (int i = 0; i < hand.size() - 1; i++)
- {
- if (getCardValue(hand.at(i)[0]) != excludedCardValue)
- {
- for (int j = i + 1; j < hand.size(); j++)
- {
- if (hand.at(i)[0] == hand.at(j)[0])
- {
- return true;
- }
- }
- }
- }
- return false;
- }
- bool isTwoPairs(vector<string> hand)
- {
- int pairCount = 0;
- for (int i = 0; i < hand.size() - 1; i++)
- {
- for (int j = i + 1; j < hand.size(); j++)
- {
- if (hand.at(i)[0] == hand.at(j)[0])
- {
- if (++pairCount == 2)
- {
- return true;
- }
- break;
- }
- }
- }
- return false;
- }
- int getHighestPairValue(vector<string> hand)
- {
- int highestPairValue = 0;
- for (int i = 0; i < hand.size() - 1; i++)
- {
- for (int j = i + 1; j < hand.size(); j++)
- {
- if (hand.at(i)[0] == hand.at(j)[0])
- {
- int cardValue = getCardValue(hand.at(i)[0]);
- if (cardValue > highestPairValue)
- {
- highestPairValue = cardValue;
- }
- break;
- }
- }
- }
- return highestPairValue;
- }
- int getLowestPairValue(vector<string> hand)
- {
- int lowestPairValue = 15;
- for (int i = 0; i < hand.size() - 1; i++)
- {
- for (int j = i + 1; j < hand.size(); j++)
- {
- if (hand.at(i)[0] == hand.at(j)[0])
- {
- int cardValue = getCardValue(hand.at(i)[0]);
- if (cardValue < lowestPairValue)
- {
- lowestPairValue = cardValue;
- }
- break;
- }
- }
- }
- return lowestPairValue == 15 ? 0 : lowestPairValue;
- }
- int getHighestNonPairValue(vector<string> hand)
- {
- vector<char> pairs;
- int highestNonPairValue = 0;
- for (int i = 0; i < hand.size(); i++)
- {
- bool pairFound = false;
- for (int j = 0; j < pairs.size(); j++)
- {
- if (pairs.at(j) == hand.at(i)[0])
- {
- pairFound = true;
- break;
- }
- }
- if (!pairFound)
- {
- for (int j = i + 1; j < hand.size(); j++)
- {
- if (hand.at(i)[0] == hand.at(j)[0])
- {
- pairFound = true;
- pairs.push_back(hand.at(i)[0]);
- break;
- }
- }
- }
- if (!pairFound)
- {
- highestNonPairValue = getCardValue(hand.at(i)[0]);
- }
- }
- return highestNonPairValue;
- }
- int getHighestNonPairValue(vector<string> hand, int order)
- {
- set<int> handValues;
- vector<char> pairs;
- for (int i = 0; i < hand.size(); i++)
- {
- bool pairFound = false;
- for (int j = 0; j < pairs.size(); j++)
- {
- if (pairs.at(j) == hand.at(i)[0])
- {
- pairFound = true;
- break;
- }
- }
- if (!pairFound)
- {
- for (int j = i + 1; j < hand.size(); j++)
- {
- if (hand.at(i)[0] == hand.at(j)[0])
- {
- pairFound = true;
- pairs.push_back(hand.at(i)[0]);
- break;
- }
- }
- }
- if (!pairFound)
- {
- handValues.insert(getCardValue(hand.at(i)[0]));
- }
- }
- int index = 0;
- set<int>::iterator iter = handValues.end();
- while (iter != handValues.begin())
- {
- if (index == order)
- {
- iter--;
- return *iter;
- }
- else
- {
- index++;
- iter--;
- }
- }
- return 0;
- }
- bool isThreeOfAKind(vector<string> hand)
- {
- for (int i = 0; i < hand.size() - 1; i++)
- {
- int cardAmount = 1;
- for (int j = i + 1; j < hand.size(); j++)
- {
- if (hand.at(i)[0] == hand.at(j)[0])
- {
- if (++cardAmount == 3)
- {
- return true;
- }
- }
- }
- }
- return false;
- }
- int getThreeOfAKindValue(vector<string> hand)
- {
- int cardValue = 0;
- for (int i = 0; i < hand.size() - 1; i++)
- {
- int cardAmount = 1;
- for (int j = i + 1; j < hand.size(); j++)
- {
- if (hand.at(i)[0] == hand.at(j)[0])
- {
- if (++cardAmount == 3)
- {
- cardValue = getCardValue(hand.at(i)[0]);
- }
- }
- }
- }
- return cardValue;
- }
- bool isFullHouse(vector<string> hand)
- {
- if (isThreeOfAKind(hand))
- {
- int cardValue = getThreeOfAKindValue(hand);
- return isPair(hand, cardValue);
- }
- return false;
- }
- bool isFourOfAKind(vector<string> hand)
- {
- for (int i = 0; i < hand.size() - 1; i++)
- {
- int cardAmount = 1;
- for (int j = i + 1; j < hand.size(); j++)
- {
- if (hand.at(i)[0] == hand.at(j)[0])
- {
- if (++cardAmount == 4)
- {
- return true;
- }
- }
- }
- }
- return false;
- }
- bool isStraight(vector<string> hand)
- {
- return areNumbersDistinct(hand) && areNumbersConsecutive(hand);
- }
- bool isFlush(vector<string> hand)
- {
- return hand.at(0)[1] == hand.at(1)[1] &&
- hand.at(0)[1] == hand.at(2)[1] &&
- hand.at(0)[1] == hand.at(3)[1] &&
- hand.at(0)[1] == hand.at(4)[1];
- }
- ///////////////////////////////////////////////////////////
- int main()
- {
- string input;
- while (getline(cin, input))
- {
- ///////////////////////////////////////////////////
- vector<string> firstHand;
- vector<string> secondHand;
- int i = 0;
- istringstream iss(input);
- for (string result; iss >> result; )
- {
- if (i < 5)
- {
- firstHand.push_back(result);
- }
- else
- {
- secondHand.push_back(result);
- }
- i++;
- }
- ///////////////////////////////////////////////////
- // Straight Flush
- if (isStraight(firstHand) && isFlush(firstHand))
- {
- if (isStraight(secondHand) && isFlush(secondHand))
- {
- if (getCardValue(firstHand.at(getHighestCardIndex(firstHand))[0]) == getCardValue(secondHand.at(getHighestCardIndex(secondHand))[0]))
- {
- cout << "Tie.";
- }
- else if (getCardValue(firstHand.at(getHighestCardIndex(firstHand))[0]) > getCardValue(secondHand.at(getHighestCardIndex(secondHand))[0]))
- {
- cout << "Black wins.";
- }
- else
- {
- cout << "White wins.";
- }
- }
- else
- {
- cout << "Black wins.";
- }
- }
- else if (isStraight(secondHand) && isFlush(secondHand))
- {
- cout << "White wins.";
- }
- // Four of a Kind
- else if (isFourOfAKind(firstHand))
- {
- if (isFourOfAKind(secondHand))
- {
- int fourOfAKindValue_1 = getThreeOfAKindValue(firstHand);
- int fourOfAKindValue_2 = getThreeOfAKindValue(secondHand);
- if (fourOfAKindValue_1 == fourOfAKindValue_2)
- {
- cout << "Tie.";
- }
- else if (fourOfAKindValue_1 > fourOfAKindValue_2)
- {
- cout << "Black wins.";
- }
- else
- {
- cout << "White wins.";
- }
- }
- else
- {
- cout << "Black wins.";
- }
- }
- else if (isFourOfAKind(secondHand))
- {
- cout << "White wins.";
- }
- // Full House
- else if (isFullHouse(firstHand))
- {
- if (isFullHouse(secondHand))
- {
- int fullHouseValue_1 = getThreeOfAKindValue(firstHand);
- int fullHouseValue_2 = getThreeOfAKindValue(secondHand);
- if (fullHouseValue_1 == fullHouseValue_2)
- {
- cout << "Tie.";
- }
- else if (fullHouseValue_1 > fullHouseValue_2)
- {
- cout << "Black wins.";
- }
- else
- {
- cout << "White wins.";
- }
- }
- else
- {
- cout << "Black wins.";
- }
- }
- else if (isFullHouse(secondHand))
- {
- cout << "White wins.";
- }
- // Flush
- else if (isFlush(firstHand))
- {
- if (isFlush(secondHand))
- {
- int highCardValue_1 = getCardValue(firstHand.at(getHighestCardIndex(firstHand))[0]);
- int highCardValue_2 = getCardValue(secondHand.at(getHighestCardIndex(secondHand))[0]);
- if (highCardValue_1 == highCardValue_2)
- {
- highCardValue_1 = getHighestCardValue(firstHand, 1);
- highCardValue_2 = getHighestCardValue(secondHand, 1);
- if (highCardValue_1 == highCardValue_2)
- {
- highCardValue_1 = getHighestCardValue(firstHand, 2);
- highCardValue_2 = getHighestCardValue(secondHand, 2);
- if (highCardValue_1 == highCardValue_2)
- {
- highCardValue_1 = getHighestCardValue(firstHand, 3);
- highCardValue_2 = getHighestCardValue(secondHand, 3);
- if (highCardValue_1 == highCardValue_2)
- {
- highCardValue_1 = getHighestCardValue(firstHand, 4);
- highCardValue_2 = getHighestCardValue(secondHand, 4);
- if (highCardValue_1 == highCardValue_2)
- {
- cout << "Tie.";
- }
- else if (highCardValue_1 > highCardValue_2)
- {
- cout << "Black wins.";
- }
- else
- {
- cout << "White wins.";
- }
- }
- else if (highCardValue_1 > highCardValue_2)
- {
- cout << "Black wins.";
- }
- else
- {
- cout << "White wins.";
- }
- }
- else if (highCardValue_1 > highCardValue_2)
- {
- cout << "Black wins.";
- }
- else
- {
- cout << "White wins.";
- }
- }
- else if (highCardValue_1 > highCardValue_2)
- {
- cout << "Black wins.";
- }
- else
- {
- cout << "White wins.";
- }
- }
- else if (highCardValue_1 > highCardValue_2)
- {
- cout << "Black wins.";
- }
- else
- {
- cout << "White wins.";
- }
- }
- }
- else if (isFlush(secondHand))
- {
- cout << "White wins.";
- }
- // Straight
- else if (isStraight(firstHand))
- {
- if (isStraight(secondHand))
- {
- int highCardValue_1 = getCardValue(firstHand.at(getHighestCardIndex(firstHand))[0]);
- int highCardValue_2 = getCardValue(secondHand.at(getHighestCardIndex(secondHand))[0]);
- if (highCardValue_1 == highCardValue_2)
- {
- cout << "Tie.";
- }
- else if (highCardValue_1 > highCardValue_2)
- {
- cout << "Black wins.";
- }
- else
- {
- cout << "White wins.";
- }
- }
- }
- else if (isStraight(secondHand))
- {
- cout << "White wins.";
- }
- // Three of a Kind
- else if (isThreeOfAKind(firstHand))
- {
- if (isThreeOfAKind(secondHand))
- {
- int threeOfAKindValue_1 = getThreeOfAKindValue(firstHand);
- int threeOfAKindValue_2 = getThreeOfAKindValue(secondHand);
- if (threeOfAKindValue_1 == threeOfAKindValue_2)
- {
- cout << "Tie.";
- }
- else if (threeOfAKindValue_1 > threeOfAKindValue_2)
- {
- cout << "Black wins.";
- }
- else
- {
- cout << "White wins.";
- }
- }
- else
- {
- cout << "Black wins.";
- }
- }
- else if (isThreeOfAKind(secondHand))
- {
- cout << "White wins.";
- }
- // Two Pairs
- else if (isTwoPairs(firstHand))
- {
- if (isTwoPairs(secondHand))
- {
- int highestPairValue_1 = getHighestPairValue(firstHand);
- int highestPairValue_2 = getHighestPairValue(secondHand);
- if (highestPairValue_1 == highestPairValue_2)
- {
- int lowestPairValue_1 = getLowestPairValue(firstHand);
- int lowestPairValue_2 = getLowestPairValue(secondHand);
- if (lowestPairValue_1 == lowestPairValue_2)
- {
- int nonPairValue_1 = getHighestNonPairValue(firstHand);
- int nonPairValue_2 = getHighestNonPairValue(secondHand);
- if (nonPairValue_1 == nonPairValue_2)
- {
- cout << "Tie.";
- }
- else if (nonPairValue_1 > nonPairValue_2)
- {
- cout << "Black wins.";
- }
- else
- {
- cout << "White wins.";
- }
- }
- else if (lowestPairValue_1 > lowestPairValue_2)
- {
- cout << "Black wins.";
- }
- else
- {
- cout << "White wins.";
- }
- }
- else if (highestPairValue_1 > highestPairValue_2)
- {
- cout << "Black wins.";
- }
- else
- {
- cout << "White wins.";
- }
- }
- else
- {
- cout << "Black wins.";
- }
- }
- else if (isTwoPairs(secondHand))
- {
- cout << "White wins.";
- }
- // Pair
- else if (isPair(firstHand))
- {
- if (isPair(secondHand))
- {
- int highestPairValue_1 = getHighestPairValue(firstHand);
- int highestPairValue_2 = getHighestPairValue(secondHand);
- if (highestPairValue_1 == highestPairValue_2)
- {
- highestPairValue_1 = getHighestNonPairValue(firstHand, 0);
- highestPairValue_2 = getHighestNonPairValue(secondHand, 0);
- if (highestPairValue_1 == highestPairValue_2)
- {
- highestPairValue_1 = getHighestNonPairValue(firstHand, 1);
- highestPairValue_2 = getHighestNonPairValue(secondHand, 1);
- if (highestPairValue_1 == highestPairValue_2)
- {
- highestPairValue_1 = getHighestNonPairValue(firstHand, 2);
- highestPairValue_2 = getHighestNonPairValue(secondHand, 2);
- if (highestPairValue_1 == highestPairValue_2)
- {
- cout << "Tie.";
- }
- else if (highestPairValue_1 > highestPairValue_2)
- {
- cout << "Black wins.";
- }
- else
- {
- cout << "White wins.";
- }
- }
- else if (highestPairValue_1 > highestPairValue_2)
- {
- cout << "Black wins.";
- }
- else
- {
- cout << "White wins.";
- }
- }
- else if (highestPairValue_1 > highestPairValue_2)
- {
- cout << "Black wins.";
- }
- else
- {
- cout << "White wins.";
- }
- }
- else if (highestPairValue_1 > highestPairValue_2)
- {
- cout << "Black wins.";
- }
- else
- {
- cout << "White wins.";
- }
- }
- else
- {
- cout << "Black wins.";
- }
- }
- else if (isPair(secondHand))
- {
- cout << "White wins.";
- }
- // High Card
- else
- {
- int highestCard_1 = getCardValue(firstHand.at(getHighestCardIndex(firstHand))[0]);
- int highestCard_2 = getCardValue(secondHand.at(getHighestCardIndex(secondHand))[0]);
- if (highestCard_1 == highestCard_2)
- {
- highestCard_1 = getHighestCardValue(firstHand, 1);
- highestCard_2 = getHighestCardValue(secondHand, 1);
- if (highestCard_1 == highestCard_2)
- {
- highestCard_1 = getHighestCardValue(firstHand, 2);
- highestCard_2 = getHighestCardValue(secondHand, 2);
- if (highestCard_1 == highestCard_2)
- {
- highestCard_1 = getHighestCardValue(firstHand, 3);
- highestCard_2 = getHighestCardValue(secondHand, 3);
- if (highestCard_1 == highestCard_2)
- {
- highestCard_1 = getHighestCardValue(firstHand, 4);
- highestCard_2 = getHighestCardValue(secondHand, 4);
- if (highestCard_1 == highestCard_2)
- {
- cout << "Tie.";
- }
- else if (highestCard_1 > highestCard_2)
- {
- cout << "Black wins.";
- }
- else
- {
- cout << "White wins.";
- }
- }
- else if (highestCard_1 > highestCard_2)
- {
- cout << "Black wins.";
- }
- else
- {
- cout << "White wins.";
- }
- }
- else if (highestCard_1 > highestCard_2)
- {
- cout << "Black wins.";
- }
- else
- {
- cout << "White wins.";
- }
- }
- else if (highestCard_1 > highestCard_2)
- {
- cout << "Black wins.";
- }
- else
- {
- cout << "White wins.";
- }
- }
- else if (highestCard_1 > highestCard_2)
- {
- cout << "Black wins.";
- }
- else
- {
- cout << "White wins.";
- }
- }
- ///////////////////////////////////////////////////
- cout << endl;
- ///////////////////////////////////////////////////
- }
- system("pause");
- }
- ///////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement