Untitled

mail@pastecode.io avatar
unknown
plain_text
7 months ago
4.2 kB
7
Indexable
Never
#include <bits/stdc++.h>
using namespace std;


vector<string>players;
vector<string>cards;
string self;
string trump;
map<string,string>game;

void getValue(string &key, string &value) {
    
    if(key == "players") {
	    char* token = std::strtok(&value[0], ",");
        while (token != nullptr) {
            players.push_back(token);
            token = std::strtok(nullptr, ",");
        }
	 } else if(key == "self") {
	        self = value;
	 } else if(key == "trump") {
	        trump = value;
	 } else if(key == "cards") {
	        char* token = std::strtok(&value[0], ",");
            while (token != nullptr) {
                cards.push_back(token);
                token = std::strtok(nullptr, ",");
            }
	 } else {
	        game[key] = value;
	 }
}

int compareCards(string& card1, string& card2) {
    
    std::string ranks = "2345678910jqka";
    int value1 = ranks.find(card1.substr(1));
    int value2 = ranks.find(card2.substr(1));
    
    return value1>value2;
    
    // if (value1 > value2) {
    //     return 1;  
    // } else if (value1 < value2) {
    //     return -1;  
    // } else {
    //     return 0;  
    // }
}

string optimalCard() {
    string patner = players[1];
    
    char leadSuit = game[players[0]][0];
    vector<string>forcedToPalyCard;
    int winner = 0;
    string maxCard = game[players[0]];
    // check 1-0
    if(game[players[1]][0] == leadSuit) {
        
        winner = compareCards(game[players[1]],game[players[0]]);
        
        if(winner == 1) {
            maxCard = game[players[1]];
        }else {
            winner = 0;
        }
        
    } else if(game[players[1]][0] == trump[0]) {
        winner = 1;
        maxCard = game[players[1]];
    }
    
    if(game[players[2]][0] == leadSuit) {
        
        
        
        if(winner == 1) {
            if(game[players[1]][0] == leadSuit) {
                winner = compareCards(game[players[1]],game[players[2]]);
                if(winner == 1) {
                    maxCard = game[players[1]];
                } else {
                    winner = 0;
                    maxCard = game[players[2]];
                }
            }    
            
        } else {
            
            if(compareCards(game[players[2]],game[players[0]])) {
                maxCard = game[players[2]];
            }
            
        }
       
        
    } else if(game[players[2]][0] == trump[0]) {
        if(winner == 1 ) {
            if(game[players[1]][0] == leadSuit) {
                winner = 0;
                maxCard = game[players[2]];
            } else if(game[players[1]][0] == trump[0]) {
                if(compareCards(game[players[2]],game[players[1]])) {
                    maxCard = game[players[2]];
                    winner = 0;
                }
            }
        } else {
            maxCard = game[players[2]];
        }
    }
    
    
    
    for(auto card:cards) {
        if(card[0] == leadSuit) {
            forcedToPalyCard.push_back(card);
        }
    }
    if(forcedToPalyCard.size() == 0) {
        string ans = cards[0];
        for(int i=1;i<cards.size();i++) {
            if(compareCards(ans,cards[i])){
                ans = cards[i];
            }
        }
        return ans;
    }
    string ans = forcedToPalyCard[0];
    for(int i=1;i<forcedToPalyCard.size();i++) {
        if(compareCards(ans,forcedToPalyCard[i])){
            ans = forcedToPalyCard[i];
        }
    }
    
    if(winner)return ans;
    
    
    ans = "";
    for(int i=0;i<forcedToPalyCard.size();i++) {
        if(compareCards(forcedToPalyCard[i], maxCard)){
            ans = forcedToPalyCard[i];
        }
    }
    return ans;
    
    
    
    
    
    
}

int main() {
	
	string s;cin>>s;
	int n = s.size();
	string curr = "";
	
	
	for(int i=0;i<n;i++) {
	    // extract key
	    int j = i;
	    string key = "";
	    while(j<n && s[j] != ':') {
	        key += s[j];
	        j++; 
	    }
	    i = j+1;
	    string value = "";
	    while(i<n && s[i] != ';') {
	        value += s[i];
	        i++;
	    }
	    
	    getValue(key,value);
	    
	}
	
	cout<<optimalCard()<<endl;
	
	

	
	
	return 0;
}