// File: ex5-7.cpp #include #include // needed for rand() function #include #include #include using namespace std; const string value_name[13] = {"two","three","four","five","six", "seven","eight","nine","ten","jack","queen","king","ace" }; const string suit_name[4] = {"clubs","diamonds","hearts","spades"}; const int HandSize = 5; const int DeckSize = 52; class Deck; class Hand { friend void threeOrFourOfAKind(const Hand&); public: Hand(const string&, Deck&); void print() const; string getName() const { return name; } const Deck& getDeck() const { return deck; } private: string name; int Card_no[HandSize]; Deck& deck; void dealMe(Deck&); }; Hand::Hand(const string& n, Deck& d) : name(n), deck(d) { dealMe(deck); } class Card { private: int value; int suit; public: Card (int x = 0) : value(x%13), suit(x%4) { } int get_value(void) const { return value; } int get_suit() const { return suit; } void print(void) const; }; void Card::print() const { cout << (value_name[value]) << " of " << (suit_name[suit]) << endl; } class Deck { friend void threeOrFourOfAKind(const Hand&); friend class Hand; public: Deck(); void print(void) const; private: Card d[DeckSize]; int nextCard; void shuffle(void); }; Deck::Deck() : nextCard(0) { for (int i = 0; i < DeckSize; i++) d[i] = Card(i); nextCard = 0; shuffle(); } void Deck::shuffle(void) { int k; Card temp; cout << "I am shuffling the Deck\n"; for (int i = 0; i < DeckSize; i++) { k = rand() % DeckSize; temp = d[i]; d[i] = d[k]; d[k] = temp; } } void Deck::print(void) const { for (int i = 0; i < DeckSize; i++) d[i].print(); } void Hand::dealMe(Deck& deck) { assert(deck.nextCard < DeckSize-4); for (int i = 0; i < HandSize; i++) Card_no[i] = deck.nextCard++; } void Hand::print() const { cout << "Ok " << name << ", here is your hand:" << endl; for (int i = 0; i < HandSize; i++) deck.d[Card_no[i]].print(); threeOrFourOfAKind(*this); cout << endl; } int main (void) { srand(time(0)); Deck poker; Hand curly("Curly",poker); Hand larry("Larry",poker); Hand moe("Moe",poker); curly.print(); larry.print(); moe.print(); } void threeOrFourOfAKind(const Hand& who) { int temp; int Card_count; for (int i = 0; i < 3; i++) { Card_count = 1; temp = (who.getDeck().d[who.Card_no[i]]).get_value(); for (int j = i + 1; j < HandSize; j++) if (temp == who.getDeck().d[who.Card_no[j]].get_value()) Card_count++; if (Card_count > 2) { cout << "Hey, you have " << Card_count << ' ' << value_name[temp] << "s.\n"; } } }