From 597f9a05c1cde173c1475ddba4830eb51e96a533 Mon Sep 17 00:00:00 2001 From: Kyle K Date: Sat, 29 Jan 2011 18:38:20 -0600 Subject: implement discardAndDraw function --- card.cpp | 2 +- card.h | 12 ++++++++++-- cardpile.cpp | 3 +++ game.cpp | 38 ++++++++++++++++++++++++++++++++++++++ game.h | 11 +---------- main.cpp | 11 +++++++++-- 6 files changed, 62 insertions(+), 15 deletions(-) diff --git a/card.cpp b/card.cpp index 3c4854d..0f4fc3f 100644 --- a/card.cpp +++ b/card.cpp @@ -101,7 +101,7 @@ enum Suit Card::getSuit(void) void Card::sortCards(Card **cards) { - /* bubble sort */ + /* reverse bubble sort */ int i, j; Card *tmp; diff --git a/card.h b/card.h index 8703d05..f282173 100644 --- a/card.h +++ b/card.h @@ -1,9 +1,19 @@ #ifndef _CARD_H_ #define _CARD_H_ +#include #include #include +#define ANSI_COLOR_RED "\x1b[31m" +#define ANSI_COLOR_GREEN "\x1b[32m" +#define ANSI_COLOR_YELLOW "\x1b[33m" +#define ANSI_COLOR_BLUE "\x1b[34m" +#define ANSI_COLOR_MAGENTA "\x1b[35m" +#define ANSI_COLOR_CYAN "\x1b[36m" +#define ANSI_COLOR_RESET "\x1b[0m" +#define PRINT_COLOR(C, S) printf("%s%s%s", C, S, ANSI_COLOR_RESET) + using std::string; using std::cout; using std::endl; @@ -28,8 +38,6 @@ class Card string getType(void); void rankCard(void); void sortCards(Card **); - - }; #endif diff --git a/cardpile.cpp b/cardpile.cpp index 786f3dc..824e689 100644 --- a/cardpile.cpp +++ b/cardpile.cpp @@ -78,7 +78,10 @@ Card *CardPile::PopCardFromDeck(void) if (deckCurrIndex >= 0) return deck[deckCurrIndex--]; else + { + PRINT_COLOR(ANSI_COLOR_RED, "deck is empty!\n"); return NULL; + } } void CardPile::resetDeck(void) diff --git a/game.cpp b/game.cpp index 2bfedcd..7e6e866 100644 --- a/game.cpp +++ b/game.cpp @@ -256,6 +256,36 @@ bool Game::doWeHaveAnAce(Card **cards) return false; } +void Game::discardAndDraw(Card **cards, int amount, int offset) +{ + if (offset + amount > 5) + { + PRINT_COLOR(ANSI_COLOR_RED, "amount of cards to be discarded is out of bounds!\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; +#endif + putCardToDiscardPile(cards[offset+i]); + } + + /* draw cards from the deck into starting at cards[offset] */ + for (i = 0; i < amount; i++) + { + cards[offset+i] = PopCardFromDeck(); +#ifdef DEBUG + cout << "debug: drawing card " << i+1 << ": " << cards[offset+i]->getType() + << " at cards[" << offset << "]" << endl; +#endif + } +} + void Game::opponentAI(Card **cards) { struct countAndSuit tmp; @@ -275,5 +305,13 @@ void Game::opponentAI(Card **cards) /* keep 4 cards of the same suit and discard 1 of different suit */ return; } + else if (doWeHaveAnAce(cards)) + { + /* discard 4 cards */ + } + else + { + /* discard 3 cards */ + } } diff --git a/game.h b/game.h index ed2807b..cb1c528 100644 --- a/game.h +++ b/game.h @@ -2,7 +2,6 @@ #define _GAME_H_ #include -#include #include #include #include @@ -13,15 +12,6 @@ #include "user.h" #include "opponent.h" -#define ANSI_COLOR_RED "\x1b[31m" -#define ANSI_COLOR_GREEN "\x1b[32m" -#define ANSI_COLOR_YELLOW "\x1b[33m" -#define ANSI_COLOR_BLUE "\x1b[34m" -#define ANSI_COLOR_MAGENTA "\x1b[35m" -#define ANSI_COLOR_CYAN "\x1b[36m" -#define ANSI_COLOR_RESET "\x1b[0m" -#define PRINT_COLOR(C, S) printf("%s%s%s", C, S, ANSI_COLOR_RESET) - using std::cout; using std::endl; using std::cin; @@ -52,6 +42,7 @@ class Game : virtual public CardPile, public User, public Opponent struct countAndSuit numOfSameSuit(Card **); bool doWeHaveAnAce(Card **); + void discardAndDraw(Card **, int, int); void opponentAI(Card **); /* variables */ diff --git a/main.cpp b/main.cpp index 710d7a2..9fa9d51 100644 --- a/main.cpp +++ b/main.cpp @@ -29,7 +29,7 @@ int main(int argc, char *argv[]) pokerGame->shufflePile(); #ifdef DEBUG - cout << "Deck after shuffle" << endl; + PRINT_COLOR(ANSI_COLOR_CYAN, "\ndebug: deck after shuffle\n"); pokerGame->printPile(); #endif @@ -49,6 +49,13 @@ int main(int argc, char *argv[]) cout << "Top suit: " << *pokerGame->suits[userCountAndSuit.whatSuit] << ", count: " << userCountAndSuit.count << endl; + if ( pokerGame->doWeHaveAnAce(pokerGame->getUserCards()) ) + cout << "You have an Ace!" << endl; + + pokerGame->discardAndDraw( pokerGame->getUserCards(), 2, 3 ); + cout << "Cards in your hand after discarding: "; + pokerGame->printUserCards(); + cout << "\nDealing cards to opponent(s)" << endl; pokerGame->dealOpponentCards(pokerGame->numOfOpponents); pokerGame->sortOpponentCards(pokerGame->numOfOpponents); @@ -56,7 +63,7 @@ int main(int argc, char *argv[]) pokerGame->printOpponentCards(pokerGame->numOfOpponents); #ifdef DEBUG - cout << "\nDeck after being delt to opponents and user" << endl; + PRINT_COLOR(ANSI_COLOR_CYAN, "\ndebug: deck after being delt to opponents and user\n"); pokerGame->printPile(); #endif -- cgit v1.2.3