// // Program Deck.cpp // // Purpose: demonstrate use of programmer-defined data types, // including enum, struct, and array of structs // // Date: 4/10/00 // // Programmer: Pamela J. Vermeer // #include #include #include #include const DECKSIZE = 52; // Data types enum Value {TWO = 2, THREE, FOUR, FIVE, SIX, SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING, ACE}; enum Suit {SPADES, CLUBS, DIAMONDS, HEARTS}; struct Card { Value myValue; Suit mySuit; }; // Functions in file int CompareCards(Card card1, Card card2); void PrintValue(Card card); void PrintSuit(Card card); void PrintCard(Card card); void InitializeStandardDeck(Card deck[]); void ShuffleDeck(Card deck[], int size); void PrintDeck(Card deck[], int size); void main() { Card deck[DECKSIZE]; // Initialize the random number generator srand(time(NULL)); InitializeStandardDeck(deck); cout << "Before shuffling: " << endl; PrintDeck(deck, DECKSIZE); cout << endl << endl; ShuffleDeck(deck, DECKSIZE); cout << "After shuffling:" << endl; PrintDeck(deck, DECKSIZE); } // // void InitializeStandardDeck(Card deck[]) // // Purpose: fills in the deck array with standard playing cards in standard order // // Parameters: the deck to fill in // void InitializeStandardDeck(Card deck[]) { for (int i = 0; i < 4; i++) for (int j = 2; j <= 14; j++) { deck[i*13 + (j - 2)].myValue = Value(j); deck[i*13 + (j - 2)].mySuit = Suit(i); } } // // void ShuffleDeck(Card deck[]) // // Purpose: randomly reorders a deck of cards // // Parameters: the deck to shuffle // // Comment: assumes constant DECKSIZE is declared // void ShuffleDeck(Card deck[], int size) { int choice; Card temp; for (int i = 0; i < size; i++){ choice = i + rand()%(size - i); temp = deck[i]; deck[i] = deck[choice]; deck[choice] = temp; } } // // int CompareCards(Card card1, Card card2) // // Purpose: compare two cards and decide whether they are equal (return 0) // or the first before the second (return -1) // or the second before the first (return 1) // // The standard order for the cards is Spades first, then Clubs, // then Diamonds, then Hearts. Within each suit, the order is // Two - Ace. So a Two of Diamonds would be "greater than" a King of Clubs // // Parameters: Card card1, card2 -- the cards to compare int CompareCards(Card card1, Card card2) { if (card1.mySuit == card2.mySuit) if (card1.myValue == card2.myValue) return 0; else if (card1.myValue < card2.myValue) return -1; else return 1; else if (card1.mySuit < card2.mySuit) return -1; else return 1; } // // void PrintValue(Value cardValue) // // Purpose: output variable of type Value to the screen // // Parameter: cardValue -- the variable to output // void PrintValue(Value cardValue) { if ( TWO <= cardValue && cardValue <= TEN) cout << int(cardValue); else switch(cardValue) { case JACK: cout << "Jack" ; break; case QUEEN: cout << "Queen" ; break; case KING: cout << "King" ; break; case ACE: cout << "Ace" ; break; } } // // void PrintSuit(Suit cardSuit) // // Purpose: output variable of type Suit to the screen // // Parameter: cardValue -- the variable to output // void PrintSuit(Suit cardSuit) { switch(cardSuit) { case HEARTS: cout << "Hearts"; break; case DIAMONDS: cout << "Diamonds"; break; case CLUBS: cout << "Clubs"; break; case SPADES: cout << "Spades"; break; } } // // void PrintCard(Card inCard) // // Purpose: output variable of type Card to the screen // // Parameter: inCard -- the variable to output // void PrintCard(Card card) { PrintValue(card.myValue); cout << " of " ; PrintSuit(card.mySuit); } // // void void PrintDeck(Card deck[]) // // Purpose: output variable of type Deck to the screen, // one card per line // // Parameter: deck -- the variable to output // // Comment: Pauses after printing 20 cards and waits for input, // because our output screen does not scroll // void PrintDeck(Card deck[], int size) { char ch; // used for input to print out more output for (int i = 0; i < size; i++) { PrintCard(deck[i]); cout << endl; // Pause after printing 20 cards if ((i+1) % 20 == 0) { cout << "Enter any character to continue "; cin >> ch; } } }