Advertisement
juanjo12x

UVA_10315_Poker_Hands

Jul 20th, 2014
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.80 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <sstream>
  5. #include <algorithm>
  6. using namespace std;
  7. typedef pair<short, char> card;
  8. typedef vector<card> hand;
  9. typedef pair<short, short> bet;
  10.  
  11. enum {
  12.     BLACK = 0,
  13.     WHITE = 1,
  14.     TIE = 2
  15. };
  16.  
  17. struct compare {
  18.     bool operator() (card i, card j) { return (i.first > j.first);}
  19. } cmp;
  20.  
  21.  
  22. stringstream oss(stringstream::in | stringstream::out);
  23. string output[] = {"Black wins.","White wins.","Tie."};
  24.  
  25. void read_hand(hand& h) {
  26.     for(short i =0; i < 5; i++) {
  27.         char rank, temp[2];
  28.         oss >> temp[0] >> temp[1];
  29.         rank = temp[0];
  30.         card c;
  31.         if(temp[1] == 'H' || temp[1] == 'D' || temp[1] == 'C' || temp[1] == 'S')
  32.             c.second = temp[1];
  33.         else
  34.             continue;
  35.         if(rank >= '0' && rank <= '9')
  36.             c.first = rank-48;
  37.         else if(rank == 'T')
  38.             c.first = 10;
  39.         else if(rank == 'J')
  40.             c.first = 11;
  41.         else if(rank == 'Q')
  42.             c.first = 12;
  43.         else if(rank == 'K')
  44.             c.first = 13;
  45.         else if(rank == 'A')
  46.             c.first = 14;
  47.         else
  48.             continue;
  49.         h.push_back(c);    
  50.     }
  51. }
  52. void print_hand(const hand& b, const hand& w) {
  53.     hand::const_iterator it = b.begin();
  54.     while(it != b.end()) {
  55.         cout << it->first << it->second << " ";
  56.         it++;
  57.     }
  58.     it = w.begin();
  59.     while(it != w.end()) {
  60.         cout << it->first << it->second << " ";
  61.         it++;
  62.     }
  63.     cout << endl;
  64. }
  65. //1
  66. bool high(hand& h, bet& b) {
  67.     b = bet(1, h.begin()->first);
  68.     return true;
  69. }
  70.  
  71. //2:2, 3:2.2, 4:3, 7:3.2, 8:4
  72. bool same_rank(hand& h, bet& b) {
  73.     pair<short, short> rank1(0,0);
  74.     pair<short, short> rank2(0,0);
  75.     hand::iterator it = h.begin();
  76.     rank1.first = it->first;
  77.     rank1.second++;
  78.     it++;
  79.     while(it != h.end()) {
  80.         if(it->first == rank1.first)
  81.             rank1.second++;
  82.         else {
  83.             if( rank1.second < 2) {
  84.                 rank1.first = it->first;
  85.                 rank2.second = 1;
  86.             } else {
  87.                 if(rank2.first == it->first) {
  88.                     rank2.second++;
  89.                 } else if(rank2.first == 0){
  90.                     rank2.first = it->first;
  91.                     rank2.second = 1;
  92.                 } else if (rank2.second < 2) {
  93.                     rank2.first = it->first;
  94.                     rank2.second = 1;
  95.                 }
  96.             }
  97.         }
  98.         it++;
  99.     }
  100.     if((rank1.second == rank2.second) && (rank1.second == 2)) {
  101.         b.first = 3    ;
  102.         b.second = rank1.first;
  103.         return true;
  104.     } else {
  105.         //swap
  106.         if(rank1.second < rank2.second) {
  107.             pair<short, short> temp(0,0);
  108.             temp = rank1;
  109.             rank1 = rank2;
  110.             rank2 = temp;
  111.         }
  112.         if(rank1.second == 4) {
  113.             b.first = 8;
  114.             b.second = rank1.first;
  115.             return true;
  116.         } else if(rank1.second == 3) {
  117.             if(rank2.second == 2) {
  118.                 b.first = 7;
  119.                 b.second = rank1.first;
  120.             } else {
  121.                 b.first = 4;
  122.                 b.second = rank1.first;
  123.             }    
  124.             return true;
  125.         } else if(rank1.second == 2) {
  126.             b.first = 2;
  127.             b.second = rank1.first;
  128.             return true;
  129.         }
  130.     }
  131.     return false;
  132. }
  133.  
  134. //5, 9
  135. bool straight(hand& h, bet& b, bool fls=false) {
  136.     for(short i =1; i < 5; i++) {
  137.         if(h[0].first-i != h[i].first)
  138.             return false;
  139.     }
  140.     if(fls == true)
  141.         b.first = 9;
  142.     else
  143.         b.first = 5;
  144.     b.second = h[0].first;
  145.     return true;
  146. }
  147.  
  148. //6
  149. bool flush(hand& h, bet& b) {
  150.     hand::iterator it = h.begin();
  151.     char color = it->second;
  152.     it++;
  153.     while(it != h.end()) {
  154.         if(it->second != color)
  155.             return false;
  156.         it++;    
  157.     }
  158.     b.first = 6;
  159.     b.second = h.begin()->first;
  160.     return true;
  161. }
  162.  
  163. bet get_bet(hand& h) {
  164.     bet b;
  165.     sort(h.begin(), h.end(), cmp);
  166.     if(flush(h, b) == true) {
  167.         straight(h, b, true);
  168.         return b;
  169.     } else if(straight(h, b) == true)
  170.         return b;
  171.     else if (same_rank(h, b) == true)
  172.         return b;
  173.     else {
  174.         high(h, b);
  175.         return b;
  176.     }
  177. }
  178.  
  179. short Show(hand& black, hand& white) {
  180.     bet b_bet = get_bet(black);
  181.     bet w_bet = get_bet(white);
  182.     if(b_bet.first == w_bet.first) {
  183.         if(b_bet.second == w_bet.second) {
  184.             while(black.empty() == false) {
  185.                 if(black.front().first == white.front().first) {
  186.                     black.erase(black.begin());        
  187.                     white.erase(white.begin());
  188.                 } else {
  189.                     if(black.front().first > white.front().first)
  190.                         return BLACK;
  191.                     else
  192.                         return WHITE;
  193.                 }
  194.             }
  195.             return TIE;
  196.         } else if(b_bet.second > w_bet.second) {
  197.             return BLACK;
  198.         } else {
  199.             return WHITE;
  200.         }
  201.     } else if(b_bet.first > w_bet.first) {
  202.         return BLACK;
  203.     } else {
  204.         return WHITE;
  205.     }
  206. }
  207.  
  208. int main() {
  209.     hand black;
  210.     hand white;
  211.     while(cin.eof() == false) {
  212.         string line;
  213.         oss.str("");
  214.         cin >> ws;
  215.         getline(cin, line);
  216.         if(line.size() < 29)
  217.             continue;
  218.         oss << line;
  219.         black.clear();
  220.         white.clear();
  221.         read_hand(black);
  222.         read_hand(white);
  223.         if((black.size() == white.size()) && (black.size() == 5)) {
  224.             //print_hand(black, white);
  225.             short result = Show(black, white);
  226.             cout << output[result] << endl;
  227.         }
  228.     }
  229.     return 0;    
  230. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement