#include #include #include using namespace std; enum Suit { CLUBS, DIAMONDS, HEARTS, SPADES }; enum Rank { ACE=1, TWO, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING }; // declare that Deck is a structure, without defining it struct Deck; struct Card { Rank rank; Suit suit; Card (Suit s, Rank r); Card (); void print () const; bool isGreater (const Card& c2) const; int find (const Deck& deck) const; int findSorted (const Deck& deck) const; int findBetween (const Deck& deck, int low, int high) const; //helper function }; Card::Card (Suit s, Rank r) { suit = s; rank = r; } Card::Card () { suit = CLUBS; rank = ACE; } void Card::print () const { vector suits (4); suits[CLUBS] = "Clubs"; suits[DIAMONDS] = "Diamonds"; suits[HEARTS] = "Hearts"; suits[SPADES] = "Spades"; vector ranks (14); ranks[ACE] = "Ace"; ranks[TWO] = "2"; ranks[THREE] = "3"; ranks[FOUR] = "4"; ranks[FIVE] = "5"; ranks[SIX] = "6"; ranks[SEVEN] = "7"; ranks[EIGHT] = "8"; ranks[NINE] = "9"; ranks[TEN] = "10"; ranks[JACK] = "Jack"; ranks[QUEEN] = "Queen"; ranks[KING] = "King"; cout << ranks[rank] << " of " << suits[suit] << endl; } bool Card::isGreater (const Card& c2) const { // first check the suits if (suit > c2.suit) return true; if (suit < c2.suit) return false; // if the suits are equal, check the ranks if (rank > c2.rank) return true; if (rank < c2.rank) return false; // if the ranks are also equal, return false return false; } bool equals (const Card& c1, const Card& c2) { return (c1.rank == c2.rank && c1.suit == c2.suit); } struct Deck { vector cards; Deck (int n); // create uninitialized deck of size n Deck (); // create a completed, sorted deck of cards void print () const; Deck subdeck (int low, int high) const; void shuffle (); }; Deck::Deck (int size) { vector temp (size); cards = temp; } Deck::Deck () { vector temp (52); cards = temp; int i = 0; // next vector element to initialize for (Suit suit = CLUBS; suit <= SPADES; suit = Suit(suit+1)) { for (Rank rank = ACE; rank <= KING; rank = Rank(rank+1)) { cards[i].suit = suit; cards[i].rank = rank; i++; } } } void Deck::print () const { for (int i = 0; i < cards.size(); i++) { cards[i].print (); } } Deck Deck::subdeck (int low, int high) const { Deck sub (high-low+1); for (int i = 0; i