Dr_Max_Experience

Task 4

Mar 27th, 2022 (edited)
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.22 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace ООП
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             string userInput;
  14.             int cardsCount;
  15.             bool correctImput = false;
  16.             string[] newDeck =  { "6♠", "7♠", "8♠", "9♠", "10♠", "В♠", "Д♠", "К♠", "Т♠",
  17.             "6♥", "7♥", "8♥", "9♥", "10♥", "В♥", "Д♥", "К♥", "Т♥",
  18.             "6♦", "7♦", "8♦", "9♦", "10♦", "В♦", "Д♦", "К♦", "Т♦",
  19.             "6♣", "7♣", "8♣", "9♣", "10♣", "В♣", "Д♣", "К♣", "Т♣",};
  20.  
  21.             Deck deck = new Deck(new List<Card>());
  22.             deck.CreateCards(newDeck);
  23.  
  24.             Player player = new Player(new List<Card>());
  25.  
  26.             while (correctImput == false)
  27.             {
  28.                 Console.Clear();
  29.                 Console.Write("Введите кол-во карт, которое необходимо взять: ");
  30.                 userInput = Console.ReadLine();
  31.  
  32.                 correctImput = int.TryParse(userInput, out cardsCount);
  33.  
  34.                 if (correctImput && cardsCount > 0 && cardsCount <= deck.CardsCount())
  35.                 {
  36.                     for (int i = 0; i < cardsCount; i++)
  37.                     {
  38.                         player.TakeCard(deck.GiveRandomCard());
  39.                     }
  40.                 }
  41.                 else
  42.                 {
  43.                     correctImput = false;
  44.                     Console.WriteLine("Некорректный ввод!");
  45.                     Console.ReadKey();
  46.                 }
  47.             }
  48.  
  49.             Console.Write("Ваши карты: ");
  50.             player.ShowCards();
  51.  
  52.             Console.ReadKey();
  53.         }
  54.     }
  55.  
  56.     class Deck
  57.     {
  58.         private List<Card> _deck;
  59.  
  60.         public Deck(List<Card> cards)
  61.         {
  62.             _deck = cards;
  63.         }
  64.  
  65.         public void CreateCards(string[] cards)
  66.         {
  67.             foreach (var card in cards)
  68.             {
  69.                 _deck.Add(new Card(card));
  70.             }
  71.         }
  72.  
  73.         public Card GiveRandomCard()
  74.         {
  75.             Random random = new Random();
  76.             int index = random.Next(_deck.Count - 1);
  77.             Card card = _deck[index];
  78.             _deck.RemoveAt(index);
  79.  
  80.             return card;
  81.         }
  82.  
  83.         public int CardsCount()
  84.         {
  85.             return _deck.Count;
  86.         }
  87.     }
  88.  
  89.     class Card
  90.     {
  91.         private string _cardName;
  92.  
  93.         public Card(string cardName)
  94.         {
  95.             _cardName = cardName;
  96.         }
  97.  
  98.         public void ShowCard()
  99.         {
  100.             Console.Write(_cardName);
  101.         }
  102.     }
  103.  
  104.     class Player
  105.     {
  106.         private List<Card> _deck;
  107.  
  108.         public Player(List<Card> cards)
  109.         {
  110.             _deck = cards;
  111.         }
  112.  
  113.         public void TakeCard(Card cardName)
  114.         {
  115.             _deck.Add(cardName);
  116.         }
  117.  
  118.         public void ShowCards()
  119.         {
  120.             foreach (var card in _deck)
  121.             {
  122.                 card.ShowCard();
  123.                 Console.Write(" ");
  124.             }
  125.         }
  126.     }
  127. }
Add Comment
Please, Sign In to add comment