SHARE
TWEET

Untitled

a guest Jan 18th, 2019 56 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <vector>
  3. using namespace std;
  4.  
  5. enum SUIT { HEART, CLUB, DIAMOND, SPADE };
  6. string suit_string[] = {"HEART", "CLUB", "DIAMOND", "SPADE"};
  7.  
  8. enum VALUE { ACE, TWO, THREE, FOUR, FIVE, SIX,
  9.     SEVEN, EIGHT, NINE, TEN, JACK, QUEEN, KING};
  10. string value_string[] = {"ACE", "TWO", "THREE", "FOUR", "FIVE",
  11.     "SIX", "SEVEN", "EIGHT","NINE", "TEN", "JACK", "QUEEN", "KING"};
  12.  
  13. class Card {
  14.  public:
  15.     Card(SUIT s, VALUE v);
  16.     SUIT suit;
  17.     VALUE value;
  18.     string toString();
  19. };
  20.  
  21. Card::Card(SUIT s, VALUE v){
  22.     suit=s;
  23.     value=v;
  24. }
  25.  
  26. string Card::toString(){
  27.     return value_string[(int)this->value]+" of "+suit_string[(int)this->suit];
  28. }
  29.  
  30.  
  31.  
  32. int main() {
  33.         vector<Card*> deck;
  34.         for (int i=0; i<4; i++) {
  35.             for (int j=0; j<13; j++) {
  36.                 deck.push_back(new Card((SUIT)i,(VALUE)j));
  37.             }              
  38.         }
  39.  
  40.         int removed_count, runs = 0;
  41.  
  42.         do{
  43.             random_shuffle(deck.begin(),deck.end());
  44.             removed_count=0;
  45.  
  46.             cout<<"--- Run number: "<<++runs<<"n";
  47.             cout<<"--- still "<<deck.size()<<" cards in the deckn";
  48.  
  49.             for (int i=0; i<4; i++) {
  50.                 for (int j=0; j<13; j++) {
  51.                     //cout<<i*13+j<<"n";
  52.  
  53.                     int position=i*13+j; //from 0 to 51
  54.  
  55.                     cout<<"'"+value_string[j]+"'"<<" <-> ";
  56.  
  57.                     Card card=*(deck.at(position));
  58.                     cout<<card.toString()<<" ";
  59.  
  60.                         if(card.value == j){
  61.                             removed_count++;
  62.                             deck.erase(deck.begin()+position); //problems here!
  63.                             cout<<"removedn";
  64.                         }else{
  65.                             cout<<"not removedn";
  66.                         }
  67.  
  68.                     }              
  69.             }
  70.             if(deck.empty()) break;
  71.         }while(removed_count>0);
  72.  
  73.         if(deck.empty()){
  74.             cout<<"You win!n";
  75.         }else{
  76.             cout<<"You lose!n";
  77.         }
  78.  
  79.         return 0;
  80. }
  81.    
  82. 'KING' <-> terminate called after throwing an instance of 'std::out_of_range'
  83.   what():  vector::_M_range_check
  84.    
  85. deck.erase(deck.begin()+position);
  86.    
  87. int position=i*13+j; //from 0 to 51
  88.    
  89. Card card=*(deck.at(position));
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top