Advertisement
dimipan80

Hands of Cards

Mar 14th, 2017
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.59 KB | None | 0 0
  1. /*
  2. You are given a sequence of people and for every person what cards he draws from the deck.
  3. The input will be separate lines in the format: {personName}: {PT, PT, PT,… PT}
  4. Where P (2, 3, 4, 5, 6, 7, 8, 9, 10, J, Q, K, A) is the power of the card and T (S, H, D, C) is the type.
  5. The input ends when a "JOKER" is drawn. The name can contain any ASCII symbol except ':'.
  6. The input will always be valid and in the format described, there is no need to check it.
  7. A single person cannot have more than one card with the same power and type, if he draws such a card he discards it.
  8. The people are playing with multiple decks. Each card has a value that is calculated by the power multiplied by the type.
  9. Powers 2 to 10 have the same value and J to A are 11 to 14.
  10. Types are mapped to multipliers the following way (S -> 4, H-> 3, D -> 2, C -> 1).
  11. Finally print out the total value each player has in his hand in the format: {personName}: {value}
  12. */
  13.  
  14. namespace Hands_of_Cards
  15. {
  16.     using System;
  17.     using System.Collections.Generic;
  18.  
  19.     internal static class HandsOfCards
  20.     {
  21.         private static void Main()
  22.         {
  23.             var cardsPower = new Dictionary<string, int>
  24.             {
  25.                 { "j",  11 },
  26.                 { "q",  12 },
  27.                 { "k",  13 },
  28.                 { "a",  14 }
  29.             };
  30.  
  31.             var cardsType = new Dictionary<char, int>
  32.             {
  33.                 { 'c', 1 },
  34.                 { 'd', 2 },
  35.                 { 'h', 3 },
  36.                 { 's', 4 }
  37.             };
  38.  
  39.             var playersPoints = new Dictionary<string, int>();
  40.             var playersCards = new Dictionary<string, HashSet<string>>();
  41.  
  42.             while (true)
  43.             {
  44.                 var playerHandParts = Console.ReadLine().Split(
  45.                    new char[] { ':' }, StringSplitOptions.RemoveEmptyEntries);
  46.                 if (playerHandParts[0].Trim() == "JOKER")
  47.                 {
  48.                     break;
  49.                 }
  50.  
  51.                 var playerName = playerHandParts[0].Trim();
  52.                 if (!playersCards.ContainsKey(playerName))
  53.                 {
  54.                     playersCards.Add(playerName, new HashSet<string>());
  55.                 }
  56.  
  57.                 var handCards = playerHandParts[1].Split(
  58.                     new char[] { ',', ' ', ';', '.' }, StringSplitOptions.RemoveEmptyEntries);
  59.                
  60.                 for (int i = 0; i < handCards.Length; i++)
  61.                 {
  62.                     playersCards[playerName].Add(handCards[i].Trim().ToLower());
  63.                 }
  64.             }
  65.  
  66.             foreach (var entry in playersCards)
  67.             {
  68.                 if (!playersPoints.ContainsKey(entry.Key))
  69.                 {
  70.                     playersPoints[entry.Key] = 0;
  71.                 }
  72.  
  73.                 foreach (var card in entry.Value)
  74.                 {
  75.                     var cardPower = card.Substring(0, card.Length - 1);
  76.                     var cardType = card[card.Length - 1];
  77.                     if (cardsPower.ContainsKey(cardPower))
  78.                     {
  79.                         playersPoints[entry.Key] +=
  80.                             cardsPower[cardPower] * cardsType[cardType];
  81.                     }
  82.                     else
  83.                     {
  84.                         playersPoints[entry.Key] +=
  85.                             int.Parse(cardPower) * cardsType[cardType];
  86.                     }
  87.                 }
  88.             }
  89.  
  90.             foreach (var person in playersPoints)
  91.             {
  92.                 Console.WriteLine("{0}: {1}", person.Key, person.Value);
  93.             }
  94.         }
  95.     }
  96. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement