Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace _05HandsOfCards
- {
- class Program
- {
- private static void AddCards(List<string> playersAndCards, Dictionary<string, List<string>> dict)
- {
- string player = playersAndCards[0];
- var cards = playersAndCards.Where((x, index) => index != 0).Distinct().ToList();
- if (dict.ContainsKey(player))
- {
- foreach (var card in cards)
- {
- if (!(dict[player].Contains(card)))
- {
- dict[player].Add(card);
- }
- }
- }
- else
- {
- dict.Add(player, new List<string>());
- dict[player].AddRange(cards);
- }
- }
- private static int CalculateLeft(string card)
- {
- int sum = 0;
- string number = new string(card.Where(char.IsDigit).ToArray());
- if (number.Any())
- {
- sum += int.Parse(number);
- }
- else
- {
- switch (card[0])
- {
- case 'J':
- sum += 11;
- break;
- case 'Q':
- sum += 12;
- break;
- case 'K':
- sum += 13;
- break;
- case 'A':
- sum += 14;
- break;
- default:
- break;
- }
- }
- return sum;
- }
- private static int CalculateRight(string card)
- {
- char letter = card[card.Length - 1];
- switch (letter)
- {
- case 'S':
- return 4;
- case 'H':
- return 3;
- case 'D':
- return 2;
- default:
- return 1;
- }
- }
- private static int CalculateCardPoints(List<string> hand)
- {
- int sum = 0;
- foreach (var card in hand)
- {
- sum += CalculateLeft(card) * CalculateRight(card);
- }
- return sum;
- }
- private static void EndGame(Dictionary<string, List<string>> dict)
- {
- var results = new Dictionary<string, int>();
- foreach (KeyValuePair<string, List<string>> player in dict)
- {
- results.Add(player.Key, CalculateCardPoints(player.Value));
- }
- foreach (var result in results)
- {
- Console.WriteLine("{0}: {1}", result.Key, result.Value);
- }
- }
- static void Main(string[] args)
- {
- var players = new Dictionary<string, List<string>>();
- bool done = false;
- while (!done)
- {
- var playersAndCards = Console.ReadLine().Split(new char[] { ':', ' ', ',' }, StringSplitOptions.RemoveEmptyEntries).ToList();
- switch (playersAndCards[0])
- {
- case "JOKER":
- EndGame(players);
- done = true;
- break;
- default:
- AddCards(playersAndCards, players);
- break;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement