#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 class, without defining it class Deck; class Card { Suit suit; Rank rank; public: // constructors Card (); Card (Suit s, Rank r); // accessor functions int getRank () const { return rank; } int getSuit () const { return suit; } void setRank (Rank r) { rank = r; } void setSuit (Suit s) { suit = s; } // other functions void print () const; bool isGreater (const Card& c2) const; int find (const Deck&) const; int findSorted (const Deck& deck) const; private: 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.getRank() == c2.getRank() && c1.getSuit() == c2.getSuit() ); } class Deck { vector cards; public: // constructors Deck (int n); // create uninitialized deck of size n Deck (); // create a completed, sorted deck of cards // accessor function // D.getCards() returns a vector containing the Cards in Deck D. vector getCards () const { return cards; }; // other functions void print () const; Deck subdeck (int low, int high) const; void shuffle (); void merge (const Deck& d1, const Deck& d2); void sort(); void mergeSort(); }; Deck::Deck (int size) { vector temp (size); cards = temp; } Deck::Deck () { vector temp (52); cards = temp; int i = 0; for (Suit suit = CLUBS; suit <= SPADES; suit = Suit(suit+1)) { for (Rank rank = ACE; rank <= KING; rank = Rank(rank+1)) { cards[i].setSuit (suit); cards[i].setRank (rank); i++; } } } // print() member function of Deck void Deck::print () const { for (int i = 0; i < cards.size(); i++) { cards[i].print (); } } // stand-alone printDeck(D) to print a deck void printDeck (const Deck& deck) { for (int i = 0; i < deck.getCards().size(); i++) { deck.getCards()[i].print(); } } Deck Deck::subdeck (int low, int high) const { Deck sub (high-low+1); for (int i = 0; i result (d1.getCards().size() + d2.getCards().size()); if (d1.getCards().size() == 0 && d2.getCards().size() == 0) { cards = result; return; } // use the index i to keep track of where we are in // the first deck, and the index j for the second deck int i = 0; int j = 0; // the index k traverses the result deck for (int k = 0; k= d1.getCards().size()) { winner = d2.getCards()[j]; j++; } else if (j >= d2.getCards().size()) { winner = d1.getCards()[i]; i++; // otherwise, compare the two cards } else if (d1.getCards()[i].isGreater(d2.getCards()[j])) { winner = d2.getCards()[j]; j++; } else { winner = d1.getCards()[i]; i++; } // add the winner to the new deck result[k] = winner; } cards = result; } void Deck::mergeSort() { const int size = cards.size(); // if the deck is 0 or 1 cards, we're done if (size <= 1) return; // find the midpoint of the deck const int mid = size / 2; // divide the deck into two subdecks Deck left(0), right(0); left = subdeck(0, mid-1); right = subdeck(mid, size-1); // sort the subdecks using mergeSort left.mergeSort(); right.mergeSort(); // merge the two halves to get the result merge (left, right); } int main () { Card AceOfSpades (SPADES, ACE); Deck deck; deck.shuffle(); cout << "Shuffled deck:\n"; deck.print(); cout << "Ace of Spades in position " << AceOfSpades . find(deck) << endl; cout << "\nSorted deck:\n"; deck.sort(); printDeck(deck); cout << "Ace of Spades in position " << AceOfSpades . findSorted(deck) << endl; return 0; }