// // Card Class Implementation // Described in Chapter 2 of // Data Structures in C++ using the STL // Published by Addison-Wesley, 1997 // Written by Tim Budd, budd@cs.orst.edu // Oregon State University // // // Modified by Michael Redmond // La Salle University 2/00 -> 3/19/00 // 1) included operators > < == // 2) made default value an invalid card - negative rank // 3) made a joker exist (where joker is 15 of no suit) // 4) added isJoker function and isInvalid function (negative rank) // 5) added isFace which reports whether a card is a Face card or better (i.e. Ace and Joker count) # include //# include #include #include "card.h" using namespace std; Card::Card ( ) // initialize a new Card // default value is invalid card indicator - -1 of no suit { rank = -1; suit = none; } Card::Card (suits sv, int rv) // initialize a new Card using the argument values { rank = rv; suit = sv; } // inspectors // reports if the card is a joker or not bool Card::isJoker() const{ if ((rank == 15) && (suit == none)) { return true; } // must not be a joker return false; } // reports if the card is invalid or not - happens sometimes such as when draw from an empty deck bool Card::isInvalid() const{ if (rank < 0) { return true; } return false; } // reports if the card is a face card or better bool Card::isFace() const{ if (rank > 10) { return true; } if (rank == 1) { // count ace too return true; } // must not be a face card return false; } bool operator == (const Card & leftCard, const Card & rightCard) { // determine if the first card is equal to the second - uses rank only if (leftCard.rank == rightCard.rank) { if (leftCard.suit == rightCard.suit) { return true; } else { return false; } } else { return false; } return true; // should never be executed } /* bool operator == (const Card & leftCard, const Card & rightCard) { // determine if the first card is equal to the second - uses rank only if (leftCard.rank == rightCard.rank) { return true; } else { return false; } return true; // should never be executed } */ // warning - right now this leaves joker as highest possible card (maybe ok) // and invalid cards as lowest possible bool operator > (const Card & leftCard, const Card & rightCard) { // determine if the first card is greater than the second - uses rank only if (leftCard == rightCard) { return false; } else if (leftCard.rank == rightCard.rank) { // check if have same rank - if so use suit if (leftCard.suit > rightCard.suit) { return true; } else { return false; } } // end same rank - know from here on out that have different rank - so suit doesn't matter else if (leftCard.rank == 1) { // ace (and not both aces) return true; } else if (rightCard.rank == 1) { // ace (and not both aces) return false; } else if (leftCard.rank > rightCard.rank) { // aces out of way - so higher rank means better card return true; } else { // second card is better return false; } return true; // should never be executed } /* // warning - right now this leaves joker as highest possible card (maybe ok) // and invalid cards as lowest possible bool operator > (const Card & leftCard, const Card & rightCard) { // determine if the first card is greater than the second - uses rank only if (leftCard == rightCard) { return false; } else if (leftCard.rank == 1) { // ace (and not both aces) return true; } else if (rightCard.rank == 1) { // ace (and not both aces) return false; } else if (leftCard.rank > rightCard.rank) { // aces out of way - so higher rank means better card return true; } else { // second card is better return false; } return true; // should never be executed } */ bool operator < (const Card & leftCard, const Card & rightCard) { // first is less than if it is not equal to and not greater than if (leftCard == rightCard) { return false; } if (leftCard > rightCard) { return false; } return true; // true if not either of the above } ostream & operator << (ostream & out, const Card & aCard) // output a textual representation of a Card { // first output rank switch (aCard.rank) { case -1: out << "INVALID CARD"; break; case 1: out << "Ace"; break; case 11: out << "Jack"; break; case 12: out << "Queen"; break; case 13: out << "King"; break; case 15: out << "Joker"; break; default: // output number out << aCard.rank; break; } // then output suit switch (aCard.suit) { case diamond: out << " of Diamonds"; break; case spade: out << " of Spades"; break; case heart: out << " of Hearts"; break; case club: out << " of Clubs"; break; case none: break; // output nothing for suit of joker or invalid } return out; }