Advertisement
Guest User

Untitled

a guest
Nov 28th, 2018
121
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.55 KB | None | 0 0
  1. #include "Pile.h"
  2. #include <iomanip>
  3. using namespace std;
  4. void srand(int time());
  5.  
  6. Pile::Pile()
  7. {
  8.     p = vector<Card>(0);
  9. }
  10.  
  11. //draws card from the top of the pile and removes it from the vector
  12. Card Pile:: dealCard()
  13. {
  14.     if(p.size() > 0)
  15.     {
  16.         Card tempCard = p.front();
  17.         p.erase(p.begin());
  18.    
  19.         return tempCard;
  20.     }
  21.     else
  22.         throw std::length_error("Pile is empty");
  23.    
  24.        
  25. }
  26.  
  27. int Pile:: getCount()
  28. {
  29.     return p.size();
  30. }
  31.  
  32. void Pile:: shuffle()
  33. {
  34.     int swapIndex;
  35.     Card tempCard(1, 's');
  36.    
  37.     for(int i = 0; i >= 50; i++) //loop that "shuffles" the cards by removing and inserting 50 times randomly
  38.     {
  39.         swapIndex = rand() % p.size();
  40.         tempCard = p.pop_back();        //pops the back of the deck removing the card and storing it temporarily
  41.         p.insert(swapIndex, tempCard);  //the temporarily stored card is inserted back into the deck at the random position
  42.     }
  43. }
  44.  
  45. void Pile:: clear()
  46. {
  47.     p.clear();
  48. }
  49.  
  50. Pile Pile:: operator + (const Card& c)
  51. {
  52.     p.push_back(c);
  53. }
  54.  
  55. Pile Pile:: operator + (const Pile& b) const
  56. {
  57.     while(b.p.size() > 0)
  58.     {
  59.         p.push_back(b.p.front());
  60.         b.p.erase(b.p.begin());
  61.     }
  62. }
  63.  
  64. ostream& operator << (ostream& out, const Pile& p)
  65. {
  66.     int count = 0;
  67.     int index = 0;
  68.    
  69.     while(p.p.size() > 0)
  70.     {
  71.         out << p.p[index] << setw(2);
  72.         count++;
  73.         index++;
  74.        
  75.         if(count = 10)
  76.         {
  77.             out << endl;
  78.             count = 0;
  79.         }
  80.     }
  81. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement