muffinman1015

pokersort hand evaluation_public

Dec 5th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.00 KB | None | 0 0
  1. bool is_four(VVI& hand){
  2.     if (hand[0][0] == hand[1][0] && hand[1][0] == hand[2][0] && hand[2][0] == hand[3][0]){
  3.         return true;
  4.     }
  5.     return hand[1][0] == hand[2][0] && hand[2][0] == hand[3][0] && hand[3][0] == hand[4][0];
  6. }
  7.  
  8. bool is_flush(VVI& hand){
  9.     int suit = hand[0][1];
  10.     for (auto i: hand){
  11.         if(i[1] != suit){
  12.             return false;
  13.         }
  14.     }
  15.     return true;
  16. }
  17.  
  18. bool is_straight(VVI& hand) { //use second to last value later when valuing
  19.     if (hand[0][0] == TWO && hand[1][0] == THREE && hand[2][0] == FOUR && hand[3][0] == FIVE && hand[4][0] == ACE){
  20.         return true;
  21.     }
  22.     for (int i = 1; i < hand.size(); i++){
  23.         /*print_card(hand[i]);
  24.         cout << " ";
  25.         print_card(hand[i-1]);
  26.         cout << endl;
  27.         cout << hand[i][0] << " " << hand[i-1][0] << endl;*/
  28.         if (hand[i][0] - hand[i-1][0] != 1){
  29.             return false;
  30.         }
  31.     }
  32.     return true;
  33. }
  34.  
  35. bool is_straightflush(VVI& hand){
  36.     return is_straight(hand) && is_flush(hand);
  37. }
  38.  
  39. bool is_full_house(VVI& hand){ //use middle element later when valuing
  40.     if(!is_four(hand)) {
  41.         if (hand[0][0] == hand[1][0] && hand[2][0] == hand[3][0] && hand[3][0] == hand[4][0]){
  42.             return true;
  43.         }
  44.         if (hand[0][0] == hand[1][0] && hand[1][0] == hand[2][0] && hand[3][0] == hand[4][0]){
  45.             return true;
  46.         }
  47.     }
  48.     return false;
  49. }
  50.  
  51. bool is_triple(VVI& hand){
  52.     if(!is_full_house(hand) && !is_four(hand)){
  53.         if (hand[0][0] == hand[1][0] && hand[1][0] == hand[2][0]){
  54.             return true;
  55.         }
  56.         if (hand[1][0] == hand[2][0] && hand[2][0] == hand[3][0]){
  57.             return true;
  58.         }
  59.         if (hand[2][0] == hand[3][0] && hand[3][0] == hand[4][0]){
  60.             return true;
  61.         }
  62.     }
  63.     return false;
  64. }
  65.  
  66. bool is_two_pair(VVI& hand){
  67.     if (!is_triple(hand) && !is_four(hand)){
  68.         if (hand[0][0] == hand[1][0] && hand[2][0] == hand[3][0]){
  69.             return true;
  70.         }
  71.         if (hand[0][0] == hand[1][0] && hand[3][0] == hand[4][0]){
  72.             return true;
  73.         }
  74.         if (hand[1][0] == hand[2][0] && hand[3][0] == hand[4][0]){
  75.             return true;
  76.         }
  77.     }
  78.     return false;
  79. }
  80.  
  81. bool is_pair(VVI& hand){
  82.     if (!is_two_pair(hand) && !is_triple(hand) && !is_four(hand) && !is_full_house(hand)) {
  83.         if (hand[0][0] == hand[1][0]) {
  84.             return true;
  85.         }
  86.         if (hand[1][0] == hand[2][0]) {
  87.             return true;
  88.         }
  89.         if (hand[2][0] == hand[3][0]) {
  90.             return true;
  91.         }
  92.         if (hand[3][0] == hand[4][0]) {
  93.             return true;
  94.         }
  95.     }
  96.     return false;
  97. }
  98.  
  99. bool is_high_card(VVI& hand){
  100.     return !is_pair(hand) && !is_two_pair(hand) && !is_triple(hand) && !is_straight(hand) && !is_full_house(hand) &&
  101.            !is_flush(hand) && !is_four(hand);
  102. }
  103.  
  104. vector<bool> repetition_array(VVI& hand){
  105.     vector<bool> repeated(hand.size());
  106.     for (int i = 0; i < hand.size(); i++) {
  107.         //cout << "Currently on position " << i << endl;
  108.         for (int j = 0; j < hand.size(); j++) {
  109.             if (j == i){
  110.                 continue;
  111.             }
  112.             if (hand[j][0] == hand[i][0]) {
  113.                 repeated[i] = true;
  114.             }
  115.         }
  116.     }
  117.     //for (int i = 0; i < repeated.size(); i++) {
  118.        // cout << repeated[i] << " ";
  119.     //}
  120.     //cout << endl;
  121.     return repeated;
  122. }
  123. VVI get_unique_cards(VVI& hand){
  124.     VVI unique_elts;
  125.     vector<bool> repeated = repetition_array(hand);
  126.     for (int i = 0; i < repeated.size(); i++){
  127.         if (!repeated[i]){
  128.             unique_elts.push_back(hand[i]);
  129.         }
  130.     }
  131.     //print_hand_names(unique_elts);
  132.     return unique_elts;
  133. }
  134.  
  135. VVI get_repeated_cards(VVI& hand){
  136.     VVI repeated_elts;
  137.     vector<bool> repeated = repetition_array(hand);
  138.     for (int i = 0; i < repeated.size(); i++){
  139.         if (repeated[i]){
  140.             repeated_elts.push_back(hand[i]);
  141.         }
  142.     }
  143.     //print_hand_names(repeated_elts);
  144.     return repeated_elts;
  145. }
  146. VI get_high_unique_card(VVI& hand){
  147.     bool repeated[hand.size()-1];
  148.     insertion_sort_hand(hand);
  149.     //print_hand_names(hand);
  150.     VI highest_unique = hand[0];
  151.     //cout << "Initial: ";
  152.     //print_card_names(highest_unique);
  153.     //cout << endl;
  154.     for (auto i: get_unique_cards(hand)){
  155.         if (i[0] > highest_unique[0]){
  156.             highest_unique = i;
  157.         }
  158.     }
  159.     //cout << "Results: ";
  160.     //print_card_names(highest_unique);
  161.     //cout << endl;
  162.     return highest_unique;
  163. }
  164.  
  165. VI get_high_card(VVI& hand){
  166.     insertion_sort_hand(hand);
  167.     return hand[hand.size()-1];
  168. }
  169.  
  170. int get_high_unique_rank(VVI& hand){
  171.     return get_high_unique_card(hand)[0];
  172. }
  173.  
  174. int get_high_rank(VVI& hand){
  175.     return get_high_card(hand)[0];
  176. }
  177.  
  178. int classify(VVI& hand){
  179.     insertion_sort_hand(hand);
  180.     if(is_straightflush(hand)){
  181.         cout << "STRAIGHT_FLUSH "; //<< endl;
  182.         return STRAIGHT_FLUSH;
  183.     }
  184.     if(is_four(hand)){
  185.         cout << "FOUR_KIND "; // << endl;
  186.         return FOUR_KIND;
  187.     }
  188.     if(is_full_house(hand)){
  189.         cout << "FULL_HOUSE "; // << endl;
  190.         return FULL_HOUSE;
  191.     }
  192.     if(is_flush(hand)){
  193.         cout << "FLUSH "; // << endl;
  194.         return FLUSH;
  195.     }
  196.     if(is_straight(hand)){
  197.         cout << "STRAIGHT "; // << endl;
  198.         return STRAIGHT;
  199.     }
  200.     if(is_triple(hand)){
  201.         cout << "THREE_KIND "; // << endl;
  202.         return THREE_KIND;
  203.     }
  204.     if(is_two_pair(hand)){
  205.         cout << "TWO_PAIR "; // << endl;
  206.         return TWO_PAIR;
  207.     }
  208.     if(is_pair(hand)){
  209.         cout << "PAIR "; // << endl;
  210.         return PAIR;
  211.     }
  212.     if(is_high_card(hand)){
  213.         cout << "HIGH_CARD "; // << endl;
  214.         return HIGH_CARD;
  215.     }
  216.     cout << "ERROR"; // << endl;
  217.     return -1;
  218. }
  219.  
  220. int eval(VVI& hand1, VVI& hand2){
  221.     int eval = classify(hand1) - classify(hand2);
  222.     //cout << endl;
  223.     if (eval == 0) {
  224.         //cout << "Same rank of hand. Proceeding to secondary classification." << endl;
  225.         if (is_high_card(hand1)){
  226.             //cout << "IS HIGH_CARD!" << endl;
  227.             VVI hand1Copy = hand1;
  228.             VVI hand2Copy = hand2;
  229.             while (get_high_unique_rank(hand1Copy) == get_high_unique_rank(hand2Copy)){
  230.                 hand1Copy.erase(hand1Copy.begin()+hand1Copy.size());
  231.                 hand2Copy.erase(hand2Copy.begin()+hand2Copy.size());
  232.             }
  233.             return get_high_unique_rank(hand1Copy) - get_high_unique_rank(hand2Copy);
  234.         }
  235.         if (is_pair(hand1)){
  236.             //cout << "IS PAIR!" << endl;
  237.             VVI repeated_elts_1 = get_repeated_cards(hand1);
  238.             VVI repeated_elts_2 = get_repeated_cards(hand2);
  239.             VVI unique_elts_1 = get_unique_cards(hand1);
  240.             VVI unique_elts_2 = get_unique_cards(hand2);
  241.             if (get_high_rank(repeated_elts_1) == get_high_rank(repeated_elts_2)){
  242.                 return get_high_unique_rank(unique_elts_1) - get_high_unique_rank(unique_elts_2);
  243.             }
  244.             return get_high_unique_rank(repeated_elts_1) - get_high_unique_rank(repeated_elts_2);
  245.         }
  246.         if (is_two_pair(hand1)){
  247.             //cout << "IS TWO_PAIR!" << endl;
  248.             VVI repeated_elts_1 = get_repeated_cards(hand1);
  249.             VVI repeated_elts_2 = get_repeated_cards(hand2);
  250.             VVI unique_elts_1 = get_unique_cards(hand1);
  251.             VVI unique_elts_2 = get_unique_cards(hand2);
  252.             if (get_high_rank(repeated_elts_1) == get_high_rank(repeated_elts_2)){
  253.                 if (repeated_elts_1[0][0] == repeated_elts_2[0][0]) {
  254.                     return get_high_unique_rank(unique_elts_1) - get_high_unique_rank(unique_elts_2);
  255.                 } else {
  256.                     return repeated_elts_1[0][0] - repeated_elts_2[0][0];
  257.                 }
  258.             }
  259.             return get_high_rank(repeated_elts_1) - get_high_rank(repeated_elts_2);
  260.         }
  261.         if (is_triple(hand1)){
  262.             //cout << "IS THREE_KIND!" << endl;
  263.             VVI repeated_elts_1 = get_repeated_cards(hand1);
  264.             VVI repeated_elts_2 = get_repeated_cards(hand2);
  265.             VVI unique_elts_1 = get_unique_cards(hand1);
  266.             VVI unique_elts_2 = get_unique_cards(hand2);
  267.             if (get_high_rank(repeated_elts_1) == get_high_rank(repeated_elts_2)){
  268.                 return get_high_unique_rank(unique_elts_1) - get_high_unique_rank(unique_elts_2);
  269.             }
  270.             return get_high_rank(repeated_elts_1) - get_high_rank(repeated_elts_2);
  271.         }
  272.         if(is_straight(hand1)){
  273.             //cout << "IS STRAIGHT!" << endl;
  274.             return get_high_unique_rank(hand1) - get_high_unique_rank(hand2);
  275.         }
  276.         if (is_flush(hand1)){
  277.             //cout << "IS FLUSH!" << endl;
  278.             VVI hand1Copy = hand1;
  279.             VVI hand2Copy = hand2;
  280.             while (get_high_unique_rank(hand1Copy) == get_high_unique_rank(hand2Copy)){
  281.                 hand1Copy.erase(hand1Copy.begin()+hand1Copy.size());
  282.                 hand2Copy.erase(hand2Copy.begin()+hand2Copy.size());
  283.             }
  284.             return get_high_unique_rank(hand1Copy) - get_high_unique_rank(hand2Copy);
  285.         }
  286.         if (is_full_house(hand1)){
  287.             //cout << "IS FULL HOUSE!" << endl;
  288.             VVI hand1Copy = hand1;
  289.             VVI hand2Copy = hand2;
  290.             int rank_triple = get_high_rank(hand1Copy);
  291.             if (get_high_rank(hand2Copy) == rank_triple){
  292.                 for (int i = 0; i < 5; i++){
  293.                     if (hand1Copy[i][0] == rank_triple){
  294.                         hand1Copy.erase(hand1Copy.begin()+i);
  295.                     }
  296.                     if(hand2Copy[i][0] == rank_triple){
  297.                         hand2Copy.erase(hand2Copy.begin()+i);
  298.                     }
  299.                 }
  300.                 return hand1Copy[0][0] - hand2Copy[0][0];
  301.             }
  302.             return rank_triple - get_high_rank(hand2Copy);
  303.         }
  304.         if (is_four(hand1)){
  305.             //cout << "IS_FOUR!" << endl;
  306.             VVI repeated_elts_1 = get_repeated_cards(hand1);
  307.             VVI repeated_elts_2 = get_repeated_cards(hand2);
  308.             VVI unique_elts_1 = get_unique_cards(hand1);
  309.             VVI unique_elts_2 = get_unique_cards(hand2);
  310.             if (get_high_rank(repeated_elts_1) == get_high_rank(repeated_elts_2)){
  311.                 //cout << "Rank of FOUR_KINDs are equal." << endl;
  312.                 return get_high_unique_rank(unique_elts_1) - get_high_unique_rank(unique_elts_2);
  313.             }
  314.             //cout << "Rank of FOUR_KINDs are different." << endl;
  315.             return get_high_rank(repeated_elts_1) - get_high_rank(repeated_elts_2);
  316.         }
  317.         if (is_straightflush(hand1)) {
  318.             //cout << "IS STRAIGHT FLUSH!" << endl;
  319.             return get_high_unique_rank(hand1) - get_high_unique_rank(hand2);
  320.         }
  321.     }
  322.     return eval;
  323. }
  324.  
  325. int compareTo(int a, int b){
  326.     VVI handA = convert_to_hand(a);
  327.     VVI handB = convert_to_hand(b);
  328.     return eval(handA, handB);
  329. }
Add Comment
Please, Sign In to add comment