Advertisement
holllowknight

Колода карт

Apr 24th, 2023 (edited)
725
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.03 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 Cards
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int enough = 0;
  14.             int drawnCards;
  15.             bool isDrawing = true;
  16.             Player player = new Player();
  17.             Deck deck = new Deck();
  18.             deck.Mix();
  19.  
  20.             while (isDrawing)
  21.             {
  22.                 Console.WriteLine($"У вас {player.GetCardsCount()} карт");
  23.                 Console.WriteLine("Сколько карт взять из колоды?");
  24.                 Console.WriteLine($"{enough} - Для завершения");
  25.                 string userInput = Console.ReadLine();
  26.                 bool isNumber = int.TryParse(userInput, out drawnCards);
  27.  
  28.                 if (isNumber == true)
  29.                 {
  30.                     if (drawnCards == enough)
  31.                     {
  32.                         isDrawing = false;
  33.                     }
  34.                     else
  35.                     {
  36.                         for (int i = 0; i < drawnCards; i++)
  37.                         {
  38.                             if (deck.TryDrawCard(out Card card))
  39.                                 player.TakeCard(card);
  40.                         }
  41.                     }
  42.                 }
  43.             }
  44.  
  45.             player.ShowCards();
  46.         }
  47.     }
  48.  
  49.     class Card
  50.     {
  51.         private Suit _suit;
  52.         private Level _level;
  53.  
  54.         public Card(Suit suit, Level level)
  55.         {
  56.             _suit = suit;
  57.             _level = level;
  58.         }
  59.  
  60.         public enum Suit
  61.         {
  62.             Hearts = 0,
  63.             Spades,
  64.             Diamonds,
  65.             Clubs
  66.         }
  67.  
  68.         public enum Level
  69.         {
  70.             Six = 0,
  71.             Seven,
  72.             Eight,
  73.             Nine,
  74.             Ten,
  75.             Jack,
  76.             Queen,
  77.             King,
  78.             Ace
  79.         }
  80.  
  81.         public string GetName()
  82.         {
  83.             return string.Format($"{_level} {_suit}");
  84.         }
  85.     }
  86.  
  87.     class Deck
  88.     {
  89.         private List<Card> _cards;
  90.  
  91.         public Deck()
  92.         {
  93.             _cards = new List<Card>();
  94.             var levels = Enum.GetValues(typeof(Card.Level));
  95.             var suits = Enum.GetValues(typeof(Card.Suit));
  96.  
  97.             foreach (int level in levels)
  98.             {
  99.                 foreach (int suit in suits)
  100.                 {
  101.                     Card.Suit newSuit = (Card.Suit)Enum.ToObject(typeof(Card.Suit), suit);
  102.                     Card.Level newLevel = (Card.Level)Enum.ToObject(typeof(Card.Level), level);
  103.                     Card card = new Card(newSuit, newLevel);
  104.                     _cards.Add(card);
  105.                 }
  106.             }
  107.         }
  108.  
  109.         public void Mix()
  110.         {
  111.             Random random = new Random();
  112.  
  113.             for (int i = 0; i < _cards.Count; i++)
  114.             {
  115.                 int exchangedIndex = random.Next(_cards.Count);
  116.                 Card temporaryCard = _cards[i];
  117.                 _cards[i] = _cards[exchangedIndex];
  118.                 _cards[exchangedIndex] = temporaryCard;
  119.             }
  120.         }
  121.  
  122.         public bool TryDrawCard(out Card card)
  123.         {
  124.             if(_cards.Count > 0)
  125.             {
  126.                 card = _cards.First();
  127.                 _cards.Remove(card);
  128.                 return true;
  129.             }
  130.             else
  131.             {
  132.                 card = null;
  133.                 return false;
  134.             }
  135.         }
  136.     }
  137.  
  138.     class Player
  139.     {
  140.         private List<Card> _cards;
  141.  
  142.         public Player()
  143.         {
  144.             _cards = new List<Card>();
  145.         }
  146.  
  147.         public void ShowCards()
  148.         {
  149.             foreach (Card card in _cards)
  150.                 Console.WriteLine(card.GetName());
  151.         }
  152.  
  153.         public int GetCardsCount()
  154.         {
  155.             return _cards.Count;
  156.         }
  157.  
  158.         public void TakeCard(Card card)
  159.         {
  160.             _cards.Add(card);
  161.         }
  162.     }
  163. }
  164.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement