From da1a7180edee0f108d15314e493faa2e9e067095 Mon Sep 17 00:00:00 2001 From: Kyle K Date: Sat, 29 Jan 2011 19:46:29 -0600 Subject: do more error checking --- game.cpp | 62 +++++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 53 insertions(+), 9 deletions(-) (limited to 'game.cpp') diff --git a/game.cpp b/game.cpp index 385ff2f..8d91427 100644 --- a/game.cpp +++ b/game.cpp @@ -119,6 +119,12 @@ void Game::repeatGame(bool *quit) int Game::howManyCardsOfSameSuit(Card **cards) { + if (cards == NULL) + { + PRINT_COLOR(ANSI_COLOR_RED, "\nhowManyCardsOfSameSuit(): cards parameter is NULL!\n"); + return -1; + } + char c, l; l = cards[0]->getType()[1]; @@ -135,6 +141,12 @@ int Game::howManyCardsOfSameSuit(Card **cards) bool Game::pairOrBetter(Card **cards) { + if (cards == NULL) + { + PRINT_COLOR(ANSI_COLOR_RED, "\npairOrBetter(): cards parameter is NULL!\n"); + return false; + } + char c1, c2; int ret = 0; @@ -160,6 +172,12 @@ bool Game::pairOrBetter(Card **cards) /* returns max number of same cards, returns 0 when all cards are different */ int Game::numOfSameCards(Card **cards) { + if (cards == NULL) + { + PRINT_COLOR(ANSI_COLOR_RED, "\nnumOfSameCards(): cards parameter is NULL!\n"); + return -1; + } + char c1, c2; int same = 0; int max = 0; @@ -193,17 +211,24 @@ int Game::numOfSameCards(Card **cards) struct countAndSuit Game::numOfSameSuit(Card **cards) { - int max = 0; - enum Suit tmpSuit = Undef; - - struct countAndSuit same[4] = + struct countAndSuit same[5] = { { Clubs, 0}, { Diamond, 0 }, { Hearts, 0 }, { Spades, 0 }, + { Undef, -1 }, }; + if (cards == NULL) + { + PRINT_COLOR(ANSI_COLOR_RED, "\nnumOfSameSuit(): cards parameter is NULL!\n"); + return same[4]; + } + + int max = 0; + enum Suit tmpSuit = Undef; + /* go through all cards and count how many there is of each suit */ int i; for (i = 0; i < 5; i++) @@ -243,6 +268,12 @@ struct countAndSuit Game::numOfSameSuit(Card **cards) bool Game::doWeHaveAnAce(Card **cards) { + if (cards == NULL) + { + PRINT_COLOR(ANSI_COLOR_RED, "\ndoWeHaveAnAce(): cards parameter is NULL!\n"); + return false; + } + char c; int i; @@ -263,14 +294,19 @@ void Game::discardAndDraw(Card **cards, int amount, int offset) PRINT_COLOR(ANSI_COLOR_RED, "amount of cards to be discarded is out of bounds!\n"); return; } + else if (cards == NULL) + { + PRINT_COLOR(ANSI_COLOR_RED, "\ndiscardAndDraw(): cards parameter is NULL!\n"); + return; + } int i; /* discard cards starting from offset */ for (i = 0; i < amount; i++) { #ifdef DEBUG - cout << "debug: discarding card " << i+1 << ": " << cards[offset+i]->getType() - << " at cards[" << offset << "]" << endl; + cout << ANSI_COLOR_CYAN << "debug: discarding card " << i+1 << " \"" << cards[offset+i]->getType() + << "\" at cards[" << offset+i << "]" << endl << ANSI_COLOR_RESET; #endif putCardToDiscardPile(cards[offset+i]); } @@ -280,14 +316,20 @@ void Game::discardAndDraw(Card **cards, int amount, int offset) { cards[offset+i] = PopCardFromDeck(); #ifdef DEBUG - cout << "debug: drawing card " << i+1 << ": " << cards[offset+i]->getType() - << " at cards[" << offset << "]" << endl; + cout << ANSI_COLOR_CYAN << "debug: drawing card " << i+1 << " \"" << cards[offset+i]->getType() + << "\" into cards[" << offset+i << "]" << endl << ANSI_COLOR_RESET; #endif } } void Game::opponentAI(Card **cards) { + if (cards == NULL) + { + PRINT_COLOR(ANSI_COLOR_RED, "\ngetopponentAI(): cards parameter is NULL!\n"); + return; + } + struct countAndSuit tmp = numOfSameSuit(cards); if (pairOrBetter(cards)) @@ -316,8 +358,8 @@ void Game::opponentAI(Card **cards) if (cards[i]->getSuit() != tmp.whatSuit) break; } - /* no i should be the index of card that we want to discard */ + /* no i should be the index of card that we want to discard */ discardAndDraw(cards, 1, i); return; @@ -337,5 +379,7 @@ void Game::opponentAI(Card **cards) /* discard 3 cards */ discardAndDraw(cards, 3, 2); } + + PRINT_COLOR(ANSI_COLOR_RED, "\nopponentAI(): cards did not match any of the criterias!\n"); } -- cgit v1.2.3