Advertisement
DeeAG

Problem3

Mar 3rd, 2021
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.97 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace Problem3
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             List<string> cards = Console.ReadLine()
  12.                 .Split(":", StringSplitOptions.RemoveEmptyEntries)
  13.                 .ToList();
  14.  
  15.             List<string> deck = new List<string>();
  16.  
  17.             while (true)
  18.             {
  19.                 string line = Console.ReadLine();
  20.  
  21.                 if (line == "Ready")
  22.                 {
  23.                     break;
  24.                 }
  25.  
  26.                 string[] tokens = line.Split(' ', StringSplitOptions.RemoveEmptyEntries);
  27.  
  28.                 string command = tokens[0];
  29.  
  30.                 if (command == "Add")
  31.                 {
  32.                     string cardName = tokens[1];
  33.  
  34.                     if (Exist(cards, cardName))
  35.                     {
  36.                       deck.Add(cardName);  
  37.                     }
  38.                     else
  39.                     {
  40.                         Console.WriteLine("Card not found.");
  41.                     }
  42.                 }
  43.                 else if (command == "Insert")
  44.                 {
  45.                     string cardName = tokens[1];
  46.                     int index = int.Parse(tokens[2]);
  47.  
  48.                     if (Exist(cards, cardName) &&
  49.                         IsValidIndex(cards, index))
  50.                     {
  51.                         deck.Insert(index, cardName);
  52.                     }
  53.                     else
  54.                     {
  55.                         Console.WriteLine("Error!");
  56.                     }
  57.                 }
  58.                 else if (command == "Remove")
  59.                 {
  60.                     string cardName = tokens[1];
  61.  
  62.                     if (Exist(deck, cardName))
  63.                     {
  64.                         deck.Remove(cardName);
  65.                     }
  66.                     else
  67.                     {
  68.                         Console.WriteLine("Card not found.");
  69.                     }
  70.                 }
  71.                 else if (command == "Swap")
  72.                 {
  73.                     string cardName1 = tokens[1];
  74.                     string cardName2 = tokens[2];
  75.  
  76.                     int indexCard1 = deck.IndexOf(cardName1);
  77.                     int indexCard2 = deck.IndexOf(cardName2);
  78.  
  79.                     string oldElement = deck[indexCard1];
  80.                     deck[indexCard1] = deck[indexCard2];
  81.                     deck[indexCard2] = oldElement;
  82.                 }
  83.                 else if (command == "Shuffle")
  84.                 {
  85.                     deck.Reverse();
  86.                 }
  87.             }
  88.  
  89.             Console.WriteLine(string.Join(' ',deck));
  90.         }
  91.  
  92.         private static bool IsValidIndex(List<string> cards, int index)
  93.         {
  94.             return index >= 0 && index < cards.Count;
  95.         }
  96.  
  97.         private static bool Exist(List<string> cards, string card)
  98.         {
  99.             return cards.Contains(card);
  100.         }
  101.     }
  102. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement