Advertisement
Guest User

Untitled

a guest
Jan 10th, 2021
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.36 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace MOBAChallenger_withClass
  7. {
  8.     public static class Program
  9.     {
  10.         private static void Main()
  11.         {
  12.             var playersPool = new PlayersPool();
  13.  
  14.             string command;
  15.             while ((command = Console.ReadLine()) != "Season end")
  16.             {
  17.                 if (!command.Contains("vs"))
  18.                 {
  19.                     var commandArray = command.Split(" -> ");
  20.                     var playerName = commandArray[0];
  21.                     var position = commandArray[1];
  22.                     var skill = int.Parse(commandArray[2]);
  23.  
  24.                     playersPool.AddPlayerData(playerName, position, skill);
  25.                 }
  26.                 else
  27.                 {
  28.                     var commandArray = command.Split(' ');
  29.                     var firstPlayer = commandArray[0];
  30.                     var secondPlayer = commandArray[2];
  31.  
  32.                     playersPool.TryDuel(firstPlayer, secondPlayer);
  33.                 }
  34.             }
  35.  
  36.             Console.WriteLine(playersPool);
  37.         }
  38.     }
  39.  
  40.     public class PlayersPool
  41.     {
  42.         private readonly Dictionary<string, Player> _players;
  43.  
  44.         public PlayersPool()
  45.         {
  46.             _players = new Dictionary<string, Player>();
  47.         }
  48.  
  49.         public void AddPlayerData(string playerName, string position, int skill)
  50.         {
  51.             if (!_players.ContainsKey(playerName))
  52.             {
  53.                 _players.Add(playerName, new Player(playerName));
  54.             }
  55.  
  56.             _players[playerName].AddOrUpdatePosition(position, skill);
  57.         }
  58.  
  59.         public override string ToString()
  60.         {
  61.             var sb = new StringBuilder();
  62.  
  63.             foreach (var player in _players.Values
  64.                 .OrderByDescending(player => player.GetTotalSkillPoints())
  65.                 .ThenBy(player => player.Name))
  66.             {
  67.                 Console.WriteLine(player);
  68.                 sb.Append(Environment.NewLine);
  69.             }
  70.  
  71.             return sb.ToString().Trim();
  72.         }
  73.  
  74.         public void TryDuel(string firstPlayer, string secondPlayer)
  75.         {
  76.             if (!_players.ContainsKey(firstPlayer) || !_players.ContainsKey(secondPlayer))
  77.             {
  78.                 return;
  79.             }
  80.  
  81.             var playerOne = _players[firstPlayer];
  82.             var playerTwo = _players[secondPlayer];
  83.  
  84.             if (playerOne.GetTotalSkillPoints() == playerTwo.GetTotalSkillPoints())
  85.             {
  86.                 return;
  87.             }
  88.  
  89.             var duelsCount = playerOne.GetPositions()
  90.                 .Intersect(playerTwo.GetPositions())
  91.                 .Count();
  92.  
  93.             if (duelsCount <= 0)
  94.             {
  95.                 return;
  96.             }
  97.  
  98.             _players.Remove(playerOne.GetTotalSkillPoints() > playerTwo.GetTotalSkillPoints()
  99.                 ? playerTwo.Name
  100.                 : playerOne.Name);
  101.         }
  102.     }
  103.  
  104.     public class Player
  105.     {
  106.         public string Name { get; }
  107.  
  108.         private readonly Dictionary<string, int> _dictPositionSkill;
  109.  
  110.         public Player(string name)
  111.         {
  112.             Name = name;
  113.             _dictPositionSkill = new Dictionary<string, int>();
  114.         }
  115.  
  116.         public void AddOrUpdatePosition(string position, int skill)
  117.         {
  118.             if (!_dictPositionSkill.ContainsKey(position))
  119.             {
  120.                 _dictPositionSkill.Add(position, skill);
  121.             }
  122.             else if (_dictPositionSkill[position] < skill)
  123.             {
  124.                 _dictPositionSkill[position] = skill;
  125.             }
  126.         }
  127.  
  128.         public override string ToString()
  129.         {
  130.             var sb = new StringBuilder($"{Name}: {GetTotalSkillPoints()} skill");
  131.  
  132.             foreach (var (position, skill) in _dictPositionSkill
  133.                 .OrderByDescending(x => x.Value)
  134.                 .ThenBy(x => x.Key))
  135.             {
  136.                 sb.Append(Environment.NewLine);
  137.                 sb.Append($"- {position} <::> {skill}");
  138.             }
  139.  
  140.             return sb.ToString();
  141.         }
  142.  
  143.         public IEnumerable<string> GetPositions()
  144.         {
  145.             return _dictPositionSkill.Keys;
  146.         }
  147.  
  148.         public int GetTotalSkillPoints()
  149.         {
  150.             return _dictPositionSkill.Values.Sum();
  151.         }
  152.     }
  153. }
  154.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement