Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- namespace _05._Hands_of_Cards
- {
- class Program
- { // 07. Dictionaries, Lambda and LINQ - Exercises
- static void Main(string[] args)
- {
- string input = Console.ReadLine(); // Input
- Dictionary<string, int> handsOfCards = new Dictionary<string, int>(); // Final Dict with names and sum
- Dictionary<string, List<string>> cardsFromHand = new Dictionary<string, List<string>>(); //Current Dict with names and list of cards
- List<string> names = new List<string>(); // List with all not repeating names
- List<int> result = new List<int>(); List with sum of all not repeating cards
- while (input != "JOKER")
- {
- string[] personAndCards = input.Split(new string[] { ": ", ", " }, StringSplitOptions.RemoveEmptyEntries).ToArray(); // Info Array
- string name = personAndCards[0]; // Person name
- List<string> cards = new List<string>(); // List with current cards
- for (int i = 1; i < personAndCards.Length; i++)
- {
- cards.Add(personAndCards[i]); // Add current cards to List with cards
- }
- if (cardsFromHand.ContainsKey(name)) // If the name exists
- {
- foreach (var item in cardsFromHand)
- {
- for (int i = 0; i < cards.Count; i++)
- {
- if (cardsFromHand[item.Key].Contains(cards[i])) // If the card exist in the list in the Dict - does not work correctly;(
- {
- continue; ;
- }
- cardsFromHand[name].Add(cards[i]); // Else add that card to the list
- }
- }
- }
- else
- {
- cardsFromHand.Add(name, cards); //Else add to the Dict name with cards
- names.Add(name); // Add to the names list
- }
- input = Console.ReadLine();
- }
- // For calculation the card
- int sum = 0;
- int j = 0;
- foreach (var cards in cardsFromHand)
- {
- for (int i = 0; i < cards.Value.Count; i++)
- {
- char firstLetter = ' ';
- char secondLetter = ' ';
- foreach (var item in cardsFromHand)
- {
- string card = item.Value[i]; // Card
- firstLetter = card[0]; // First char of the card
- secondLetter = card[1]; // Second char of the card
- }
- int power = 0;
- int type = 0;
- if (int.TryParse(firstLetter.ToString(), out power)) // Try parse the first char if it is a number
- {
- switch (secondLetter)
- {
- case 'C':
- type = 1;
- break;
- case 'D':
- type = 2;
- break;
- case 'H':
- type = 3;
- break;
- case 'S':
- type = 4;
- break;
- }
- }
- else // Else if it is a letter
- {
- switch (firstLetter)
- {
- case 'J':
- power = 11;
- break;
- case 'Q':
- power = 12;
- break;
- case 'K':
- power = 13;
- break;
- case 'A':
- power = 14;
- break;
- default:
- power = 10;
- break;
- }
- switch (secondLetter)
- {
- case 'C':
- type = 1;
- break;
- case 'D':
- type = 2;
- break;
- case 'H':
- type = 3;
- break;
- case 'S':
- type = 4;
- break;
- }
- }
- sum += power * type; // sum
- j++;
- }
- result.Add(sum); // Add to the result list
- }
- for (int i = 0; i < result.Count; i++)
- {
- handsOfCards.Add(names[i], result[i]);
- }
- foreach (var hand in handsOfCards)
- {
- Console.WriteLine($"{hand.Key}: {hand.Value}");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement