diff options
-rw-r--r-- | card.cpp | 2 | ||||
-rw-r--r-- | card.h | 12 | ||||
-rw-r--r-- | cardpile.cpp | 3 | ||||
-rw-r--r-- | game.cpp | 38 | ||||
-rw-r--r-- | game.h | 11 | ||||
-rw-r--r-- | main.cpp | 11 |
6 files changed, 62 insertions, 15 deletions
@@ -101,7 +101,7 @@ enum Suit Card::getSuit(void) void Card::sortCards(Card **cards) { - /* bubble sort */ + /* reverse bubble sort */ int i, j; Card *tmp; @@ -1,9 +1,19 @@ #ifndef _CARD_H_ #define _CARD_H_ +#include <cstdio> #include <iostream> #include <string> +#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) @@ -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 */ + } } @@ -2,7 +2,6 @@ #define _GAME_H_ #include <iostream> -#include <cstdio> #include <string> #include <cstdlib> #include <ctime> @@ -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 */ @@ -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 |