// // Deck Class Header File // // 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 3/19/00 // La Salle University // 1) Separated from War assignment // 2) Use vectors instead of arrays // 3) Changed shuffle function to be more understandable # include //# include #include #include "card.h" #include "deck.h" using namespace std; Deck::Deck ( ) // initialize a deck by creating all 52 cards { for (int i = 1; i <= 13; i++) { Card c1(diamond, i), c2(spade, i), c3(heart, i), c4(club, i); cards.push_back(c1); cards.push_back(c2); cards.push_back(c3); cards.push_back(c4); } } Card Deck::draw ( ) // return one card from the end of the deck { if (! isEmpty()) { Card newOne = cards.back(); cards.pop_back(); return newOne; } else { // otherwise return invalid card Card badOne; // default constructor gives invalid card return badOne; } } // strategy here is to swap each position with some random other position // if that isn't shuffled enough we could put this inside a for loop to do // that a given number of times void Deck::shuffle ( ) { int numCards = cards.size(); Card temp; // for swapping // loop through each position in turn for (int cnt = 0; cnt < numCards; cnt++) { // generate a random position to swap with int rval = rand(); int toSwap = rval % numCards; // swap temp = cards[cnt]; cards[cnt] = cards[toSwap]; cards[toSwap] = temp; } return; } void Deck::Show() { int numCards = cards.size(); cout << endl << "Deck: (size " << numCards << "): "; for (int j = 0;j < numCards;j++){ cout << cards[j] << " | "; } cout << endl; return; }