Simulate a server of a role-playing game for two players.

Each player has an “elo”, which is a value larger the better is the player. Everyone starts with 1200 points, and nobody ever gets a lower quantity, no matter how many games are lost. Whenever there is a match, the winner gets 10 elo points, and the loser loses 10 elo points (with the limitation above). The elo of a player is kept when he or she disconnects from the server.

We have these instructions:

LOGIN j : The player j starts a session. Ignore it if the player is already connected.

LOGOUT j : The player j closes the session. Ignore it if the player is not connected.

PLAY j1 ⁠ ⁠ j2 : Indicates that j_1 has beaten j_2, with j_1 \neq j_2. Ignore it but print an error message if any of the two players is not connected.

GET_ELO j : Print the player j (who was connected for sure previously, although now may be disconnected) with his or her current elo.

Input: several instructions for at most 105 players. Each player’s name is different and made up of only lowercase letters.

Output: For every instruction GET_ELO (and perhaps PLAY) print the proper output. At the end, print an empty line, the word RANKING, and a ranking sorted in decreasing order by elo (if there is a tie, print first the alfabetically smallest name) with all the players ever connected to the server.

  • A possible appraoch is to use a set to store the logged players and a map to associate each player with his/her elo
  • #include<algorithm> allows to sort a vector v using sort(v.begin(),v.end())
//STL - Role classification
#include<iostream>
#include<set>
#include<map>
#include<algorithm>

using namespace std;

int main(){
    set<string> logged;
    map<string,int> elo;
    string op;
    while(cin >> op){
        string player1;
        cin >> player1;
        if (op == "LOGIN"){
            // do something
        }
        else if (op == "LOGOUT"){
            // do something
        }
        else if (op == "PLAY"){
            // do something
        }
        else{// op == GET_ELO
            // do something
        }
    }
    cout << endl << "RANKING" << endl;
            // do something

}
//STL - Role classification
#include<iostream>
#include<set>
#include<map>
#include<algorithm>

using namespace std;

int main(){
    string cmd;
    set<string> logged;
    map<string,int> elo;

    while(cin >> cmd){
        string player1;
        cin >> player1;
        if (cmd == "LOGIN"){
            logged.insert(player1);
         if (not elo.count(player1)) elo[player1] = 1200;
     }
        else if (cmd == "LOGOUT") logged.erase(player1);
        else if (cmd == "GET_ELO") cout << player1 << ' ' << elo[player1] << endl;
        else{// case of PLAY
            string player2;
            cin >> player2;
            if (not logged.count(player1) or not logged.count(player2)) cout << "player(s) not connected" << endl;
            else{
            elo[player1] +=10;
                elo[player2] = max(elo[player2]-10,1200);
            }
        }
    }
    cout << endl << "RANKING" << endl;
    vector<pair<int,string>> v;
    for (auto p : elo) v.emplace_back(-p.second,p.first);
    sort(v.begin(),v.end());
    for (auto p :v) cout << p.second << ' ' << -p.first << endl;
}