diff options
Diffstat (limited to 'game.cpp')
-rw-r--r-- | game.cpp | 57 |
1 files changed, 56 insertions, 1 deletions
@@ -287,6 +287,51 @@ bool Game::doWeHaveAnAce(Card **cards) return false; } +void Game::discardFromPairOrBetter(Card **cards, int whichOpponent) +{ + if (cards == NULL) + { + PRINT_COLOR(ANSI_COLOR_RED, "\ndiscardFromPairOrBetter(): cards parameter is NULL!\n"); + return; + } + + + int howManyOfSameCards[5] = { 1,1,1,1,1 }; + int numOfDiscarded = 0; + char c1, c2; + + /* goal is to look later at indices that contain zero */ + int i, j; + for (i = 0; i < 5; i++) + { + c1 = cards[i]->getType()[0]; + + for (j = 0; j < 5; j++) + { + /* don't count itself */ + if (j == i) + continue; + + c2 = cards[j]->getType()[0]; + if (c1 == c2) + howManyOfSameCards[i] += 1; + } + } + + /* indices that contain a zero should be discarded */ + for (i = 0; i < 5; i++) + { + if (howManyOfSameCards[i] == 1) + { + discardAndDraw(cards, 1, i); + numOfDiscarded++; + } + } + + cout << "Opponent " << whichOpponent << " has discarded " + << numOfDiscarded << " card(s)" << endl; +} + /* make sure cards are in decreasing rank */ bool Game::sanityCheck(Card **cards) { @@ -370,7 +415,9 @@ void Game::opponentAI(int whichOpponent) cout << ANSI_COLOR_CYAN << "\ndebug: opponent " << whichOpponent << ", there's a pair or better" << endl << ANSI_COLOR_RESET; #endif - /* discard 4 other cards*/ + /* discard some cards*/ + discardFromPairOrBetter(cards, whichOpponent); + return; } @@ -381,6 +428,8 @@ void Game::opponentAI(int whichOpponent) << ", 5 cards are of the same suit!" << endl << ANSI_COLOR_RESET; #endif /* keep 5 cards of the same suit */ + cout << "Opponent " << whichOpponent << " kept all 5 of the cards" << endl; + return; } @@ -402,6 +451,7 @@ void Game::opponentAI(int whichOpponent) /* no i should be the index of card that we want to discard */ discardAndDraw(cards, 1, i); + cout << "Opponent " << whichOpponent << " has discarded 1 card" << endl; return; } @@ -415,6 +465,8 @@ void Game::opponentAI(int whichOpponent) /* discard 4 cards */ discardAndDraw(cards, 4, 1); + cout << "Opponent " << whichOpponent << " has discarded 4 cards" << endl; + return; } @@ -427,6 +479,9 @@ void Game::opponentAI(int whichOpponent) /* discard 3 cards */ discardAndDraw(cards, 3, 2); + cout << "Opponent " << whichOpponent << " has discarded 3 cards" << endl; + + return; } #ifdef DEBUG |