Advertisement
illiden

Cards

Jul 15th, 2022
870
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.57 KB | None | 0 0
  1. using System;
  2.  
  3. namespace CSLight // Note: actual namespace depends on the project name.
  4. {
  5.     public class Program
  6.     {
  7.         private static void Main(string[] args)
  8.         {
  9.             Pack pack = new Pack();
  10.             Player player = new Player(pack);
  11.  
  12.             bool isWorks = true;
  13.             while (isWorks)
  14.             {
  15.                 Console.WriteLine("1. Взять еще одну карту\n" +
  16.                                   "2. Взять еще какое-то количество карт\n" +
  17.                                   "3. Посмотреть, сколько карт осталось в колоде\n" +
  18.                                   "4. Посмотреть, какие карты в руке\n" +
  19.                                   "5. Выход\n");
  20.  
  21.                 int inputCommand = InputToCorrectness("Введите номер команды: ",
  22.                     "Некорректная команда, попробуйте еще раз\n");
  23.  
  24.                 switch (inputCommand)
  25.                 {
  26.                     case 1:
  27.                         if(player.TryGetCard() == false)
  28.                             Console.WriteLine("Карт больше нет");
  29.                         break;
  30.                     case 2:
  31.                         int countCards = InputToCorrectness("Сколько карт вы хотите взять?: ",
  32.                             "Некорректное число, попробуйте еще раз\n");
  33.                         if(player.TryGetCards(countCards) == false)
  34.                             Console.WriteLine("Невозможно взять столько карт");
  35.                         break;
  36.                     case 3:
  37.                         Console.WriteLine($"В колоде осталось {pack.Count} карт");
  38.                         break;
  39.                     case 4:
  40.                         player.ShowCards(Console.WriteLine);
  41.                         break;
  42.                     case 5:
  43.                         isWorks = false;
  44.                         break;
  45.                     default:
  46.                         Console.WriteLine("Такой команды не существует");
  47.                         break;
  48.                 }
  49.  
  50.                 Console.Write("\nНажмите любую клавишу...");
  51.                 Console.ReadKey();
  52.                 Console.Clear();
  53.             }
  54.         }
  55.  
  56.         private static int InputToCorrectness(string output, string error)
  57.         {
  58.             int inputNumber = 0;
  59.             bool isCorrectInput = true;
  60.             do
  61.             {
  62.                 isCorrectInput = TryInputInt(out inputNumber, output);
  63.                 if (!isCorrectInput)
  64.                     Console.WriteLine(error);
  65.             } while (!isCorrectInput);
  66.  
  67.             return inputNumber;
  68.         }
  69.        
  70.         private static bool TryInputInt(out int inputInt, string output = "")
  71.         {
  72.             Console.Write(output);
  73.             string input = Console.ReadLine() ?? "";
  74.             return int.TryParse(input, out inputInt);
  75.         }
  76.     }
  77. }
  78.  
  79. public class Player
  80. {
  81.     private List<Card> _cards;
  82.     private Pack _pack;
  83.  
  84.     public Player(Pack pack)
  85.     {
  86.         _cards = new List<Card>();
  87.         _pack = pack;
  88.     }
  89.  
  90.     public bool TryGetCards(int count)
  91.     {
  92.         if (_pack.Count < count)
  93.             return false;
  94.          
  95.         for (int i = 0; i < count; i++)
  96.         {
  97.             TryGetCard();
  98.         }
  99.  
  100.         return true;
  101.     }
  102.  
  103.     public bool TryGetCard()
  104.     {
  105.         bool canGet = _pack.TryGet(out Card card);
  106.         if (canGet)
  107.             _cards.Add(card);
  108.         return canGet;
  109.     }
  110.  
  111.     public void ShowCards(Action<string> showMethod)
  112.     {
  113.         foreach (Card card in _cards)
  114.         {
  115.             showMethod?.Invoke($"{card.Rank} - {card.Suit}");
  116.         }
  117.     }
  118. }
  119.  
  120. public class Pack
  121. {
  122.     private Stack<Card> _cards;
  123.  
  124.     public int Count => _cards.Count;
  125.  
  126.     public bool TryGet(out Card card)
  127.     {
  128.         return _cards.TryPop(out card);
  129.     }
  130.  
  131.     public Pack()
  132.     {
  133.         Card[] cards = GetAllCards();
  134.         Shuffle(cards);
  135.         SetCards(cards);
  136.     }
  137.  
  138.     private void SetCards(Card[] cards)
  139.     {
  140.         _cards = new Stack<Card>(cards);
  141.     }
  142.    
  143.     private void Shuffle<T>(IList<T> array)
  144.     {
  145.         Random random = new Random();
  146.         for (int i = 0; i < array.Count; i++)
  147.         {
  148.             int randomIndex = random.Next(0, array.Count);
  149.             (array[randomIndex], array[i]) = (array[i], array[randomIndex]);
  150.         }
  151.     }
  152.  
  153.     private Card[] GetAllCards()
  154.     {
  155.         var ranks = Enum.GetNames(typeof(Rank));
  156.         var suits = Enum.GetNames(typeof(Suit));
  157.         Card[] cards = new Card[ranks.Length * suits.Length];
  158.         for (int i = 0; i < ranks.Length; i++)
  159.         {
  160.             for (int j = 0; j < suits.Length; j++)
  161.             {
  162.                 int cardIndex = i * suits.Length + j;
  163.                 cards[cardIndex] = new Card((Rank) i, (Suit) j);
  164.             }
  165.         }
  166.  
  167.         return cards;
  168.     }
  169. }
  170.  
  171. public class Card
  172. {
  173.     public Rank Rank { get; private set; }
  174.     public Suit Suit { get; private set; }
  175.  
  176.     public Card(Rank rank, Suit suit)
  177.     {
  178.         Rank = rank;
  179.         Suit = suit;
  180.     }
  181. }
  182.  
  183. public enum Rank
  184. {
  185.     Two,
  186.     Three,
  187.     Four,
  188.     Five,
  189.     Six,
  190.     Seven,
  191.     Eight,
  192.     Nine,
  193.     Ten,
  194.     Jack,
  195.     Queen,
  196.     King,
  197.     Ace
  198. }
  199.  
  200. public enum Suit
  201. {
  202.     Clubs,
  203.     Diamonds,
  204.     Hearts,
  205.     Spades
  206. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement