Advertisement
Guest User

Zaiat

a guest
Jul 22nd, 2019
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 17.07 KB | None | 0 0
  1. ///////////////////////////////////////////////////////////
  2.  
  3. #include <iostream>
  4. #include <string>
  5. #include <vector>
  6. #include <sstream>
  7. #include <set>
  8.  
  9. using namespace std;
  10.  
  11. ///////////////////////////////////////////////////////////
  12.  
  13. int getCardValue(char cardFirstChar)
  14. {
  15. int value;
  16. switch (cardFirstChar)
  17. {
  18. case 'T':
  19. {
  20. value = 10;
  21. break;
  22. }
  23. case 'J':
  24. {
  25. value = 11;
  26. break;
  27. }
  28. case 'Q':
  29. {
  30. value = 12;
  31. break;
  32. }
  33. case 'K':
  34. {
  35. value = 13;
  36. break;
  37. }
  38. case 'A':
  39. {
  40. value = 14;
  41. break;
  42. }
  43. default:
  44. {
  45. value = (int)cardFirstChar - '0';
  46. }
  47. }
  48. return value;
  49. }
  50.  
  51. int getHighestCardIndex(vector<string> hand)
  52. {
  53. int highestCard = 0;
  54. int highestCardIndex = 0;
  55. for (int i = 0; i < hand.size(); i++)
  56. {
  57. int cardValue = getCardValue(hand.at(i)[0]);
  58. if (cardValue > highestCard)
  59. {
  60. highestCard = cardValue;
  61. highestCardIndex = i;
  62. }
  63. }
  64. return highestCardIndex;
  65. }
  66.  
  67. int getHighestCardValue(vector<string> hand, int order)
  68. {
  69. set<int> handValues;
  70. for (int i = 0; i < hand.size(); i++)
  71. {
  72. handValues.insert(getCardValue(hand.at(i)[0]));
  73. }
  74. int index = 0;
  75. set<int>::iterator iter = handValues.end();
  76. while (iter != handValues.begin())
  77. {
  78. if (index == order)
  79. {
  80. iter--;
  81. return *iter;
  82. }
  83. else
  84. {
  85. index++;
  86. iter--;
  87. }
  88. }
  89. return 0;
  90. }
  91.  
  92. int getLowestCardIndex(vector<string> hand)
  93. {
  94. int lowestCard = 99999;
  95. int lowestCardIndex = 0;
  96. for (int i = 0; i < hand.size(); i++)
  97. {
  98. int cardValue = getCardValue(hand.at(i)[0]);
  99. if (cardValue < lowestCard)
  100. {
  101. lowestCard = cardValue;
  102. lowestCardIndex = i;
  103. }
  104. }
  105. return lowestCardIndex;
  106. }
  107.  
  108. bool areNumbersDistinct(vector<string> hand)
  109. {
  110. for (int i = 0; i < hand.size() - 1; i++)
  111. {
  112. for (int j = i + 1; j < hand.size(); j++)
  113. {
  114. if (hand.at(i)[0] == hand.at(j)[0])
  115. {
  116. return false;
  117. }
  118. }
  119. }
  120. return true;
  121. }
  122.  
  123. bool areNumbersConsecutive(vector<string> hand)
  124. {
  125. return getCardValue(hand.at(getHighestCardIndex(hand))[0]) - getCardValue(hand.at(getLowestCardIndex(hand))[0]) + 1 == hand.size();
  126. }
  127.  
  128. ///////////////////////////////////////////////////////////
  129.  
  130. bool isPair(vector<string> hand)
  131. {
  132. for (int i = 0; i < hand.size() - 1; i++)
  133. {
  134. for (int j = i + 1; j < hand.size(); j++)
  135. {
  136. if (hand.at(i)[0] == hand.at(j)[0])
  137. {
  138. return true;
  139. }
  140. }
  141. }
  142. return false;
  143. }
  144.  
  145. bool isPair(vector<string> hand, int excludedCardValue)
  146. {
  147. for (int i = 0; i < hand.size() - 1; i++)
  148. {
  149. if (getCardValue(hand.at(i)[0]) != excludedCardValue)
  150. {
  151. for (int j = i + 1; j < hand.size(); j++)
  152. {
  153. if (hand.at(i)[0] == hand.at(j)[0])
  154. {
  155. return true;
  156. }
  157. }
  158. }
  159. }
  160. return false;
  161. }
  162.  
  163. bool isTwoPairs(vector<string> hand)
  164. {
  165. int pairCount = 0;
  166. for (int i = 0; i < hand.size() - 1; i++)
  167. {
  168. for (int j = i + 1; j < hand.size(); j++)
  169. {
  170. if (hand.at(i)[0] == hand.at(j)[0])
  171. {
  172. if (++pairCount == 2)
  173. {
  174. return true;
  175. }
  176. break;
  177. }
  178. }
  179. }
  180. return false;
  181. }
  182.  
  183. int getHighestPairValue(vector<string> hand)
  184. {
  185. int highestPairValue = 0;
  186. for (int i = 0; i < hand.size() - 1; i++)
  187. {
  188. for (int j = i + 1; j < hand.size(); j++)
  189. {
  190. if (hand.at(i)[0] == hand.at(j)[0])
  191. {
  192. int cardValue = getCardValue(hand.at(i)[0]);
  193. if (cardValue > highestPairValue)
  194. {
  195. highestPairValue = cardValue;
  196. }
  197. break;
  198. }
  199. }
  200. }
  201. return highestPairValue;
  202. }
  203.  
  204. int getLowestPairValue(vector<string> hand)
  205. {
  206. int lowestPairValue = 15;
  207. for (int i = 0; i < hand.size() - 1; i++)
  208. {
  209. for (int j = i + 1; j < hand.size(); j++)
  210. {
  211. if (hand.at(i)[0] == hand.at(j)[0])
  212. {
  213. int cardValue = getCardValue(hand.at(i)[0]);
  214. if (cardValue < lowestPairValue)
  215. {
  216. lowestPairValue = cardValue;
  217. }
  218. break;
  219. }
  220. }
  221. }
  222. return lowestPairValue == 15 ? 0 : lowestPairValue;
  223. }
  224.  
  225. int getHighestNonPairValue(vector<string> hand)
  226. {
  227. vector<char> pairs;
  228. int highestNonPairValue = 0;
  229. for (int i = 0; i < hand.size(); i++)
  230. {
  231. bool pairFound = false;
  232. for (int j = 0; j < pairs.size(); j++)
  233. {
  234. if (pairs.at(j) == hand.at(i)[0])
  235. {
  236. pairFound = true;
  237. break;
  238. }
  239. }
  240. if (!pairFound)
  241. {
  242. for (int j = i + 1; j < hand.size(); j++)
  243. {
  244. if (hand.at(i)[0] == hand.at(j)[0])
  245. {
  246. pairFound = true;
  247. pairs.push_back(hand.at(i)[0]);
  248. break;
  249. }
  250. }
  251. }
  252. if (!pairFound)
  253. {
  254. highestNonPairValue = getCardValue(hand.at(i)[0]);
  255. }
  256. }
  257. return highestNonPairValue;
  258. }
  259.  
  260. int getHighestNonPairValue(vector<string> hand, int order)
  261. {
  262. set<int> handValues;
  263. vector<char> pairs;
  264. for (int i = 0; i < hand.size(); i++)
  265. {
  266. bool pairFound = false;
  267. for (int j = 0; j < pairs.size(); j++)
  268. {
  269. if (pairs.at(j) == hand.at(i)[0])
  270. {
  271. pairFound = true;
  272. break;
  273. }
  274. }
  275. if (!pairFound)
  276. {
  277. for (int j = i + 1; j < hand.size(); j++)
  278. {
  279. if (hand.at(i)[0] == hand.at(j)[0])
  280. {
  281. pairFound = true;
  282. pairs.push_back(hand.at(i)[0]);
  283. break;
  284. }
  285. }
  286. }
  287. if (!pairFound)
  288. {
  289. handValues.insert(getCardValue(hand.at(i)[0]));
  290. }
  291. }
  292. int index = 0;
  293. set<int>::iterator iter = handValues.end();
  294. while (iter != handValues.begin())
  295. {
  296. if (index == order)
  297. {
  298. iter--;
  299. return *iter;
  300. }
  301. else
  302. {
  303. index++;
  304. iter--;
  305. }
  306. }
  307. return 0;
  308. }
  309.  
  310. bool isThreeOfAKind(vector<string> hand)
  311. {
  312. for (int i = 0; i < hand.size() - 1; i++)
  313. {
  314. int cardAmount = 1;
  315. for (int j = i + 1; j < hand.size(); j++)
  316. {
  317. if (hand.at(i)[0] == hand.at(j)[0])
  318. {
  319. if (++cardAmount == 3)
  320. {
  321. return true;
  322. }
  323. }
  324. }
  325. }
  326. return false;
  327. }
  328.  
  329. int getThreeOfAKindValue(vector<string> hand)
  330. {
  331. int cardValue = 0;
  332. for (int i = 0; i < hand.size() - 1; i++)
  333. {
  334. int cardAmount = 1;
  335. for (int j = i + 1; j < hand.size(); j++)
  336. {
  337. if (hand.at(i)[0] == hand.at(j)[0])
  338. {
  339. if (++cardAmount == 3)
  340. {
  341. cardValue = getCardValue(hand.at(i)[0]);
  342. }
  343. }
  344. }
  345. }
  346. return cardValue;
  347. }
  348.  
  349. bool isFullHouse(vector<string> hand)
  350. {
  351. if (isThreeOfAKind(hand))
  352. {
  353. int cardValue = getThreeOfAKindValue(hand);
  354. return isPair(hand, cardValue);
  355. }
  356. return false;
  357. }
  358.  
  359. bool isFourOfAKind(vector<string> hand)
  360. {
  361. for (int i = 0; i < hand.size() - 1; i++)
  362. {
  363. int cardAmount = 1;
  364. for (int j = i + 1; j < hand.size(); j++)
  365. {
  366. if (hand.at(i)[0] == hand.at(j)[0])
  367. {
  368. if (++cardAmount == 4)
  369. {
  370. return true;
  371. }
  372. }
  373. }
  374. }
  375. return false;
  376. }
  377.  
  378. bool isStraight(vector<string> hand)
  379. {
  380. return areNumbersDistinct(hand) && areNumbersConsecutive(hand);
  381. }
  382.  
  383. bool isFlush(vector<string> hand)
  384. {
  385. return hand.at(0)[1] == hand.at(1)[1] &&
  386. hand.at(0)[1] == hand.at(2)[1] &&
  387. hand.at(0)[1] == hand.at(3)[1] &&
  388. hand.at(0)[1] == hand.at(4)[1];
  389. }
  390.  
  391.  
  392. ///////////////////////////////////////////////////////////
  393.  
  394. int main()
  395. {
  396. string input;
  397. while (getline(cin, input))
  398. {
  399. ///////////////////////////////////////////////////
  400.  
  401. vector<string> firstHand;
  402. vector<string> secondHand;
  403.  
  404. int i = 0;
  405. istringstream iss(input);
  406. for (string result; iss >> result; )
  407. {
  408. if (i < 5)
  409. {
  410. firstHand.push_back(result);
  411. }
  412. else
  413. {
  414. secondHand.push_back(result);
  415. }
  416. i++;
  417. }
  418.  
  419. ///////////////////////////////////////////////////
  420.  
  421. // Straight Flush
  422. if (isStraight(firstHand) && isFlush(firstHand))
  423. {
  424. if (isStraight(secondHand) && isFlush(secondHand))
  425. {
  426. if (getCardValue(firstHand.at(getHighestCardIndex(firstHand))[0]) == getCardValue(secondHand.at(getHighestCardIndex(secondHand))[0]))
  427. {
  428. cout << "Tie.";
  429. }
  430. else if (getCardValue(firstHand.at(getHighestCardIndex(firstHand))[0]) > getCardValue(secondHand.at(getHighestCardIndex(secondHand))[0]))
  431. {
  432. cout << "Black wins.";
  433. }
  434. else
  435. {
  436. cout << "White wins.";
  437. }
  438. }
  439. else
  440. {
  441. cout << "Black wins.";
  442. }
  443. }
  444. else if (isStraight(secondHand) && isFlush(secondHand))
  445. {
  446. cout << "White wins.";
  447. }
  448.  
  449. // Four of a Kind
  450. else if (isFourOfAKind(firstHand))
  451. {
  452. if (isFourOfAKind(secondHand))
  453. {
  454. int fourOfAKindValue_1 = getThreeOfAKindValue(firstHand);
  455. int fourOfAKindValue_2 = getThreeOfAKindValue(secondHand);
  456. if (fourOfAKindValue_1 == fourOfAKindValue_2)
  457. {
  458. cout << "Tie.";
  459. }
  460. else if (fourOfAKindValue_1 > fourOfAKindValue_2)
  461. {
  462. cout << "Black wins.";
  463. }
  464. else
  465. {
  466. cout << "White wins.";
  467. }
  468. }
  469. else
  470. {
  471. cout << "Black wins.";
  472. }
  473. }
  474. else if (isFourOfAKind(secondHand))
  475. {
  476. cout << "White wins.";
  477. }
  478.  
  479. // Full House
  480. else if (isFullHouse(firstHand))
  481. {
  482. if (isFullHouse(secondHand))
  483. {
  484. int fullHouseValue_1 = getThreeOfAKindValue(firstHand);
  485. int fullHouseValue_2 = getThreeOfAKindValue(secondHand);
  486. if (fullHouseValue_1 == fullHouseValue_2)
  487. {
  488. cout << "Tie.";
  489. }
  490. else if (fullHouseValue_1 > fullHouseValue_2)
  491. {
  492. cout << "Black wins.";
  493. }
  494. else
  495. {
  496. cout << "White wins.";
  497. }
  498. }
  499. else
  500. {
  501. cout << "Black wins.";
  502. }
  503. }
  504. else if (isFullHouse(secondHand))
  505. {
  506. cout << "White wins.";
  507. }
  508.  
  509. // Flush
  510. else if (isFlush(firstHand))
  511. {
  512. if (isFlush(secondHand))
  513. {
  514. int highCardValue_1 = getCardValue(firstHand.at(getHighestCardIndex(firstHand))[0]);
  515. int highCardValue_2 = getCardValue(secondHand.at(getHighestCardIndex(secondHand))[0]);
  516. if (highCardValue_1 == highCardValue_2)
  517. {
  518. highCardValue_1 = getHighestCardValue(firstHand, 1);
  519. highCardValue_2 = getHighestCardValue(secondHand, 1);
  520. if (highCardValue_1 == highCardValue_2)
  521. {
  522. highCardValue_1 = getHighestCardValue(firstHand, 2);
  523. highCardValue_2 = getHighestCardValue(secondHand, 2);
  524. if (highCardValue_1 == highCardValue_2)
  525. {
  526. highCardValue_1 = getHighestCardValue(firstHand, 3);
  527. highCardValue_2 = getHighestCardValue(secondHand, 3);
  528. if (highCardValue_1 == highCardValue_2)
  529. {
  530. highCardValue_1 = getHighestCardValue(firstHand, 4);
  531. highCardValue_2 = getHighestCardValue(secondHand, 4);
  532. if (highCardValue_1 == highCardValue_2)
  533. {
  534. cout << "Tie.";
  535. }
  536. else if (highCardValue_1 > highCardValue_2)
  537. {
  538. cout << "Black wins.";
  539. }
  540. else
  541. {
  542. cout << "White wins.";
  543. }
  544. }
  545. else if (highCardValue_1 > highCardValue_2)
  546. {
  547. cout << "Black wins.";
  548. }
  549. else
  550. {
  551. cout << "White wins.";
  552. }
  553. }
  554. else if (highCardValue_1 > highCardValue_2)
  555. {
  556. cout << "Black wins.";
  557. }
  558. else
  559. {
  560. cout << "White wins.";
  561. }
  562. }
  563. else if (highCardValue_1 > highCardValue_2)
  564. {
  565. cout << "Black wins.";
  566. }
  567. else
  568. {
  569. cout << "White wins.";
  570. }
  571. }
  572. else if (highCardValue_1 > highCardValue_2)
  573. {
  574. cout << "Black wins.";
  575. }
  576. else
  577. {
  578. cout << "White wins.";
  579. }
  580. }
  581. }
  582. else if (isFlush(secondHand))
  583. {
  584. cout << "White wins.";
  585. }
  586.  
  587. // Straight
  588. else if (isStraight(firstHand))
  589. {
  590. if (isStraight(secondHand))
  591. {
  592. int highCardValue_1 = getCardValue(firstHand.at(getHighestCardIndex(firstHand))[0]);
  593. int highCardValue_2 = getCardValue(secondHand.at(getHighestCardIndex(secondHand))[0]);
  594. if (highCardValue_1 == highCardValue_2)
  595. {
  596. cout << "Tie.";
  597. }
  598. else if (highCardValue_1 > highCardValue_2)
  599. {
  600. cout << "Black wins.";
  601. }
  602. else
  603. {
  604. cout << "White wins.";
  605. }
  606. }
  607. }
  608. else if (isStraight(secondHand))
  609. {
  610. cout << "White wins.";
  611. }
  612.  
  613. // Three of a Kind
  614. else if (isThreeOfAKind(firstHand))
  615. {
  616. if (isThreeOfAKind(secondHand))
  617. {
  618. int threeOfAKindValue_1 = getThreeOfAKindValue(firstHand);
  619. int threeOfAKindValue_2 = getThreeOfAKindValue(secondHand);
  620. if (threeOfAKindValue_1 == threeOfAKindValue_2)
  621. {
  622. cout << "Tie.";
  623. }
  624. else if (threeOfAKindValue_1 > threeOfAKindValue_2)
  625. {
  626. cout << "Black wins.";
  627. }
  628. else
  629. {
  630. cout << "White wins.";
  631. }
  632. }
  633. else
  634. {
  635. cout << "Black wins.";
  636. }
  637. }
  638. else if (isThreeOfAKind(secondHand))
  639. {
  640. cout << "White wins.";
  641. }
  642.  
  643. // Two Pairs
  644. else if (isTwoPairs(firstHand))
  645. {
  646. if (isTwoPairs(secondHand))
  647. {
  648. int highestPairValue_1 = getHighestPairValue(firstHand);
  649. int highestPairValue_2 = getHighestPairValue(secondHand);
  650. if (highestPairValue_1 == highestPairValue_2)
  651. {
  652. int lowestPairValue_1 = getLowestPairValue(firstHand);
  653. int lowestPairValue_2 = getLowestPairValue(secondHand);
  654. if (lowestPairValue_1 == lowestPairValue_2)
  655. {
  656. int nonPairValue_1 = getHighestNonPairValue(firstHand);
  657. int nonPairValue_2 = getHighestNonPairValue(secondHand);
  658. if (nonPairValue_1 == nonPairValue_2)
  659. {
  660. cout << "Tie.";
  661. }
  662. else if (nonPairValue_1 > nonPairValue_2)
  663. {
  664. cout << "Black wins.";
  665. }
  666. else
  667. {
  668. cout << "White wins.";
  669. }
  670. }
  671. else if (lowestPairValue_1 > lowestPairValue_2)
  672. {
  673. cout << "Black wins.";
  674. }
  675. else
  676. {
  677. cout << "White wins.";
  678. }
  679. }
  680. else if (highestPairValue_1 > highestPairValue_2)
  681. {
  682. cout << "Black wins.";
  683. }
  684. else
  685. {
  686. cout << "White wins.";
  687. }
  688. }
  689. else
  690. {
  691. cout << "Black wins.";
  692. }
  693. }
  694. else if (isTwoPairs(secondHand))
  695. {
  696. cout << "White wins.";
  697. }
  698.  
  699. // Pair
  700. else if (isPair(firstHand))
  701. {
  702. if (isPair(secondHand))
  703. {
  704. int highestPairValue_1 = getHighestPairValue(firstHand);
  705. int highestPairValue_2 = getHighestPairValue(secondHand);
  706. if (highestPairValue_1 == highestPairValue_2)
  707. {
  708. highestPairValue_1 = getHighestNonPairValue(firstHand, 0);
  709. highestPairValue_2 = getHighestNonPairValue(secondHand, 0);
  710. if (highestPairValue_1 == highestPairValue_2)
  711. {
  712. highestPairValue_1 = getHighestNonPairValue(firstHand, 1);
  713. highestPairValue_2 = getHighestNonPairValue(secondHand, 1);
  714. if (highestPairValue_1 == highestPairValue_2)
  715. {
  716. highestPairValue_1 = getHighestNonPairValue(firstHand, 2);
  717. highestPairValue_2 = getHighestNonPairValue(secondHand, 2);
  718. if (highestPairValue_1 == highestPairValue_2)
  719. {
  720. cout << "Tie.";
  721. }
  722. else if (highestPairValue_1 > highestPairValue_2)
  723. {
  724. cout << "Black wins.";
  725. }
  726. else
  727. {
  728. cout << "White wins.";
  729. }
  730. }
  731. else if (highestPairValue_1 > highestPairValue_2)
  732. {
  733. cout << "Black wins.";
  734. }
  735. else
  736. {
  737. cout << "White wins.";
  738. }
  739. }
  740. else if (highestPairValue_1 > highestPairValue_2)
  741. {
  742. cout << "Black wins.";
  743. }
  744. else
  745. {
  746. cout << "White wins.";
  747. }
  748. }
  749. else if (highestPairValue_1 > highestPairValue_2)
  750. {
  751. cout << "Black wins.";
  752. }
  753. else
  754. {
  755. cout << "White wins.";
  756. }
  757. }
  758. else
  759. {
  760. cout << "Black wins.";
  761. }
  762. }
  763. else if (isPair(secondHand))
  764. {
  765. cout << "White wins.";
  766. }
  767.  
  768.  
  769. // High Card
  770. else
  771. {
  772. int highestCard_1 = getCardValue(firstHand.at(getHighestCardIndex(firstHand))[0]);
  773. int highestCard_2 = getCardValue(secondHand.at(getHighestCardIndex(secondHand))[0]);
  774. if (highestCard_1 == highestCard_2)
  775. {
  776. highestCard_1 = getHighestCardValue(firstHand, 1);
  777. highestCard_2 = getHighestCardValue(secondHand, 1);
  778. if (highestCard_1 == highestCard_2)
  779. {
  780. highestCard_1 = getHighestCardValue(firstHand, 2);
  781. highestCard_2 = getHighestCardValue(secondHand, 2);
  782. if (highestCard_1 == highestCard_2)
  783. {
  784. highestCard_1 = getHighestCardValue(firstHand, 3);
  785. highestCard_2 = getHighestCardValue(secondHand, 3);
  786. if (highestCard_1 == highestCard_2)
  787. {
  788. highestCard_1 = getHighestCardValue(firstHand, 4);
  789. highestCard_2 = getHighestCardValue(secondHand, 4);
  790. if (highestCard_1 == highestCard_2)
  791. {
  792. cout << "Tie.";
  793. }
  794. else if (highestCard_1 > highestCard_2)
  795. {
  796. cout << "Black wins.";
  797. }
  798. else
  799. {
  800. cout << "White wins.";
  801. }
  802. }
  803. else if (highestCard_1 > highestCard_2)
  804. {
  805. cout << "Black wins.";
  806. }
  807. else
  808. {
  809. cout << "White wins.";
  810. }
  811. }
  812. else if (highestCard_1 > highestCard_2)
  813. {
  814. cout << "Black wins.";
  815. }
  816. else
  817. {
  818. cout << "White wins.";
  819. }
  820. }
  821. else if (highestCard_1 > highestCard_2)
  822. {
  823. cout << "Black wins.";
  824. }
  825. else
  826. {
  827. cout << "White wins.";
  828. }
  829. }
  830. else if (highestCard_1 > highestCard_2)
  831. {
  832. cout << "Black wins.";
  833. }
  834. else
  835. {
  836. cout << "White wins.";
  837. }
  838. }
  839.  
  840. ///////////////////////////////////////////////////
  841.  
  842. cout << endl;
  843.  
  844. ///////////////////////////////////////////////////
  845. }
  846. system("pause");
  847. }
  848.  
  849. ///////////////////////////////////////////////////////////
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement