Advertisement
IvanOseledko

Homework41

Feb 6th, 2025
50
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.53 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.         public 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.GetNewCards(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 GetNewCards(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.             foreach (CardSuit suit in Enum.GetValues(typeof(CardSuit)))
  105.             {
  106.                 foreach (CardValue value in Enum.GetValues(typeof(CardValue)))
  107.                 {
  108.                     _cards.Push(new Card(suit, value));
  109.                 }
  110.             }
  111.  
  112.             CurrentCardsCount = _cards.Count;
  113.         }
  114.  
  115.         public int CurrentCardsCount { get; private set; }
  116.        
  117.         public void ShowInfo()
  118.         {
  119.             foreach (var card in _cards)
  120.             {
  121.                 card.ShowInfo();
  122.             }
  123.         }
  124.  
  125.         public int GetCardsCount()
  126.         {
  127.             return _cards.Count;
  128.         }
  129.  
  130.         public Card GetCard()
  131.         {
  132.             if (_cards.Count == 0)
  133.             {
  134.                 Console.WriteLine("\nКолода пуста, невозможно взять карту.");
  135.  
  136.                 return null;
  137.             }
  138.             else
  139.             {
  140.                 CurrentCardsCount--;
  141.  
  142.                 return _cards.Pop();
  143.             }
  144.         }
  145.  
  146.         public void ShowCurrentCardsCount()
  147.         {
  148.             Console.WriteLine($"\nТекущее кличество карт в колоде: {CurrentCardsCount}.");
  149.         }
  150.     }
  151.  
  152.     class Card
  153.     {
  154.         public Card(CardSuit suit, CardValue value)
  155.         {
  156.             Suit = suit;
  157.             Value = value;
  158.         }
  159.  
  160.         public CardSuit Suit { get; private set; }
  161.         public CardValue Value { get; private set; }
  162.  
  163.         public void ShowInfo()
  164.         {
  165.             Console.WriteLine($"Масть: {Suit}, значение: {Value}");
  166.         }
  167.     }
  168.  
  169.     public enum CardSuit
  170.     {
  171.         Hearts,    
  172.         Spades,    
  173.         Clubs,    
  174.         Diamonds  
  175.     }
  176.  
  177.     public enum CardValue
  178.     {
  179.         Six = 6,
  180.         Seven,
  181.         Eight,
  182.         Nine,
  183.         Ten,
  184.         Jack,    
  185.         Queen,  
  186.         King,    
  187.         Ace      
  188.     }
  189. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement