Advertisement
IvanOseledko

Homework41

Feb 7th, 2025
55
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.25 KB | None | 0 0
  1. using System;
  2.  
  3. namespace Homework41
  4. {
  5.     class Program
  6.     {
  7.         static void Main(string[] args)
  8.         {
  9.             Player player = new Player();
  10.             Deck deck = new Deck();
  11.             Dealer dealer = new Dealer(deck, player);
  12.  
  13.             player.RequestCards();
  14.             dealer.IssueCards();
  15.             player.ShowAllCadrs();
  16.         }
  17.     }
  18.  
  19.     class Dealer
  20.     {
  21.         private Deck _deck;
  22.         private Player _player;
  23.  
  24.         public Dealer(Deck deck, Player player)
  25.         {
  26.             _deck = deck;
  27.             _player = player;
  28.         }
  29.  
  30.         private bool TryIssueCards()
  31.         {
  32.             return _deck.GetCardsCount() >= _player.DesiredCards;
  33.         }
  34.  
  35.         public void IssueCards()
  36.         {
  37.             if (TryIssueCards() == true)
  38.             {
  39.                 List<Card> cards = new List<Card>();
  40.  
  41.                 for (int i = 0; i < _player.DesiredCards; i++)
  42.                 {
  43.                     cards.Add(_deck.GetCard());
  44.                 }
  45.  
  46.                 _player.TakeNewCards(cards);
  47.             }
  48.             else
  49.             {
  50.                 Console.WriteLine("\nНедостаточно карт в колоде.");
  51.             }
  52.         }
  53.     }
  54.  
  55.     class Player
  56.     {
  57.         private List<Card> _cards = new List<Card>();
  58.        
  59.         public int DesiredCards { get; private set; }
  60.  
  61.         public void ShowAllCadrs()
  62.         {
  63.             Console.WriteLine("\nКарты игрока: \n");
  64.  
  65.             foreach (var card in _cards)
  66.             {
  67.                 card.ShowInfo();
  68.             }
  69.         }
  70.  
  71.         public void TakeNewCards(List<Card> cards)
  72.         {
  73.             if (cards.Count == 0)
  74.             {
  75.                 Console.WriteLine("\nНевозможно выдать 0 карт...");
  76.             }
  77.             else
  78.             {
  79.                 _cards.AddRange(cards);
  80.             }
  81.         }
  82.  
  83.         public void RequestCards()
  84.         {
  85.             int desiredCards;
  86.            
  87.             Console.Write("Введите количество карт, которое хотите получить: ");
  88.  
  89.             while (!int.TryParse(Console.ReadLine(), out desiredCards) && desiredCards > 0)
  90.             {
  91.                 Console.Write("\n\nНекорректное количество карт. Пожалуйста, введите положительное число: ");
  92.             }
  93.  
  94.             DesiredCards = desiredCards;
  95.         }
  96.     }
  97.  
  98.     class Deck
  99.     {
  100.         private Stack<Card> _cards = new Stack<Card>();        
  101.  
  102.         public Deck()
  103.         {
  104.             FillWithCards();
  105.  
  106.             CurrentCardsCount = _cards.Count;
  107.         }
  108.  
  109.         public int CurrentCardsCount { get; private set; }
  110.        
  111.         public void FillWithCards()
  112.         {
  113.             foreach (CardSuit suit in Enum.GetValues(typeof(CardSuit)))
  114.             {
  115.                 foreach (CardValue value in Enum.GetValues(typeof(CardValue)))
  116.                 {
  117.                     _cards.Push(new Card(suit, value));
  118.                 }
  119.             }
  120.         }
  121.  
  122.         public int GetCardsCount()
  123.         {
  124.             return _cards.Count;
  125.         }
  126.  
  127.         public Card GetCard()
  128.         {
  129.             if (_cards.Count == 0)
  130.             {
  131.                 Console.WriteLine("\nКолода пуста, невозможно взять карту.");
  132.  
  133.                 return null;
  134.             }
  135.            
  136.             CurrentCardsCount--;
  137.  
  138.             return _cards.Pop();            
  139.         }
  140.     }
  141.  
  142.     class Card
  143.     {
  144.         public Card(CardSuit suit, CardValue value)
  145.         {
  146.             Suit = suit;
  147.             Value = value;
  148.         }
  149.  
  150.         public CardSuit Suit { get; private set; }
  151.         public CardValue Value { get; private set; }
  152.  
  153.         public void ShowInfo()
  154.         {
  155.             Console.WriteLine($"Масть: {Suit}, значение: {Value}");
  156.         }
  157.     }
  158.  
  159.     public enum CardSuit
  160.     {
  161.         Hearts,    
  162.         Spades,    
  163.         Clubs,    
  164.         Diamonds  
  165.     }
  166.  
  167.     public enum CardValue
  168.     {
  169.         Six = 6,
  170.         Seven,
  171.         Eight,
  172.         Nine,
  173.         Ten,
  174.         Jack,    
  175.         Queen,  
  176.         King,    
  177.         Ace      
  178.     }
  179. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement