Server IP : 172.16.15.8 / Your IP : 3.16.75.156 Web Server : Apache System : Linux zeus.vwu.edu 4.18.0-553.27.1.el8_10.x86_64 #1 SMP Wed Nov 6 14:29:02 UTC 2024 x86_64 User : apache ( 48) PHP Version : 7.2.24 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON Directory (0705) : /home/wcdavis/../ajwilcox/../tasantos/ |
[ Home ] | [ C0mmand ] | [ Upload File ] |
---|
// cardclass.cpp - cpp file for card and card deck classes #include <iostream> #include <cstdlib> // for random num generator #include "cardclass.h" using namespace std; // ----- First the Card members ----- //-------------------------------------------------------------------------- // The default CardType constructor actually does nothing! //-------------------------------------------------------------------------- // Accepts: nothing // Returns: nothing //-------------------------------------------------------------------------- CardType :: CardType () { } //-------------------------------------------------------------------------- // Assign allows assignment of this card's face and suit //-------------------------------------------------------------------------- // Accepts: the new face and suit values // Returns: nothing //-------------------------------------------------------------------------- void CardType :: Assign (SuitType s, FaceType f) { suit = s; face = f; } //-------------------------------------------------------------------------- // Print displays the face value and suit of this card using words //-------------------------------------------------------------------------- // Accepts: nothing // Returns: nothing //-------------------------------------------------------------------------- void CardType :: Print () { switch (face) // first display face value { case ACE : cout << " ACE"; break; case TWO : cout << " TWO"; break; case THREE : cout << "THREE"; break; case FOUR : cout << " FOUR"; break; case FIVE : cout << " FIVE"; break; case SIX : cout << " SIX"; break; case SEVEN : cout << "SEVEN"; break; case EIGHT : cout << "EIGHT"; break; case NINE : cout << " NINE"; break; case TEN : cout << " TEN"; break; case JACK : cout << " JACK"; break; case QUEEN : cout << "QUEEN"; break; case KING : cout << " KING"; break; } cout << " of "; switch (suit) // then display suit { case CLUB : cout << "CLUBS "; break; case DIAMOND : cout << "DIAMONDS"; break; case HEART : cout << "HEARTS "; break; case SPADE : cout << "SPADES "; break; } } //-------------------------------------------------------------------------- // GreaterFace compares the face value of this card and another card. //-------------------------------------------------------------------------- // Accepts: the card to which this card is compared // Returns: TRUE if this card's face > other card's face, FALSE otherwise //-------------------------------------------------------------------------- bool CardType :: GreaterFace(CardType othercard) { if (face > othercard.face) return true; else return false; } //-------------------------------------------------------------------------- // EqualFace compares the face value of this card and another card. //-------------------------------------------------------------------------- // Accepts: the card to which this card is compared // Returns: TRUE if this card's face == other card's face, FALSE otherwise //-------------------------------------------------------------------------- bool CardType :: EqualFace(CardType othercard) { if (face == othercard.face) return true; else return false; } //-------------------------------------------------------------------------- // GreaterSuit compares the suit of this card and another card. //-------------------------------------------------------------------------- // Accepts: the card to which this card is compared // Returns: TRUE if this card's suit > other card's suit //-------------------------------------------------------------------------- bool CardType :: GreaterSuit(CardType othercard) { if (suit > othercard.suit) return true; else return false; } //-------------------------------------------------------------------------- // EqualSuit compares the suit of this card and another card. //-------------------------------------------------------------------------- // Accepts: the card to which this card is compared // Returns: TRUE if this card's suit == other card's suit //-------------------------------------------------------------------------- bool CardType :: EqualSuit(CardType othercard) { if (suit == othercard.suit) return true; else return false; } // ----- Now the Deck members //-------------------------------------------------------------------------- // The default constructor sets up this deck as a "fresh" deck of cards // by systematically assigning a value to each card in the deck. //-------------------------------------------------------------------------- // Accepts: nothing // Returns: nothing //-------------------------------------------------------------------------- DeckType :: DeckType() { int i = 0; // counter for cards SuitType s; // counter for suit loop FaceType f; // counter for face value loop CardType c; // temporary card for (s = CLUB; s <= SPADE; s = SuitType(int(s) + 1)) // pair each suit... for (f = TWO; f <= ACE; f = FaceType(int(f) + 1)) // ... & each face { c.Assign(s,f); // build a card with this suit and face cards[i] = c; // put card in the deck i++; // increment for next card }; top = 0; // top starts at beginning of deck } //-------------------------------------------------------------------------- // Shuffle uses a random number to "swap" cards around in the deck. //-------------------------------------------------------------------------- // Accepts: nothing // Returns: nothing //-------------------------------------------------------------------------- void DeckType :: Shuffle () { int current, // current card swapposition; // position with which to swap current card CardType tempcard; // used for swapping for (current = DECKSIZE - 1; current > 0; current--) { swapposition = rand() % DECKSIZE; // choose card to swap with tempcard = cards[current]; // perform the swap cards[current] = cards[swapposition]; cards[swapposition] = tempcard; } } //-------------------------------------------------------------------------- // DrawCard returns the next card in the deck. //-------------------------------------------------------------------------- // Pre: Deck is not empty (top <= DECKSIZE - 1) //-------------------------------------------------------------------------- // Accepts: the deck // Returns: the next card in the deck && and the modified deck //-------------------------------------------------------------------------- CardType DeckType :: DrawCard () { CardType tempcard; tempcard = cards[top]; // grab copy of next card top++; // adjust top to "remove" card from deck return tempcard; } //-------------------------------------------------------------------------- // IsEmpty tests to see if all cards have been drawn from the deck //-------------------------------------------------------------------------- // Accepts: nothing // Returns: TRUE if deck is empty, FALSE otherwise //-------------------------------------------------------------------------- bool DeckType :: IsEmpty () { if (top == DECKSIZE) // top will have advanced past last card position return true; else return false; } //-------------------------------------------------------------------------- // Print uses the card Print member to display the entire deck of cards //-------------------------------------------------------------------------- // Accepts: nothing // Returns: nothing //-------------------------------------------------------------------------- void DeckType :: Print () { int i; // counter for cards for (i = 0; i < DECKSIZE; i++) { cards[i].Print(); if (i % 2 == 1) // start a new line after every other card cout << "\n"; else cout << " "; } }