From f3ecbd6df972f22566d9055297dfabd8decb9c07 Mon Sep 17 00:00:00 2001 From: Kyle K Date: Sat, 29 Jan 2011 20:15:11 -0600 Subject: implement sanity check for High Hand --- card.cpp | 5 +++++ card.h | 2 +- game.cpp | 49 +++++++++++++++++++++++++++++++++++++++++++++++-- game.h | 1 + main.cpp | 4 +++- 5 files changed, 57 insertions(+), 4 deletions(-) diff --git a/card.cpp b/card.cpp index ffcffbd..0f504cc 100644 --- a/card.cpp +++ b/card.cpp @@ -73,6 +73,11 @@ void Card::rankCard(void) } } +int Card::getRank(void) +{ + return rank; +} + enum Suit Card::getSuit(void) { enum Suit whatSuit = Undef; diff --git a/card.h b/card.h index f282173..77a3804 100644 --- a/card.h +++ b/card.h @@ -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 */ diff --git a/game.cpp b/game.cpp index 8d91427..6325731 100644 --- a/game.cpp +++ b/game.cpp @@ -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 } diff --git a/game.h b/game.h index ec8e8e7..eeb9d23 100644 --- a/game.h +++ b/game.h @@ -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 **); diff --git a/main.cpp b/main.cpp index f7ecf30..01ce7fd 100644 --- a/main.cpp +++ b/main.cpp @@ -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 -- cgit v1.2.3