/* * card.cpp * * */ #include "card.h" Card::Card(void) : type(""), rank(0) { } Card::Card(Card *card) { type = card->type; rank = card->rank; } Card::Card(string &str) { type = str; } string Card::getType(void) { return type; } void Card::rankCard(void) { char c; c = getType()[0]; switch (c) { case 'A': rank = 13; break; case 'K': rank = 12; break; case 'Q': rank = 11; break; case 'J': rank = 10; break; case 'T': rank = 9; break; case '9': rank = 8; break; case '8': rank = 7; break; case '7': rank = 6; break; case '6': rank = 5; break; case '5': rank = 4; break; case '4': rank = 3; break; case '3': rank = 2; break; case '2': rank = 1; break; default: std::cerr << "something went wrong while ranking a card" << endl; } } /* rank value that will be used by a sorting algorithm to put cards such as a * pair, two pair, three of a kind, four of a kind, or full house all the way * to the left */ void Card::specialRank(void) { char c; c = getType()[0]; switch (c) { case 'A': rank = 26; break; case 'K': rank = 25; break; case 'Q': rank = 24; break; case 'J': rank = 23; break; case 'T': rank = 22; break; case '9': rank = 21; break; case '8': rank = 20; break; case '7': rank = 19; break; case '6': rank = 18; break; case '5': rank = 17; break; case '4': rank = 16; break; case '3': rank = 15; break; case '2': rank = 14; break; default: std::cerr << "something went wrong while special specialRanking a card" << endl; } } int Card::getRank(void) { return rank; } enum Suit Card::getSuit(void) { enum Suit whatSuit = Undef; char c = type[1]; switch (c) { case 'C': whatSuit = Clubs; break; case 'D': whatSuit = Diamond; break; case 'H': whatSuit = Hearts; break; case 'S': whatSuit = Spades; break; default: std::cerr << "could not determine the suit of the card" << endl; } return whatSuit; } void Card::sortCards(Card **cards) { if (cards == NULL) { PRINT_COLOR(ANSI_COLOR_RED, "\nsortCards(): cards parameter is NULL!\n"); return; } int i, j; int howManyOfSameCards[5] = { 1,1,1,1,1 }; char c1, c2; for (i = 0; i < 5; i++) { c1 = cards[i]->getType()[0]; for (j = 0; j < 5; j++) { /* don't count itself */ if (j == i) continue; c2 = cards[j]->getType()[0]; if (c1 == c2) howManyOfSameCards[i] += 1; } } /* indices that contain 1 are not associated with any other cards, * indices that are not 1 have a pair or greater, so we want to give * them higher rank so they get shown all the way on the left */ for (i = 0; i < 5; i++) { if (howManyOfSameCards[i] != 1) cards[i]->specialRank(); } /* reverse bubble sort */ Card *tmp; for (i = 0; i < 5; i++) for (j = 0; j < 5-i-1; j++) { if (cards[j]->rank < cards[j+1]->rank) { tmp = cards[j]; cards[j] = cards[j+1]; cards[j+1] = tmp; } } /* reset special rank */ for (i = 0; i < 5; i++) cards[i]->rankCard(); }