Advertisement
Guest User

Untitled

a guest
Nov 23rd, 2014
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.23 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, 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.           //  hand[i].suitexp();
  62.             //    if((hand[i].suit.compare(opp.suit))==0){
  63.                         if (!ans.empty()){
  64.                         cout << "Your best option is: " << ans[0].str() << endl;
  65.                         cout << "other options include:" << endl;
  66.                         for(int i = 1; i<ans.size(); i++) {
  67.                               //  ans[i].suitexp();
  68.                                 cout <<ans[i].str() << endl;
  69.                         }
  70.                         }
  71.                 else{ cout << "You don't have any winning cards!" << endl;
  72.  
  73.                 }
  74.         return 0;
  75. }
  76.  
  77.  
  78. void winningcards(const card opp, vector<card>& me, vector<card>& ans){
  79.         for(int a = 0; a<me.size(); a++){
  80.                 me[a].suitexp();
  81.                 if((opp.rank != 1) && (me[a].rank > opp.rank) && (me[a].suit.compare(opp.suit)==0) || (me[a].rank==1) && (me[a].suit.compare(opp.suit)==0)){
  82.                        ans.push_back(me[a]);
  83.                 }
  84.         }
  85.         for(int b=1; b<ans.size(); b++){
  86.                 if((ans[b].rank > ans[0].rank) && (ans[0].rank == 1) || (ans[b].rank == 1)){
  87.                         card tmp;
  88.                         tmp = ans[0];
  89.                         ans[0] = ans[b];
  90.                         ans[b]=tmp;
  91.                 }
  92.  
  93.         }
  94.  
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement