yanass

MOBA Dict with Class

Jul 14th, 2019
110
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace _04.MOBAChallenger
  6. {
  7.     class MOBAChallenger
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             Dictionary<string, PlayerPosition> players =
  12.                 new Dictionary<string, PlayerPosition>();
  13.             string input = string.Empty;
  14.             while ((input = Console.ReadLine()) != "Season end")
  15.             {
  16.                 string[] tokens = input.Split(new string[] { " -> " },
  17.                     StringSplitOptions.RemoveEmptyEntries);
  18.                 if (tokens.Length == 3)
  19.                 {
  20.                     string player = tokens[0];
  21.                     string position = tokens[1];
  22.                     int skill = int.Parse(tokens[2]);
  23.                     if (!players.ContainsKey(player))
  24.                     {
  25.                         players.Add(player, new PlayerPosition());
  26.                     }
  27.                     players[player].AddPosition(position, skill);
  28.                 }
  29.                 else
  30.                 {
  31.                     tokens = input.Split(new string[] { " vs " },
  32.                     StringSplitOptions.RemoveEmptyEntries);
  33.                     string player1 = tokens[0];
  34.                     string player2 = tokens[1];
  35.                     if (players.ContainsKey(player1) &&
  36.                         players.ContainsKey(player2) &&
  37.                         (HaveCommonPositions(players, player1, player2)))
  38.                     {
  39.                         if (players[player1].TotalSkill > players[player2].TotalSkill)
  40.                         {
  41.                             players.Remove(player2);
  42.                         }
  43.                         else if (players[player1].TotalSkill < players[player2].TotalSkill)
  44.                         {
  45.                             players.Remove(player1);
  46.                         }
  47.                     }
  48.                 }
  49.             }
  50.             foreach (KeyValuePair<string, PlayerPosition> player in players
  51.                 .OrderByDescending(x => x.Value.TotalSkill)
  52.                 .ThenBy(x => x.Key))
  53.             {
  54.                 Console.WriteLine($"{player.Key}: {player.Value.TotalSkill} skill");
  55.                 foreach (KeyValuePair<string, int> position in player
  56.                     .Value
  57.                     .Positions
  58.                     .OrderByDescending(x => x.Value)
  59.                     .ThenBy(x => x.Key))
  60.                 {
  61.                     Console.WriteLine($"- {position.Key} <::> {position.Value}");
  62.                 }
  63.             }
  64.         }
  65.  
  66.         private static bool HaveCommonPositions(
  67.             Dictionary<string, PlayerPosition> players,
  68.             string player1, string player2)
  69.         {
  70.             foreach (KeyValuePair<string, int> position1 in players[player1].Positions)
  71.             {
  72.                 foreach (KeyValuePair<string, int> position2 in players[player2].Positions)
  73.                 {
  74.                     if (position1.Key == position2.Key)
  75.                     {
  76.                         return true;
  77.                     }
  78.                 }
  79.             }
  80.             return false;
  81.         }
  82.     }
  83.     class PlayerPosition
  84.     {
  85.         public Dictionary<string, int> Positions { get; set; }
  86.         public int TotalSkill { get; set; }
  87.         public PlayerPosition()
  88.         {
  89.             Positions = new Dictionary<string, int>();
  90.         }
  91.         public void AddPosition(string position, int skill)
  92.         {
  93.             if (!Positions.ContainsKey(position))
  94.             {
  95.                 Positions.Add(position, skill);
  96.                 TotalSkill += skill;
  97.             }
  98.             else if (Positions.ContainsKey(position) &&
  99.                 Positions[position] < skill)
  100.             {
  101.                 TotalSkill -= Positions[position];
  102.                 TotalSkill += skill;
  103.                 Positions[position] = skill;
  104.             }
  105.         }
  106.     }
  107. }
Add Comment
Please, Sign In to add comment