From 1523b889daa27904fbc1bf2c8b722b78af7db917 Mon Sep 17 00:00:00 2001 From: Kyle K Date: Mon, 31 Jan 2011 17:33:24 -0600 Subject: wip :) --- card.cpp | 54 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ card.h | 1 + game.cpp | 9 +++++++++ game.h | 3 ++- main.cpp | 41 +++++++++++++++++++++++------------------ sorthand.cpp | 41 +++++++++++++++++++++++++++++++++++++---- sorthand.h | 1 + 7 files changed, 127 insertions(+), 23 deletions(-) diff --git a/card.cpp b/card.cpp index f100e1c..5bb1a56 100644 --- a/card.cpp +++ b/card.cpp @@ -78,6 +78,60 @@ void Card::rankCard(void) } } +/* 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, SortHand will set these attributes when necessary and call sort */ +void Card::specialRank(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 special ranking a card" << endl; + } +} + int Card::getRank(void) { return rank; diff --git a/card.h b/card.h index ad5c238..6a7555f 100644 --- a/card.h +++ b/card.h @@ -38,6 +38,7 @@ class Card /* could be overloaded with cout's << operator */ string getType(void); void rankCard(void); + void specialRank(void); void sortCards(Card **); char giveRank(char); }; diff --git a/game.cpp b/game.cpp index c0a0dd0..c7b78ae 100644 --- a/game.cpp +++ b/game.cpp @@ -641,6 +641,8 @@ void Game::userAI(Card **cards) int howManyUserCanDiscard = 0; int i; + /* XXX: handle straight */ + if (pairOrBetter(cards)) { int *cardsToDiscard = whichCardsToDiscard(cards); @@ -797,8 +799,12 @@ void Game::evaluateHand(Card **cards, int who) /* four of a kind */ else if (tmp2.count == 4) + { whatHand = 2; + sortFourOfAKind(cards, &tmp2); + } + /* XXX: flawed */ /* full house */ else if ((tmp2.count == 3) && (cards[3]->getType()[0] == cards[4]->getType()[0])) whatHand = 3; @@ -813,7 +819,10 @@ void Game::evaluateHand(Card **cards, int who) /* three of a kind */ else if (tmp2.count == 3) + { whatHand = 6; + sortThreeOfAKind(cards, &tmp2); + } /* two pair */ else if (hasTwoPairs(cards)) diff --git a/game.h b/game.h index 10e401f..298a1a1 100644 --- a/game.h +++ b/game.h @@ -11,12 +11,13 @@ #include "cardpile.h" #include "user.h" #include "opponent.h" +#include "sorthand.h" using std::cout; using std::endl; using std::cin; -class Game : virtual public CardPile, public User, public Opponent +class Game : virtual public CardPile, public User, public Opponent, public SortHand { private: diff --git a/main.cpp b/main.cpp index ef40406..049a69d 100644 --- a/main.cpp +++ b/main.cpp @@ -31,28 +31,20 @@ int main(int argc, char *argv[]) #ifdef DEBUG PRINT_COLOR(ANSI_COLOR_CYAN, "\ndebug: deck after shuffle\n"); pokerGame->printPile(); + cout << endl; #endif /* all methods assume that cards are sorted, it is our responsibility to * to meet that, this results in simpler code */ - cout << "\n> Dealing cards to the user" << endl; - pokerGame->dealUserCards(); - pokerGame->sortUserCards(); - cout << "> Dealing cards to opponent(s)" << endl << endl; pokerGame->dealOpponentCards(); pokerGame->sortOpponentCards(); - cout << "Cards in your hand: "; - pokerGame->printUserCards(); - - pokerGame->userAI(pokerGame->getUserCards()); #ifdef DEBUG - PRINT_COLOR(ANSI_COLOR_CYAN, "\ndebug: opponents' cards:\n"); + PRINT_COLOR(ANSI_COLOR_CYAN, "debug: opponents' cards before discarding:\n"); pokerGame->printOpponentCards(); PRINT_COLOR(ANSI_COLOR_CYAN, "\ndebug: \"Computer's AI\""); #endif - /* perform AI for each opponent */ int i; for (i = 0; i < pokerGame->numOfOpponents; i++) @@ -60,25 +52,38 @@ int main(int argc, char *argv[]) /* resort the cards after the AI */ pokerGame->sortOpponentCards(); - cout << "\nFinal cards in your hand: "; +#ifdef DEBUG + PRINT_COLOR(ANSI_COLOR_CYAN, "\ndebug: opponents' cards after discarding:\n"); + pokerGame->printOpponentCards(); +#endif + + cout << "\n> Dealing cards to the user" << endl; + pokerGame->dealUserCards(); + pokerGame->sortUserCards(); + + cout << "Cards in your hand: "; + pokerGame->printUserCards(); + pokerGame->userAI(pokerGame->getUserCards()); + + pokerGame->evaluateHand(pokerGame->getUserCards(), 0); /* user hand */ + for (i = 0; i < pokerGame->numOfOpponents; i++) /* opponents hands */ + pokerGame->evaluateHand(pokerGame->getOpponentsCards(i+1), i+1); + pokerGame->showHands(); + + cout << "\nFinal user's cards:" << endl; pokerGame->sortUserCards(); pokerGame->printUserCards(); cout << "Final opponents' cards:" << endl; pokerGame->printOpponentCards(); + pokerGame->determineWinner(); + #ifdef DEBUG PRINT_COLOR(ANSI_COLOR_CYAN, "\ndebug: deck after being delt to opponents and user\n"); pokerGame->printPile(); #endif - pokerGame->evaluateHand(pokerGame->getUserCards(), 0); /* user hand */ - for (i = 0; i < pokerGame->numOfOpponents; i++) /* opponents hands */ - pokerGame->evaluateHand(pokerGame->getOpponentsCards(i+1), i+1); - pokerGame->showHands(); - - pokerGame->determineWinner(); - /* ask to repeat the game and reset the deck of cards */ pokerGame->repeatGame(&quit); pokerGame->resetDeck(); diff --git a/sorthand.cpp b/sorthand.cpp index 8445e7f..0b09b7c 100644 --- a/sorthand.cpp +++ b/sorthand.cpp @@ -11,12 +11,12 @@ SortHand::SortHand() } -void sortFourOfAKind(Card **cards, struct countAndType *maxCount) +void SortHand::sortFourOfAKind(Card **cards, struct countAndType *maxCount) { int i; /* find the card that is different from other four */ for (i = 0; i < 5; i++) - if (cards[i]->getRank() != maxCount->type) + if (cards[i]->getType()[0] != maxCount->type) break; #ifdef DEBUG @@ -25,14 +25,47 @@ void sortFourOfAKind(Card **cards, struct countAndType *maxCount) #endif /* put that card at the end */ - Card *tmp = cards[i]; + Card *tmp = cards[4]; cards[4] = cards[i]; cards[i] = tmp; } -void sortFullHouse(Card **cards, struct countAndType *threeCount) +void SortHand::sortThreeOfAKind(Card **cards, struct countAndType *threeCount) { + int i; + /* find 2 cards that are different from other three */ + for (i = 0; i < 5; i++) + if (cards[i]->getType()[0] != threeCount->type) + break; + int j; + for (j = 0; j < 5; j++) + if ((cards[j]->getType()[0] != threeCount->type) && (i != j)) + break; + +#ifdef DEBUG + cout << ANSI_COLOR_CYAN << "\ndebug: sortThreeOfAKind(): card " << cards[i]->getType() + << " and " << cards[j]->getType() << " are different from other 3 cards" + << endl << ANSI_COLOR_RESET; +#endif + + /* put those cards at the end */ + Card *tmp = cards[4]; + Card *tmp2 = cards[3]; + + if (cards[i]->getRank() < cards[j]->getRank()) + { + cards[4] = cards[i]; + cards[3] = cards[j]; + } + else + { + cards[4] = cards[j]; + cards[3] = cards[i]; + } + + cards[i] = tmp; + cards[j] = tmp2; } diff --git a/sorthand.h b/sorthand.h index 3baece7..6d5c288 100644 --- a/sorthand.h +++ b/sorthand.h @@ -13,6 +13,7 @@ class SortHand : virtual public CardPile SortHand(void); void sortFourOfAKind(Card **, struct countAndType *); + void sortThreeOfAKind(Card **, struct countAndType *); }; #endif -- cgit v1.2.3