C++ School Assignment? Project Due Tomorrow? Chat LIVE With A Programming Expert!
Submitted By: gabehabe
Actions:
Rating:
Views: 7,018

Language: C++

Last Modified: June 28, 2008
Instructions: I made the function as it is, because it reduces code to a minimum, and you could also add custom suits... be creative!

Snippet


  1. /********************************/
  2. /** CODE TO CREATE AND SHUFFLE **/
  3. /**      A DECK OF CARDS       **/
  4. /**     BY DANNY BATTISON      **/
  5. /**    gabehabe@hotmail.com    **/
  6. /********************************/
  7.  
  8. #include <iostream> // input/output stream
  9. #include <string> // string
  10. #include <vector> // vector
  11. #include <algorithm> // random_shuffle()
  12. #include <ctime> // time()
  13.  
  14. using namespace std;
  15.  
  16. // pass a pointer to the deck and the name of the suit
  17. void addSuit (vector <string> &deck, string suitName)
  18. {
  19.     for (int i = 1; i <= 13; i++)
  20.     {
  21.         string str; // string to add to the deck
  22.         // put the string together
  23.         if (i  < 10) str = i+48; // ASCII value of the number
  24.         if (i == 10) str = "10";
  25.         if (i == 11) str = "Jack";
  26.         if (i == 12) str = "Queen";
  27.         if (i == 13) str = "King";
  28.         str += " of " + suitName;
  29.         // add the string to the deck
  30.         deck.push_back (str);
  31.     }
  32. }
  33.  
  34. int main ()
  35. {
  36.     // create the deck
  37.     vector <string> deck;
  38.     // pass the deck and the suit name to add
  39.     addSuit (deck, "Hearts");
  40.     addSuit (deck, "Clubs");
  41.     addSuit (deck, "Spades");
  42.     addSuit (deck, "Diamonds");
  43.  
  44.     // shuffle the deck
  45.     srand (time(NULL)); // seed the random number with the system clock
  46.     random_shuffle (deck.begin(), deck.end());
  47.  
  48.     // create an iterator, to use for printing
  49.     vector <string>:: iterator It;
  50.     // loop through the deck
  51.     for (It = deck.begin(); It != deck.end(); ++It)
  52.         cout << *It << endl; // print the card value
  53.  
  54.     cin.get (); // pause for input
  55.     return EXIT_SUCCESS;
  56. }

Copy & Paste


Comments

chuck981996 2008-07-07 04:15:09

This was good.

red_4900 2008-07-10 07:39:40

srand() was seeded,but where did you use the rand() function?

gabehabe 2008-07-13 14:17:57

There's no need to use rand... srand will seed anything random from the source (here using the system clock) So, random_shuffle() is basically a random function. =)

jazzy2011 2009-01-03 09:44:44

i changed this: // put the string together if (i < 10) str = i+48; // ASCII value of the number to this: // put the string together if (i == 1) str = "Ace"; if ((i > 1) && (i < 10)) str = i+48; // ASCII value of the number it now says 'Ace' instead of 1 :)

clarence_cool03 2009-05-13 22:24:00

{-='cOOL prOgram... 'gUy'z can i ask u favOr??... 'Do u knOw hOw to prOgraM a grocery or supermarket prOgram in devC laNguaGe??... 'pLs f u knOw can u seNd 8 2 my email aDD??... pLs guY'z... 'tHAnk's... clarence_cool03@y.c... =-}


Add comment


You must be registered and logged on to </dream.in.code> to leave comments.