Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- bool is_four(VVI& hand){
- if (hand[0][0] == hand[1][0] && hand[1][0] == hand[2][0] && hand[2][0] == hand[3][0]){
- return true;
- }
- return hand[1][0] == hand[2][0] && hand[2][0] == hand[3][0] && hand[3][0] == hand[4][0];
- }
- bool is_flush(VVI& hand){
- int suit = hand[0][1];
- for (auto i: hand){
- if(i[1] != suit){
- return false;
- }
- }
- return true;
- }
- bool is_straight(VVI& hand) { //use second to last value later when valuing
- if (hand[0][0] == TWO && hand[1][0] == THREE && hand[2][0] == FOUR && hand[3][0] == FIVE && hand[4][0] == ACE){
- return true;
- }
- for (int i = 1; i < hand.size(); i++){
- /*print_card(hand[i]);
- cout << " ";
- print_card(hand[i-1]);
- cout << endl;
- cout << hand[i][0] << " " << hand[i-1][0] << endl;*/
- if (hand[i][0] - hand[i-1][0] != 1){
- return false;
- }
- }
- return true;
- }
- bool is_straightflush(VVI& hand){
- return is_straight(hand) && is_flush(hand);
- }
- bool is_full_house(VVI& hand){ //use middle element later when valuing
- if(!is_four(hand)) {
- if (hand[0][0] == hand[1][0] && hand[2][0] == hand[3][0] && hand[3][0] == hand[4][0]){
- return true;
- }
- if (hand[0][0] == hand[1][0] && hand[1][0] == hand[2][0] && hand[3][0] == hand[4][0]){
- return true;
- }
- }
- return false;
- }
- bool is_triple(VVI& hand){
- if(!is_full_house(hand) && !is_four(hand)){
- if (hand[0][0] == hand[1][0] && hand[1][0] == hand[2][0]){
- return true;
- }
- if (hand[1][0] == hand[2][0] && hand[2][0] == hand[3][0]){
- return true;
- }
- if (hand[2][0] == hand[3][0] && hand[3][0] == hand[4][0]){
- return true;
- }
- }
- return false;
- }
- bool is_two_pair(VVI& hand){
- if (!is_triple(hand) && !is_four(hand)){
- if (hand[0][0] == hand[1][0] && hand[2][0] == hand[3][0]){
- return true;
- }
- if (hand[0][0] == hand[1][0] && hand[3][0] == hand[4][0]){
- return true;
- }
- if (hand[1][0] == hand[2][0] && hand[3][0] == hand[4][0]){
- return true;
- }
- }
- return false;
- }
- bool is_pair(VVI& hand){
- if (!is_two_pair(hand) && !is_triple(hand) && !is_four(hand) && !is_full_house(hand)) {
- if (hand[0][0] == hand[1][0]) {
- return true;
- }
- if (hand[1][0] == hand[2][0]) {
- return true;
- }
- if (hand[2][0] == hand[3][0]) {
- return true;
- }
- if (hand[3][0] == hand[4][0]) {
- return true;
- }
- }
- return false;
- }
- bool is_high_card(VVI& hand){
- return !is_pair(hand) && !is_two_pair(hand) && !is_triple(hand) && !is_straight(hand) && !is_full_house(hand) &&
- !is_flush(hand) && !is_four(hand);
- }
- vector<bool> repetition_array(VVI& hand){
- vector<bool> repeated(hand.size());
- for (int i = 0; i < hand.size(); i++) {
- //cout << "Currently on position " << i << endl;
- for (int j = 0; j < hand.size(); j++) {
- if (j == i){
- continue;
- }
- if (hand[j][0] == hand[i][0]) {
- repeated[i] = true;
- }
- }
- }
- //for (int i = 0; i < repeated.size(); i++) {
- // cout << repeated[i] << " ";
- //}
- //cout << endl;
- return repeated;
- }
- VVI get_unique_cards(VVI& hand){
- VVI unique_elts;
- vector<bool> repeated = repetition_array(hand);
- for (int i = 0; i < repeated.size(); i++){
- if (!repeated[i]){
- unique_elts.push_back(hand[i]);
- }
- }
- //print_hand_names(unique_elts);
- return unique_elts;
- }
- VVI get_repeated_cards(VVI& hand){
- VVI repeated_elts;
- vector<bool> repeated = repetition_array(hand);
- for (int i = 0; i < repeated.size(); i++){
- if (repeated[i]){
- repeated_elts.push_back(hand[i]);
- }
- }
- //print_hand_names(repeated_elts);
- return repeated_elts;
- }
- VI get_high_unique_card(VVI& hand){
- bool repeated[hand.size()-1];
- insertion_sort_hand(hand);
- //print_hand_names(hand);
- VI highest_unique = hand[0];
- //cout << "Initial: ";
- //print_card_names(highest_unique);
- //cout << endl;
- for (auto i: get_unique_cards(hand)){
- if (i[0] > highest_unique[0]){
- highest_unique = i;
- }
- }
- //cout << "Results: ";
- //print_card_names(highest_unique);
- //cout << endl;
- return highest_unique;
- }
- VI get_high_card(VVI& hand){
- insertion_sort_hand(hand);
- return hand[hand.size()-1];
- }
- int get_high_unique_rank(VVI& hand){
- return get_high_unique_card(hand)[0];
- }
- int get_high_rank(VVI& hand){
- return get_high_card(hand)[0];
- }
- int classify(VVI& hand){
- insertion_sort_hand(hand);
- if(is_straightflush(hand)){
- cout << "STRAIGHT_FLUSH "; //<< endl;
- return STRAIGHT_FLUSH;
- }
- if(is_four(hand)){
- cout << "FOUR_KIND "; // << endl;
- return FOUR_KIND;
- }
- if(is_full_house(hand)){
- cout << "FULL_HOUSE "; // << endl;
- return FULL_HOUSE;
- }
- if(is_flush(hand)){
- cout << "FLUSH "; // << endl;
- return FLUSH;
- }
- if(is_straight(hand)){
- cout << "STRAIGHT "; // << endl;
- return STRAIGHT;
- }
- if(is_triple(hand)){
- cout << "THREE_KIND "; // << endl;
- return THREE_KIND;
- }
- if(is_two_pair(hand)){
- cout << "TWO_PAIR "; // << endl;
- return TWO_PAIR;
- }
- if(is_pair(hand)){
- cout << "PAIR "; // << endl;
- return PAIR;
- }
- if(is_high_card(hand)){
- cout << "HIGH_CARD "; // << endl;
- return HIGH_CARD;
- }
- cout << "ERROR"; // << endl;
- return -1;
- }
- int eval(VVI& hand1, VVI& hand2){
- int eval = classify(hand1) - classify(hand2);
- //cout << endl;
- if (eval == 0) {
- //cout << "Same rank of hand. Proceeding to secondary classification." << endl;
- if (is_high_card(hand1)){
- //cout << "IS HIGH_CARD!" << endl;
- VVI hand1Copy = hand1;
- VVI hand2Copy = hand2;
- while (get_high_unique_rank(hand1Copy) == get_high_unique_rank(hand2Copy)){
- hand1Copy.erase(hand1Copy.begin()+hand1Copy.size());
- hand2Copy.erase(hand2Copy.begin()+hand2Copy.size());
- }
- return get_high_unique_rank(hand1Copy) - get_high_unique_rank(hand2Copy);
- }
- if (is_pair(hand1)){
- //cout << "IS PAIR!" << endl;
- VVI repeated_elts_1 = get_repeated_cards(hand1);
- VVI repeated_elts_2 = get_repeated_cards(hand2);
- VVI unique_elts_1 = get_unique_cards(hand1);
- VVI unique_elts_2 = get_unique_cards(hand2);
- if (get_high_rank(repeated_elts_1) == get_high_rank(repeated_elts_2)){
- return get_high_unique_rank(unique_elts_1) - get_high_unique_rank(unique_elts_2);
- }
- return get_high_unique_rank(repeated_elts_1) - get_high_unique_rank(repeated_elts_2);
- }
- if (is_two_pair(hand1)){
- //cout << "IS TWO_PAIR!" << endl;
- VVI repeated_elts_1 = get_repeated_cards(hand1);
- VVI repeated_elts_2 = get_repeated_cards(hand2);
- VVI unique_elts_1 = get_unique_cards(hand1);
- VVI unique_elts_2 = get_unique_cards(hand2);
- if (get_high_rank(repeated_elts_1) == get_high_rank(repeated_elts_2)){
- if (repeated_elts_1[0][0] == repeated_elts_2[0][0]) {
- return get_high_unique_rank(unique_elts_1) - get_high_unique_rank(unique_elts_2);
- } else {
- return repeated_elts_1[0][0] - repeated_elts_2[0][0];
- }
- }
- return get_high_rank(repeated_elts_1) - get_high_rank(repeated_elts_2);
- }
- if (is_triple(hand1)){
- //cout << "IS THREE_KIND!" << endl;
- VVI repeated_elts_1 = get_repeated_cards(hand1);
- VVI repeated_elts_2 = get_repeated_cards(hand2);
- VVI unique_elts_1 = get_unique_cards(hand1);
- VVI unique_elts_2 = get_unique_cards(hand2);
- if (get_high_rank(repeated_elts_1) == get_high_rank(repeated_elts_2)){
- return get_high_unique_rank(unique_elts_1) - get_high_unique_rank(unique_elts_2);
- }
- return get_high_rank(repeated_elts_1) - get_high_rank(repeated_elts_2);
- }
- if(is_straight(hand1)){
- //cout << "IS STRAIGHT!" << endl;
- return get_high_unique_rank(hand1) - get_high_unique_rank(hand2);
- }
- if (is_flush(hand1)){
- //cout << "IS FLUSH!" << endl;
- VVI hand1Copy = hand1;
- VVI hand2Copy = hand2;
- while (get_high_unique_rank(hand1Copy) == get_high_unique_rank(hand2Copy)){
- hand1Copy.erase(hand1Copy.begin()+hand1Copy.size());
- hand2Copy.erase(hand2Copy.begin()+hand2Copy.size());
- }
- return get_high_unique_rank(hand1Copy) - get_high_unique_rank(hand2Copy);
- }
- if (is_full_house(hand1)){
- //cout << "IS FULL HOUSE!" << endl;
- VVI hand1Copy = hand1;
- VVI hand2Copy = hand2;
- int rank_triple = get_high_rank(hand1Copy);
- if (get_high_rank(hand2Copy) == rank_triple){
- for (int i = 0; i < 5; i++){
- if (hand1Copy[i][0] == rank_triple){
- hand1Copy.erase(hand1Copy.begin()+i);
- }
- if(hand2Copy[i][0] == rank_triple){
- hand2Copy.erase(hand2Copy.begin()+i);
- }
- }
- return hand1Copy[0][0] - hand2Copy[0][0];
- }
- return rank_triple - get_high_rank(hand2Copy);
- }
- if (is_four(hand1)){
- //cout << "IS_FOUR!" << endl;
- VVI repeated_elts_1 = get_repeated_cards(hand1);
- VVI repeated_elts_2 = get_repeated_cards(hand2);
- VVI unique_elts_1 = get_unique_cards(hand1);
- VVI unique_elts_2 = get_unique_cards(hand2);
- if (get_high_rank(repeated_elts_1) == get_high_rank(repeated_elts_2)){
- //cout << "Rank of FOUR_KINDs are equal." << endl;
- return get_high_unique_rank(unique_elts_1) - get_high_unique_rank(unique_elts_2);
- }
- //cout << "Rank of FOUR_KINDs are different." << endl;
- return get_high_rank(repeated_elts_1) - get_high_rank(repeated_elts_2);
- }
- if (is_straightflush(hand1)) {
- //cout << "IS STRAIGHT FLUSH!" << endl;
- return get_high_unique_rank(hand1) - get_high_unique_rank(hand2);
- }
- }
- return eval;
- }
- int compareTo(int a, int b){
- VVI handA = convert_to_hand(a);
- VVI handB = convert_to_hand(b);
- return eval(handA, handB);
- }
Add Comment
Please, Sign In to add comment