WindFell

Moba Challanger

Jun 22nd, 2018
217
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.85 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. class MobaChalanger
  7. {
  8.     static void Main(string[] args)
  9.     {
  10.         var players = new Dictionary<string, Dictionary<string, int>>();
  11.  
  12.         string input;
  13.  
  14.         while ((input = Console.ReadLine()) != "Season end")
  15.         {
  16.             string[] commandArgs = input.Split();
  17.  
  18.             switch (commandArgs[1])
  19.             {
  20.                 case "->":
  21.                     string playerName = commandArgs[0];
  22.                     string position = commandArgs[2];
  23.                     int skill = int.Parse(commandArgs[4]);
  24.                     AddPlayer(playerName, position, skill, players);
  25.                     break;
  26.                 case "vs":
  27.                     string playerOne = commandArgs[0];
  28.                     string playerTwo = commandArgs[2];
  29.                     Duel(playerOne, playerTwo, players);
  30.                     break;
  31.             }
  32.         }
  33.  
  34.         string result = PrintPlayers(players);
  35.  
  36.         Console.WriteLine(result);
  37.     }
  38.  
  39.     private static void AddPlayer(string playerName, string position, int skill, Dictionary<string, Dictionary<string, int>> players)
  40.     {
  41.         if (players.ContainsKey(playerName) == false)
  42.         {
  43.             players.Add(playerName, new Dictionary<string, int>());
  44.         }
  45.  
  46.         if (players[playerName].ContainsKey(position) == false)
  47.         {
  48.             players[playerName].Add(position, skill);
  49.         }
  50.  
  51.         if (players[playerName][position] < skill)
  52.         {
  53.             players[playerName][position] = skill;
  54.         }
  55.     }
  56.  
  57.     private static void Duel(string playerOne, string playerTwo, Dictionary<string, Dictionary<string, int>> players)
  58.     {
  59.         if (players.ContainsKey(playerOne) == false || players.ContainsKey(playerTwo) == false)
  60.         {
  61.             return;
  62.         }
  63.  
  64.         bool samePositionPresent = false;
  65.  
  66.         foreach (var fisrtPosition in players[playerOne].Keys)
  67.         {
  68.             if (players[playerTwo].ContainsKey(fisrtPosition))
  69.             {
  70.                 samePositionPresent = true;
  71.                 break;
  72.             }
  73.         }
  74.  
  75.         if (samePositionPresent == false)
  76.         {
  77.             return;
  78.         }
  79.  
  80.         int playerOneTotalSkill = players[playerOne].Values.Sum();
  81.         int playerTwoTotalSkill = players[playerTwo].Values.Sum();
  82.  
  83.         if (playerOneTotalSkill == playerTwoTotalSkill)
  84.         {
  85.             return;
  86.         }
  87.  
  88.         if (playerOneTotalSkill > playerTwoTotalSkill)
  89.         {
  90.             players.Remove(playerTwo);
  91.         }
  92.         else
  93.         {
  94.             players.Remove(playerOne);
  95.         }
  96.     }
  97.     private static string PrintPlayers(Dictionary<string, Dictionary<string, int>> players)
  98.     {
  99.         StringBuilder builder = new StringBuilder();
  100.  
  101.         foreach (var player in players
  102.             .OrderByDescending(p => p.Value.Values.Sum())
  103.             .ThenBy(p => p.Key))
  104.         {
  105.             string playerName = player.Key;
  106.             int totalSkill = player.Value.Values.Sum();
  107.             builder.AppendLine($"{playerName}: {totalSkill} skill");
  108.  
  109.             foreach (var position in player.Value
  110.                 .OrderByDescending(p => p.Value)
  111.                 .ThenBy(p => p.Key))
  112.             {
  113.                 string positionType = position.Key;
  114.                 int skill = position.Value;
  115.                 builder.AppendLine($"- {positionType} <::> {skill}");
  116.             }
  117.         }
  118.  
  119.         string result = builder.ToString().TrimEnd();
  120.  
  121.         return result;
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment