diff options
-rw-r--r-- | game.cpp | 166 | ||||
-rw-r--r-- | game.h | 6 | ||||
-rw-r--r-- | main.cpp | 31 | ||||
-rw-r--r-- | opponent.cpp | 16 | ||||
-rw-r--r-- | opponent.h | 4 | ||||
-rw-r--r-- | user.cpp | 10 | ||||
-rw-r--r-- | user.h | 2 |
7 files changed, 209 insertions, 26 deletions
@@ -612,3 +612,169 @@ void Game::userAI(Card **cards) } } +bool Game::isInOrderByOne(Card **cards) +{ + if (cards == NULL) + { + PRINT_COLOR(ANSI_COLOR_RED, "\nisInOrderByOne(): cards parameter is NULL!\n"); + return false; + } + + int back1, back2; + back1 = cards[0]->getRank(); + + int i; + for (i = 1; i < 5; i++) + { + back2 = cards[i]->getRank(); + if ((back2 + 1) != back1) + return false; + + back1 = back2; + } + + return true; +} + +bool Game::isInOrder(Card **cards) +{ + if (cards == NULL) + { + PRINT_COLOR(ANSI_COLOR_RED, "\nisInOrder(): cards parameter is NULL!\n"); + return false; + } + + int back1, back2; + back1 = cards[0]->getRank(); + + int i; + for (i = 1; i < 5; i++) + { + back2 = cards[i]->getRank(); + if (!(back2 < back1)) + return false; + + back1 = back2; + } + + return true; +} + +bool Game::hasTwoPairs(Card **cards) +{ + if (cards == NULL) + { + PRINT_COLOR(ANSI_COLOR_RED, "\nhasTwoPairs(): cards parameter is NULL!\n"); + return false; + } + + char c1, c2; + int ret = 0; + int whitelist[4]; + whitelist[0] = 0; + whitelist[1] = 0; + whitelist[2] = 0; + whitelist[3] = 0; + + int i, j; + for (i = 0; i < 5; i++) + { + if (whitelist[i] == 1) + continue; + + c1 = cards[i]->getType()[0]; + + for (j = i+1; j < 5; j++) + { + if (whitelist[j] == 1) + continue; + + c2 = cards[j]->getType()[0]; + if (c1 == c2) + { + ret++; + whitelist[j] = 1; + whitelist[i] = 1; + } + } + } + + return (ret != 0); +} + +/* second param, 0 = user, 1~3 designate opponent */ +void Game::evaluateHand(Card **cards, int who) +{ + int whatHand = 9; + + bool haveAce = doWeHaveAnAce(cards); + bool inOrderByOne = isInOrderByOne(cards); + struct countAndSuit tmp = numOfSameSuit(cards); + + /* royal flush */ + if (haveAce && inOrderByOne && (tmp.count == 5)) + whatHand = 0; + + /* straight flush */ + if (!haveAce && inOrderByOne && (tmp.count == 5)) + whatHand = 1; + + /* four of a kind */ + if (tmp.count == 4) + whatHand = 2; + + /* full house */ + if ((tmp.count == 3) && (cards[3]->getType()[0] == cards[4]->getType()[0])) + whatHand = 3; + + /* flush */ + if (tmp.count == 5) + whatHand = 4; + + /* straight */ + if (inOrderByOne) + whatHand = 5; + + /* three of a kind */ + if (tmp.count == 3) + whatHand = 6; + + /* two pair */ + if (hasTwoPairs(cards)) + whatHand = 7; + + /* one pair */ + if (tmp.count == 2) + whatHand = 8; + + else + whatHand = 9; + + switch (who) + { + case 0: + setUserHand(whatHand); + break; + case 1: + setOpponentHand(whatHand, 1); + break; + case 2: + setOpponentHand(whatHand, 2); + break; + case 3: + setOpponentHand(whatHand, 3); + break; + default: + cout << "something went wrong in evaluation" << endl; + } +} + +void Game::showHands(void) +{ + cout << "User has: " << *hands[getUserHand()] << " " << endl; + + int i; + for (i = 0; i < numOfOpponents; i++) + cout << "Opponent " << i+1 << " has: " << *hands[getOpponentHand(i+1)] << " " << endl; +} + @@ -51,6 +51,12 @@ class Game : virtual public CardPile, public User, public Opponent void userDiscardCards(Card **, int); void userAI(Card **); + bool isInOrderByOne(Card **); + bool isInOrder(Card **); + bool hasTwoPairs(Card **); + void evaluateHand(Card **, int); + void showHands(void); + /* variables */ int handsPlayed; int handsWon; @@ -45,34 +45,12 @@ int main(int argc, char *argv[]) pokerGame->sortUserCards(); pokerGame->printUserCards(); -#if 0 - cout << "Number of same cards: " << - pokerGame->numOfSameCards(pokerGame->getUserCards()) << endl; - - if (pokerGame->pairOrBetter(pokerGame->getUserCards())) - cout << "User has pair or better" << endl; - - - struct countAndSuit userCountAndSuit = pokerGame->numOfSameSuit(pokerGame->getUserCards()); - cout << "Top suit: " << *pokerGame->suits[userCountAndSuit.whatSuit] << ", count: " - << userCountAndSuit.count << endl; - - if (pokerGame->doWeHaveAnAce(pokerGame->getUserCards()) ) - cout << "You have an Ace!" << endl; - - pokerGame->discardAndDraw( pokerGame->getUserCards(), 2, 3 ); - cout << "Cards in your hand after discarding: "; - pokerGame->printUserCards(); -#endif - cout << "> Dealing cards to opponent(s)" << endl; pokerGame->dealOpponentCards(); pokerGame->sortOpponentCards(); - #ifdef DEBUG PRINT_COLOR(ANSI_COLOR_CYAN, "\ndebug: opponents' cards:\n"); pokerGame->printOpponentCards(); - PRINT_COLOR(ANSI_COLOR_CYAN, "\ndebug: \"Computer's AI\""); #endif @@ -87,11 +65,18 @@ int main(int argc, char *argv[]) #ifdef DEBUG PRINT_COLOR(ANSI_COLOR_CYAN, "\ndebug: resorted opponents' cards:\n"); pokerGame->printOpponentCards(); - PRINT_COLOR(ANSI_COLOR_CYAN, "\ndebug: deck after being delt to opponents and user\n"); pokerGame->printPile(); #endif + pokerGame->evaluateHand(pokerGame->getUserCards(), i); + + /* evaluate */ + for (i = 0; i < pokerGame->numOfOpponents; i++) + pokerGame->evaluateHand( pokerGame->getOpponentsCards(i+1), i+1 ); + + pokerGame->showHands(); + /* ask to repeat the game and reset the deck of cards */ pokerGame->repeatGame(&quit); pokerGame->resetDeck(); diff --git a/opponent.cpp b/opponent.cpp index 022e26c..63dc73a 100644 --- a/opponent.cpp +++ b/opponent.cpp @@ -6,9 +6,11 @@ #include "opponent.h" -Opponent::Opponent(void) : whatHand(9) +Opponent::Opponent(void) { - + whatHand[0] = 9; + whatHand[1] = 9; + whatHand[2] = 9; } void Opponent::dealOpponentCards(void) @@ -53,3 +55,13 @@ Card **Opponent::getOpponentsCards(int whichOpponent) return opponentCards[whichOpponent-1]; } +void Opponent::setOpponentHand(int hand, int whichOpponent) +{ + whatHand[whichOpponent-1] = hand; +} + +int Opponent::getOpponentHand(int whichOpponent) +{ + return whatHand[whichOpponent-1]; +} + @@ -6,7 +6,7 @@ class Opponent : virtual public CardPile { private: - int whatHand; + int whatHand[3]; protected: Card *opponentCards[3][5]; @@ -17,6 +17,8 @@ class Opponent : virtual public CardPile void printOpponentCards(); void sortOpponentCards(); Card **getOpponentsCards(int); + void setOpponentHand(int, int); + int getOpponentHand(int); int numOfOpponents; }; @@ -43,3 +43,13 @@ Card **User::getUserCards(void) return userCards; } +void User::setUserHand(int hand) +{ + whatHand = hand; +} + +int User::getUserHand(void) +{ + return whatHand; +} + @@ -18,6 +18,8 @@ class User : virtual public CardPile void sortUserCards(void); void discardCards(void); Card **getUserCards(void); + void setUserHand(int); + int getUserHand(void); }; #endif |