Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <vector>
- #include <string>
- #include <sstream>
- #include <cmath>
- using namespace std;
- struct card{
- string suit;
- int rank;
- void suitexp(){
- if(suit == "h"){suit = "Hearts";}
- if(suit == "d"){suit = "Diamonds";}
- if(suit == "s"){suit = "Spades";}
- if(suit == "c"){suit = "Clubs";}
- }
- string str(){
- stringstream sr;
- if(rank==1){sr << "Ace" << " of " << suit;
- }
- if(rank==11){sr << "Jack" << " of " << suit;
- }
- if(rank==12){sr << "Queen" << " of " << suit;
- }
- if(rank==13){sr << "King" << " of " << suit;
- }
- if((rank!=1) && (rank!=11) && (rank!=12) && (rank!=13)){sr << rank << " of " << suit;
- }
- return sr.str();
- }
- };
- void winningcards(const card opp, const vector<card>& me, vector<card>& ans);
- int main(){
- vector<card> hand;
- card me;
- card opp;
- vector<card> ans;
- int num;
- cout << "Please enter the card your opponent played in the form \"d 11\" for the Jack of Diamonds: " << endl;
- cin >> opp.suit >> opp.rank;
- cout << "How many cards do you hold in your hand?" << endl;
- cin >> num;
- if(num !=0){
- cout << "Please enter the cards you have in your hand in the same form as before: " << endl
- ;}
- while( (hand.size() < num) && (num !=0)){
- cin >> me.suit >> me.rank;
- hand.push_back ( me ) ;
- }
- opp.suitexp();
- cout << "Your opponent played: " << opp.str() << endl;
- winningcards(opp, hand, ans);
- for(int i = 0; i<=hand.size();i++){
- if(hand[i].suit.compare(opp.suit)==0){
- cout << "Your best option is: ";
- for(vector<card>::iterator it = ans.begin(); it!=ans.end(); it++) {
- it->suitexp();
- cout << it->str() << endl;
- } cout<< "Your other options are: ";
- for(vector<card>::iterator it = ans.begin(); it!=ans.end(); it++) {
- it->suitexp();
- cout << it->str() << endl;
- }
- }else{ cout << "You don't have any winning cards!" << endl;
- }
- return 0;
- }
- }
- void winningcards(const card opp, const vector<card>& me, vector<card>& ans){
- for(int i = 0; i<me.size() - 1; i++){
- if((opp.rank != 1) && (me[i].suit.compare(opp.suit)==0) && (me[i].rank > opp.rank)){
- ans.push_back(me[i]);
- }
- }
- for(int j=1; j<ans.size(); j++){
- if((ans[0].rank > ans[j].rank) || (ans[j].rank == 0)){
- card tmp;
- tmp = ans[0];
- ans[0] = ans[j];
- ans[j]=tmp;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement