Advertisement
Guest User

Untitled

a guest
Nov 30th, 2018
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.51 KB | None | 0 0
  1. using namespace std;
  2. #include "Pile.h"
  3. #include <iomanip>
  4. #include <algorithm>  //shuffle
  5. #include <sstream>
  6. #include <stdexcept>
  7. #include <cstdlib>
  8.  
  9. Pile::Pile()
  10. {
  11.     p = vector<Card>();
  12. }
  13.  
  14. //draws card from the top of the pile and removes it from the vector
  15. Card Pile:: dealCard()
  16. {
  17.     if(p.size() > 0)
  18.     {
  19.         Card tempCard = p.front();
  20.         p.erase(p.begin());
  21.    
  22.         return tempCard;
  23.     }
  24.     else
  25.         throw std::length_error("Pile is empty");
  26.    
  27.        
  28. }
  29.  
  30. int Pile:: getCount() const
  31. {
  32.     return p.size();
  33. }
  34.  
  35. void Pile:: shuffle()
  36. {
  37.     random_shuffle(p.begin(), p.end());
  38. }
  39.  
  40. void Pile:: clear()
  41. {
  42.     p.clear();
  43. }
  44.  
  45. Pile Pile:: operator + (const Card& c) const
  46. {
  47.     Pile tempPile;       //make a copy of the pile to be returned
  48.     tempPile.p = p;
  49.     tempPile.p.push_back(c);  //push the new card onto the end of the pile
  50.     return tempPile;        //return the new pile
  51. }
  52.  
  53. Pile Pile:: operator + (Pile& b)
  54. {
  55.     Pile tempPile;
  56.     tempPile.p = p;
  57.    
  58.     while(b.p.size() > 0)
  59.     {
  60.         tempPile.p.push_back(b.p.front());
  61.         b.p.erase(b.p.begin());
  62.     }
  63.     return tempPile;
  64. }
  65.  
  66. ostream& operator << (ostream& out, const Pile& b)
  67. {
  68.     int count = 0;
  69.     int index = 0;
  70.     int max = b.getCount();
  71.    
  72.     for(int i = 0; i < max; i++)
  73.     {
  74.         out << b.p[index] << setw(2);
  75.         count++;
  76.         index++;
  77.        
  78.         if(count == 10)
  79.         {
  80.             out << endl;
  81.             count = 0;
  82.         }
  83.     }
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement