diff options
-rw-r--r-- | Makefile | 5 | ||||
-rw-r--r-- | card.cpp | 43 | ||||
-rw-r--r-- | card.h | 2 | ||||
-rw-r--r-- | cardpile.h | 12 | ||||
-rw-r--r-- | game.cpp | 23 | ||||
-rw-r--r-- | game.h | 13 | ||||
-rw-r--r-- | sorthand.cpp | 38 | ||||
-rw-r--r-- | sorthand.h | 19 |
8 files changed, 138 insertions, 17 deletions
@@ -7,7 +7,7 @@ # Makefile PROG = poker -OBJS = main.o cardpile.o card.o game.o user.o opponent.o +OBJS = main.o cardpile.o card.o game.o user.o opponent.o sorthand.o CC = g++ DBGFLAGS = -g -O0 ifdef DEBUG @@ -38,6 +38,9 @@ user.o: %.o: %.cpp %.h opponent.o: %.o: %.cpp %.h $(CC) -c $(CFLAGS) $< +sorthand.o: %.o: %.cpp %.h + $(CC) -c $(CFLAGS) $< + .PHONY: clean clean: @@ -11,6 +11,12 @@ Card::Card(void) : type(""), rank(0) } +Card::Card(Card *card) +{ + type = card->type; + rank = card->rank; +} + Card::Card(string &str) { type = str; @@ -69,7 +75,6 @@ void Card::rankCard(void) break; default: std::cerr << "something went wrong while ranking a card" << endl; - break; } } @@ -126,3 +131,39 @@ void Card::sortCards(Card **cards) } } +char Card::giveRank(char type) +{ + switch (type) + { + case 'A': + return 13; + case 'K': + return 12; + case 'Q': + return 11; + case 'J': + return 10; + case 'T': + return 9; + case '9': + return 8; + case '8': + return 7; + case '7': + return 6; + case '6': + return 5; + case '5': + return 4; + case '4': + return 3; + case '3': + return 2; + case '2': + return 1; + default: + std::cerr << "something went wrong while giving a card" << endl; + return 0; + } +} + @@ -30,6 +30,7 @@ class Card public: Card(void); Card(string &); + Card(Card *); int getRank(void); enum Suit getSuit(void); @@ -38,6 +39,7 @@ class Card string getType(void); void rankCard(void); void sortCards(Card **); + char giveRank(char); }; #endif @@ -9,6 +9,18 @@ using std::cout; using std::endl; using std::string; +struct countAndSuit +{ + enum Suit whatSuit; + int count; +}; + +struct countAndType +{ + char type; + int count; +}; + class CardPile : public Card { private: @@ -362,7 +362,7 @@ struct countAndType Game::numOfSameType(Card **cards) #ifdef DEBUG cout << ANSI_COLOR_CYAN << "\ndebug: \"" << same[whereMaxWasFound].type << "\" has highest occurence, occurs " << same[whereMaxWasFound].count - << " times" << ANSI_COLOR_RESET; + << " time(s)" << ANSI_COLOR_RESET; #endif return same[whereMaxWasFound]; @@ -785,6 +785,8 @@ void Game::evaluateHand(Card **cards, int who) struct countAndSuit tmp = numOfSameSuit(cards); struct countAndType tmp2 = numOfSameType(cards); + /* XXX: Some hands need to be sorted */ + /* royal flush */ if (haveAce && inOrderByOne && (tmp.count == 5)) whatHand = 0; @@ -877,12 +879,29 @@ void Game::determineWinner(void) int whatIndex = -1; for (i = 0; i < numOfPlayers; i++) - if (playerHands[i] < min) + if (playerHands[i] <= min) { min = playerHands[i]; whatIndex = i; } + /* deal with ties */ + int numOfTies = 0; + for (i = 0; i < numOfPlayers; i++) + if (playerHands[i] == min) + { + ties[i] = true; + numOfTies++; + } + +#ifdef DEBUG + if (numOfTies > 1) + cout << ANSI_COLOR_CYAN << "\ndebug: there's a tie between " << numOfTies + << " players" << endl << ANSI_COLOR_RESET; +#endif + + /* XXX: UNFINISHED */ + cout << ANSI_COLOR_CYAN << endl << *winmsg[whatIndex] << endl << ANSI_COLOR_RESET; @@ -16,18 +16,6 @@ using std::cout; using std::endl; using std::cin; -struct countAndSuit -{ - enum Suit whatSuit; - int count; -}; - -struct countAndType -{ - char type; - int count; -}; - class Game : virtual public CardPile, public User, public Opponent { private: @@ -40,7 +28,6 @@ class Game : virtual public CardPile, public User, public Opponent void askForNumberOfOpponents(void); void repeatGame(bool *); - /* asks user for a number, param1 = msg, 2 = rangeA, 3 = rangeB */ int parseInt(string *, int, int); int howManyCardsOfSameSuit(Card **); bool pairOrBetter(Card **); diff --git a/sorthand.cpp b/sorthand.cpp new file mode 100644 index 0000000..8445e7f --- /dev/null +++ b/sorthand.cpp @@ -0,0 +1,38 @@ +/* + * sorthand.cpp + * + * + */ + +#include "sorthand.h" + +SortHand::SortHand() +{ + +} + +void sortFourOfAKind(Card **cards, struct countAndType *maxCount) +{ + int i; + /* find the card that is different from other four */ + for (i = 0; i < 5; i++) + if (cards[i]->getRank() != maxCount->type) + break; + +#ifdef DEBUG + cout << ANSI_COLOR_CYAN << "\ndebug: sortFourOfAKind(): " << cards[i]->getType() + << " is different from other 4 cards" << endl << ANSI_COLOR_RESET; +#endif + + /* put that card at the end */ + Card *tmp = cards[i]; + + cards[4] = cards[i]; + cards[i] = tmp; +} + +void sortFullHouse(Card **cards, struct countAndType *threeCount) +{ + +} + diff --git a/sorthand.h b/sorthand.h new file mode 100644 index 0000000..3baece7 --- /dev/null +++ b/sorthand.h @@ -0,0 +1,19 @@ +#ifndef _SORTHAND_H_ +#define _SORTHAND_H_ + +#include "cardpile.h" + +class SortHand : virtual public CardPile +{ + private: + + protected: + + public: + SortHand(void); + + void sortFourOfAKind(Card **, struct countAndType *); +}; + +#endif + |