Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. #include <iostream>
  2. #include <vector>
  3. #include <string>
  4. #include <sstream>
  5. #include <cmath>
  6.  
  7. using namespace std;
  8.  
  9. struct card{
  10.     string suit;
  11.     int rank;
  12.  
  13.     void suitexp(){
  14.             if(suit == "h"){suit = "Hearts";}
  15.             if(suit == "d"){suit = "Diamonds";}
  16.             if(suit == "s"){suit = "Spades";}
  17.             if(suit == "c"){suit = "Clubs";}
  18.         }
  19.  
  20.     string str(){
  21.         stringstream sr;
  22.         if(rank==1){sr << "Ace" << " of " << suit;
  23.         }
  24.         if(rank==11){sr << "Jack" << " of " << suit;
  25.         }
  26.         if(rank==12){sr << "Queen" << " of " << suit;
  27.         }
  28.         if(rank==13){sr << "King" << " of " << suit;
  29.         }
  30.         if((rank!=1) && (rank!=11) && (rank!=12) && (rank!=13)){sr << rank << " of " << suit;
  31.         }
  32.         return sr.str();
  33.     }
  34.  
  35. };
  36.  
  37. void winningcards(const card opp, const vector<card>& me, vector<card>& ans);
  38.  
  39.  
  40. int main(){
  41.     vector<card> hand;
  42.     card me;
  43.     card opp;
  44.     vector<card> ans;
  45.     int num;
  46.     cout << "Please enter the card your opponent played in the form \"d 11\" for the Jack of Diamonds: " << endl;
  47.     cin >> opp.suit >> opp.rank;
  48.     cout << "How many cards do you hold in your hand?" << endl;
  49.     cin >> num;
  50.     if(num !=0){
  51.         cout << "Please enter the cards you have in your hand in the same form as before: " << endl
  52.     ;}
  53.     while( (hand.size() < num) && (num !=0)){
  54.         cin >> me.suit >> me.rank;
  55.             hand.push_back ( me ) ;
  56.         }
  57.     opp.suitexp();
  58.     cout << "Your opponent played: " << opp.str() << endl;
  59.     winningcards(opp, hand, ans);
  60.     for(int i = 0; i<=hand.size();i++){
  61.         if(hand[i].suit.compare(opp.suit)==0){
  62.             cout << "Your best option is: ";
  63.             for(vector<card>::iterator it = ans.begin(); it!=ans.end(); it++) {
  64.                     it->suitexp();
  65.                     cout << it->str() << endl;
  66.             } cout<< "Your other options are: ";
  67.             for(vector<card>::iterator it = ans.begin(); it!=ans.end(); it++) {
  68.                     it->suitexp();
  69.                     cout << it->str() << endl;
  70.             }
  71.  
  72.         }else{ cout << "You don't have any winning cards!" << endl;
  73.  
  74.         }
  75.     return 0;
  76. }
  77. }
  78.  
  79. void winningcards(const card opp, const vector<card>& me, vector<card>& ans){
  80.     for(int i = 0; i<me.size() - 1; i++){
  81.         if((opp.rank != 1) && (me[i].suit.compare(opp.suit)==0) && (me[i].rank > opp.rank)){
  82.             ans.push_back(me[i]);
  83.         }
  84.     }
  85.     for(int j=1; j<ans.size(); j++){
  86.         if((ans[0].rank > ans[j].rank) || (ans[j].rank == 0)){
  87.             card tmp;
  88.             tmp = ans[0];
  89.             ans[0] = ans[j];
  90.             ans[j]=tmp;
  91.         }
  92.  
  93.     }
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement