diff options
-rw-r--r-- | card.cpp | 5 | ||||
-rw-r--r-- | card.h | 2 | ||||
-rw-r--r-- | game.cpp | 49 | ||||
-rw-r--r-- | game.h | 1 | ||||
-rw-r--r-- | main.cpp | 4 |
5 files changed, 57 insertions, 4 deletions
@@ -73,6 +73,11 @@ void Card::rankCard(void) } } +int Card::getRank(void) +{ + return rank; +} + enum Suit Card::getSuit(void) { enum Suit whatSuit = Undef; @@ -31,7 +31,7 @@ class Card Card(void); Card(string &); - void getRank(void); + int getRank(void); enum Suit getSuit(void); /* could be overloaded with cout's << operator */ @@ -287,6 +287,36 @@ bool Game::doWeHaveAnAce(Card **cards) return false; } +/* make sure cards are in decreasing rank */ +bool Game::sanityCheck(Card **cards) +{ + if (cards == NULL) + { + PRINT_COLOR(ANSI_COLOR_RED, "\nsanityCheck(): 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) /* this could mean that cards are not sorted or + * there a pair, which would be really bad since + * opponentAI should catch that */ + return false; + back1 = back2; + } + +#ifdef DEBUG + PRINT_COLOR(ANSI_COLOR_CYAN, "\ndebug: sanity check passed, must be a High Hand"); +#endif + + return true; +} + void Game::discardAndDraw(Card **cards, int amount, int offset) { if (offset + amount > 5) @@ -326,7 +356,9 @@ void Game::opponentAI(Card **cards) { if (cards == NULL) { +#ifdef DEBUG PRINT_COLOR(ANSI_COLOR_RED, "\ngetopponentAI(): cards parameter is NULL!\n"); +#endif return; } @@ -334,21 +366,27 @@ void Game::opponentAI(Card **cards) if (pairOrBetter(cards)) { +#ifdef DEBUG PRINT_COLOR(ANSI_COLOR_CYAN, "\ndebug: opponentAI, there's a pair or better\n"); +#endif /* discard 4 other cards*/ return; } else if ( (tmp = numOfSameSuit(cards)).count == 5) { +#ifdef DEBUG PRINT_COLOR(ANSI_COLOR_CYAN, "\ndebug: opponentAI, 5 cards are of the same suit!\n"); +#endif /* keep 5 cards of the same suit */ return; } else if (tmp.count == 4) { +#ifdef DEBUG PRINT_COLOR(ANSI_COLOR_CYAN, "\ndebug: opponentAI, 4 cards are of the same suit\n"); +#endif /* keep 4 cards of the same suit and discard 1 of different suit */ /* we need to loop through cards[] and find a card with different suit */ @@ -367,19 +405,26 @@ void Game::opponentAI(Card **cards) else if (doWeHaveAnAce(cards)) { +#ifdef DEBUG PRINT_COLOR(ANSI_COLOR_CYAN, "\ndebug: opponentAI, High Hand with Ace detected\n"); +#endif /* discard 4 cards */ discardAndDraw(cards, 4, 1); return; } - else + else if (sanityCheck(cards)) { +#ifdef DEBUG PRINT_COLOR(ANSI_COLOR_CYAN, "\ndebug: opponentAI, High Hand detected\n"); +#endif /* discard 3 cards */ discardAndDraw(cards, 3, 2); } - PRINT_COLOR(ANSI_COLOR_RED, "\nopponentAI(): cards did not match any of the criterias!\n"); +#ifdef DEBUG + else + PRINT_COLOR(ANSI_COLOR_RED, "\nopponentAI(): FATAL, cards did not match any of the criterias!\n"); +#endif } @@ -42,6 +42,7 @@ class Game : virtual public CardPile, public User, public Opponent struct countAndSuit numOfSameSuit(Card **); bool doWeHaveAnAce(Card **); + bool sanityCheck(Card **); void discardAndDraw(Card **, int, int); void opponentAI(Card **); @@ -63,9 +63,11 @@ int main(int argc, char *argv[]) pokerGame->printOpponentCards(); #ifdef DEBUG - cout << "Computer's AI" << endl; + cout << "\nComputer's AI"; pokerGame->opponentAI(pokerGame->getOpponentsCards(1)); pokerGame->printOpponentCards(); + cout << "Resorted Opponents' cards:" << endl; + pokerGame->printOpponentCards(); #endif #ifdef DEBUG |