TodorovP

05. Hands of Cards

Jun 10th, 2018
86
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.58 KB | None | 0 0
  1. using System;
  2. using System.Linq;
  3. using System.Collections.Generic;
  4.  
  5. namespace Hands_of_Cards
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             //var personsScore = new Dictionary<string, int>();
  12.             var players = new Dictionary<string, List<string>>();
  13.             while (true)
  14.             {
  15.                 //inpit
  16.                 string[] inputLine = Console.ReadLine() //line
  17.                 .Split(':')
  18.                 .ToArray();
  19.  
  20.                 if (inputLine[0] == "JOKER") break;
  21.  
  22.                 string playerName = inputLine[0];
  23.                 List<string> cards = inputLine[1]
  24.                     .Split(",.".ToCharArray(), StringSplitOptions.RemoveEmptyEntries)
  25.                     .ToList();
  26.  
  27.                 if (players.ContainsKey(playerName) == false)
  28.                     players.Add(playerName, new List<string>());
  29.                 players[playerName].AddRange(cards);
  30.             }
  31.  
  32.             //input of power score dictionary
  33.             var powerNames = new List<string>
  34.                 { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
  35.             var powerValues = new List<int>
  36.                 { 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 };
  37.  
  38.             var powerScoreDict = new Dictionary<string, int>();
  39.             for (int i = 0; i < powerNames.Count; i++)
  40.             {
  41.                 powerScoreDict.Add(powerNames[i], powerValues[i]);
  42.             }
  43.  
  44.             //input of type score dictionary
  45.             var typeNames = new List<string> { "S", "H", "D", "C" };
  46.             var typeValues = new List<int> { 4, 3, 2, 1 };
  47.  
  48.             var typeScoreDict = new Dictionary<string, int>();
  49.             for (int i = 0; i < typeNames.Count; i++)
  50.             {
  51.                 typeScoreDict.Add(typeNames[i], typeValues[i]);
  52.             }
  53.  
  54.             // Distinct and Output
  55.             foreach (var player in players)
  56.             {
  57.                 List<string> unuqueCards = player.Value
  58.                     .Distinct()
  59.                     .ToList();
  60.  
  61.                 int sum = 0;
  62.                 foreach (var card in unuqueCards)
  63.                 {
  64.                     string type = Convert.ToString(card[card.Length - 1]);
  65.                     string power = card.Remove(card.Length - 1, 1);
  66.  
  67.                     int typeValue = typeScoreDict[type];
  68.                     int powerValue = powerScoreDict[power];
  69.  
  70.                     sum += powerValue * typeValue;
  71.                 }
  72.  
  73.                 Console.WriteLine($"{player.Key}: {sum}");
  74.             }
  75.         }
  76.     }
  77. }
Add Comment
Please, Sign In to add comment