From 0529bbb2d56942bf3b88469786a4776d5cf49447 Mon Sep 17 00:00:00 2001 From: Kyle K Date: Sat, 29 Jan 2011 23:44:07 -0600 Subject: work on opponentAI more --- cardpile.cpp | 1 + game.cpp | 57 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++- game.h | 1 + 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/cardpile.cpp b/cardpile.cpp index 824e689..1eb27eb 100644 --- a/cardpile.cpp +++ b/cardpile.cpp @@ -87,5 +87,6 @@ Card *CardPile::PopCardFromDeck(void) void CardPile::resetDeck(void) { deckCurrIndex = 51; + discardPileCurrIndex = 0; } diff --git a/game.cpp b/game.cpp index 5f2b3a1..6f02b86 100644 --- a/game.cpp +++ b/game.cpp @@ -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 diff --git a/game.h b/game.h index 5455309..9d79fcc 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 **); + void discardFromPairOrBetter(Card **, int); bool sanityCheck(Card **); void discardAndDraw(Card **, int, int); void opponentAI(int); -- cgit v1.2.3