diff options
-rw-r--r-- | card.cpp | 26 | ||||
-rw-r--r-- | card.h | 7 | ||||
-rw-r--r-- | cardpile.cpp | 2 | ||||
-rw-r--r-- | cardpile.h | 3 | ||||
-rw-r--r-- | game.cpp | 88 | ||||
-rw-r--r-- | game.h | 16 | ||||
-rw-r--r-- | main.cpp | 147 | ||||
-rw-r--r-- | opponent.h | 2 |
8 files changed, 211 insertions, 80 deletions
@@ -73,6 +73,32 @@ void Card::rankCard(void) } } +enum Suit Card::getSuit(void) +{ + enum Suit whatSuit = Undef; + char c = type[1]; + + switch (c) + { + case 'C': + whatSuit = Clubs; + break; + case 'D': + whatSuit = Diamond; + break; + case 'H': + whatSuit = Hearts; + break; + case 'S': + whatSuit = Spades; + break; + default: + cout << "could not determine the suit of the card" << endl; + } + + return whatSuit; +} + void Card::sortCards(Card **cards) { /* bubble sort */ @@ -8,6 +8,8 @@ using std::string; using std::cout; using std::endl; +enum Suit { Undef = 0, Clubs, Diamond, Hearts, Spades }; + class Card { private: @@ -19,10 +21,15 @@ class Card Card(void); Card(string &); + void getRank(void); + enum Suit getSuit(void); + /* could be overloaded with cout's << operator */ string getType(void); void rankCard(void); void sortCards(Card **); + + }; #endif diff --git a/cardpile.cpp b/cardpile.cpp index f5bed61..786f3dc 100644 --- a/cardpile.cpp +++ b/cardpile.cpp @@ -9,9 +9,9 @@ #include "cardpile.h" +/* generates and ranks a deck of 52 cards */ CardPile::CardPile(void) : deckCurrIndex(51), discardPileCurrIndex(0) { - /* generate deck of 52 cards */ int i, j; int k = 0; string suit[4] = {"C","D","H","S"}; @@ -7,10 +7,9 @@ using std::cout; using std::endl; - using std::string; -class CardPile : public Card +class CardPile : public Card { private: /* draw pile */ @@ -9,6 +9,7 @@ /* forward declaration of hands array, very interesting, I didn't know C++ needs this */ string *Game::hands[10]; +string *Game::suits[5]; Game::Game(void) { @@ -21,7 +22,13 @@ Game::Game(void) hands[6] = new string("Three of a Kind"); hands[7] = new string("Two Pair"); hands[8] = new string("One Pair"); - hands[9] = new string("High Card"); + hands[9] = new string("High Card"); + + suits[0] = new string("Undefined"); + suits[1] = new string("Clubs"); + suits[2] = new string("Diamonds"); + suits[3] = new string("Hearts"); + suits[4] = new string("Spades"); } void Game::shufflePile(void) @@ -95,7 +102,7 @@ void Game::repeatGame(bool *quit) cout << endl; while (continueAsking) { - cout << "Play again? (y/n) "; + PRINT_COLOR(ANSI_COLOR_GREEN, "Play again? (y/n) "); cin >> play_again; play_again = tolower(play_again); @@ -184,12 +191,89 @@ int Game::numOfSameCards(Card **cards) return max; } +struct countAndSuit Game::numOfSameSuit(Card **cards) +{ + int max = 0; + enum Suit tmpSuit = Undef; + + struct countAndSuit same[4] = + { + { Clubs, 0}, + { Diamond, 0 }, + { Hearts, 0 }, + { Spades, 0 }, + }; + + /* go through all cards and count how many there is of each suit */ + int i; + for (i = 0; i < 5; i++) + { + tmpSuit = cards[i]->getSuit(); + + switch (tmpSuit) + { + case Clubs: + same[0].count += 1; + break; + case Diamond: + same[1].count += 1; + break; + case Hearts: + same[2].count += 1; + break; + case Spades: + same[3].count += 1; + break; + default: + cout << "could not determine the suit of the card" << endl; + } + } + + int whereMaxWasFound = 0; + /* look for the max number of cards of the same suit */ + for (i = 0; i < 4; i++) + if (same[i].count > max) + { + max = same[i].count; + whereMaxWasFound = i; + } + + return same[whereMaxWasFound]; +} + +bool Game::doWeHaveAnAce(Card **cards) +{ + char c; + + int i; + for (i = 0; i < 5; i++) + { + c = cards[i]->getType()[0]; + if (c == 'A') + return true; + } + + return false; +} + void Game::opponentAI(Card **cards) { + struct countAndSuit tmp; + if (pairOrBetter(cards)) { /* discard 4 other cards*/ return; } + else if ( (tmp = numOfSameSuit(cards)).count == 5) + { + /* keep 5 cards of the same suit */ + return; + } + else if (tmp.count == 4) + { + /* keep 4 cards of the same suit and discard 1 of different suit */ + return; + } } @@ -26,12 +26,17 @@ using std::cout; using std::endl; using std::cin; +struct countAndSuit +{ + enum Suit whatSuit; + int count; +}; + class Game : virtual public CardPile, public User, public Opponent { private: protected: - static string *hands[10]; public: Game(void); @@ -42,15 +47,20 @@ class Game : virtual public CardPile, public User, public Opponent /* asks user for a number, param1 = msg, 2 = rangeA, 3 = rangeB */ int parseInt(string *, int, int); int howManyCardsOfSameSuit(Card **); - bool pairOrBetter(Card **cards); + bool pairOrBetter(Card **); int numOfSameCards(Card **); - void opponentAI(Card **cards); + struct countAndSuit numOfSameSuit(Card **); + bool doWeHaveAnAce(Card **); + void opponentAI(Card **); /* variables */ int numOfOpponents; int handsPlayed; int handsWon; + + static string *hands[10]; + static string *suits[5]; }; #endif @@ -1,71 +1,76 @@ -/* Kamil Kaminski
- * NetID: kkamin8
- *
- * CS340
- * Project 1, Poker Game
- *
- *
- */
-
-#include "game.h"
-
-int main(int argc, char *argv[])
-{
- /* seed rand() */
- srand((unsigned int ) time(NULL));
-
- /* pokerGame instance */
- Game *pokerGame = new Game();
- PRINT_COLOR(ANSI_COLOR_GREEN, "\tWelcome to Poker Game\n\n");
-
- /* number of opponents user input */
- pokerGame->askForNumberOfOpponents();
-
- /* pokerGame loop */
- bool quit = false;
- while (!quit)
- {
- cout << "\nThe deck is being shuffled" << endl;
- pokerGame->shufflePile();
-
-#ifdef DEBUG
- cout << "Deck after shuffle" << endl;
- pokerGame->printPile();
-#endif
-
- cout << "\nDealing cards to the user" << endl;
- pokerGame->dealUserCards();
- pokerGame->sortUserCards();
- cout << "Cards in your hand: ";
- pokerGame->printUserCards();
-
- cout << "Number of same cards: " <<
- pokerGame->numOfSameCards(pokerGame->getUserCards()) << endl;
-
- if ( pokerGame->pairOrBetter(pokerGame->getUserCards()))
- cout << "User has pair or better" << endl;
-
- cout << "\nDealing cards to opponent(s)" << endl;
- pokerGame->dealOpponentCards(pokerGame->numOfOpponents);
- pokerGame->sortOpponentCards(pokerGame->numOfOpponents);
- cout << "Opponents' cards:" << endl;
- pokerGame->printOpponentCards(pokerGame->numOfOpponents);
-
-#ifdef DEBUG
- cout << "\nDeck after being delt to opponents and user" << endl;
- pokerGame->printPile();
-#endif
-
- /* ask to repeat the game and reset the deck of cards */
- pokerGame->repeatGame(&quit);
- pokerGame->resetDeck();
- }
-
- cout << "\nThank you for playing\n" << pokerGame->handsPlayed << ": Hands played\n"
- << pokerGame->handsWon << ": Hands won" << endl;
-
- delete pokerGame;
-
- return 0;
-}
-
+/* Kamil Kaminski + * NetID: kkamin8 + * + * CS340 + * Project 1, Poker Game + * + * + */ + +#include "game.h" + +int main(int argc, char *argv[]) +{ + /* seed rand() */ + srand((unsigned int ) time(NULL)); + + /* pokerGame instance */ + Game *pokerGame = new Game(); + PRINT_COLOR(ANSI_COLOR_GREEN, "\tWelcome to Poker Game\n\n"); + + /* number of opponents user input */ + pokerGame->askForNumberOfOpponents(); + + /* pokerGame loop */ + bool quit = false; + while (!quit) + { + cout << "\nThe deck is being shuffled" << endl; + pokerGame->shufflePile(); + +#ifdef DEBUG + cout << "Deck after shuffle" << endl; + pokerGame->printPile(); +#endif + + cout << "\nDealing cards to the user" << endl; + pokerGame->dealUserCards(); + pokerGame->sortUserCards(); + cout << "Cards in your hand: "; + pokerGame->printUserCards(); + + cout << "Number of same cards: " << + pokerGame->numOfSameCards(pokerGame->getUserCards()) << endl; + + if ( pokerGame->pairOrBetter(pokerGame->getUserCards())) + cout << "User has pair or better" << endl; + + struct countAndSuit userCountAndSuit = pokerGame->numOfSameSuit(pokerGame->getUserCards()); + cout << "Top suit: " << *pokerGame->suits[userCountAndSuit.whatSuit] << ", count: " + << userCountAndSuit.count << endl; + + cout << "\nDealing cards to opponent(s)" << endl; + pokerGame->dealOpponentCards(pokerGame->numOfOpponents); + pokerGame->sortOpponentCards(pokerGame->numOfOpponents); + cout << "Opponents' cards:" << endl; + pokerGame->printOpponentCards(pokerGame->numOfOpponents); + +#ifdef DEBUG + cout << "\nDeck after being delt to opponents and user" << endl; + pokerGame->printPile(); +#endif + + /* ask to repeat the game and reset the deck of cards */ + pokerGame->repeatGame(&quit); + pokerGame->resetDeck(); + } + + cout << "\nThank you for playing\n" << pokerGame->handsPlayed << ": Hands played\n" + << pokerGame->handsWon << ": Hands won" << endl; + + delete pokerGame; + PRINT_COLOR(ANSI_COLOR_CYAN, "\tBye!\n"); + + return 0; +} + @@ -9,7 +9,7 @@ class Opponent : virtual public CardPile int whatHand; protected: - Card *opponentCards[3][5]; + Card *opponentCards[3][5]; public: Opponent(void); |