Advertisement
Rodunskiy

Untitled

May 21st, 2025
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.30 KB | None | 0 0
  1. using System;
  2. using System.ComponentModel;
  3. using System.Globalization;
  4.  
  5. namespace CSLight
  6. {
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             GameDeck deck = new GameDeck();
  12.             deck.InitializeDeck();
  13.  
  14.             GamingTable gamingTable = new GamingTable(deck);
  15.             gamingTable.GiveCards();
  16.             gamingTable.ShowPlayerCards();
  17.         }
  18.     }
  19.  
  20.     class GamingTable
  21.     {
  22.         private const int MaxCardsInDeck = 24;
  23.  
  24.         private GameDeck _gameDeck;
  25.  
  26.         private Player _player = new Player();
  27.  
  28.         private bool _isInputValid = true;
  29.         private int _cardsToDistribute = 0;
  30.  
  31.         public GamingTable(GameDeck gameDeck)
  32.         {
  33.             _gameDeck = gameDeck;
  34.         }
  35.  
  36.         public void GiveCards()
  37.         {
  38.             while (_isInputValid)
  39.             {
  40.                 Console.WriteLine("Сколько карт вы хотите получить?(в колоде 24 карты)");
  41.                 _cardsToDistribute = ReadInt();
  42.  
  43.                 if (_cardsToDistribute > MaxCardsInDeck)
  44.                 {
  45.                     Console.WriteLine("Вы ввели больше 24 карт. Попробуйте еще раз.");
  46.                 }
  47.                 else if (_cardsToDistribute <= 0)
  48.                 {
  49.                     Console.WriteLine("Введите положительное число карт.");
  50.                 }
  51.                 else
  52.                 {
  53.                     _isInputValid = false;
  54.                 }
  55.             }
  56.  
  57.             for (int i = 0; i < _cardsToDistribute; i++)
  58.             {
  59.                 if (_gameDeck.TryGiveCard(out Card card))
  60.                 {
  61.                     _player.TakeCards(card);
  62.                 }
  63.             }
  64.         }
  65.  
  66.         public void ShowPlayerCards()
  67.         {
  68.             _player.ShowCards();
  69.         }
  70.  
  71.         private int ReadInt()
  72.         {
  73.             int result;
  74.  
  75.             while (int.TryParse(Console.ReadLine(), out result) == false)
  76.             {
  77.                 Console.WriteLine("Ошибка. Попробуйте еще раз:");
  78.             }
  79.  
  80.             return result;
  81.         }
  82.     }
  83.  
  84.     class Player
  85.     {
  86.         private List<Card> _cards = new List<Card>();
  87.  
  88.         public void TakeCards(Card card)
  89.         {
  90.             _cards.Add(card);
  91.         }
  92.  
  93.         public void ShowCards()
  94.         {
  95.             if (_cards.Count == 0)
  96.             {
  97.                 Console.WriteLine("У игрока нет карт");
  98.                 return;
  99.             }
  100.  
  101.             foreach (var card in _cards)
  102.             {
  103.                 card.ShowInfo();
  104.             }
  105.         }
  106.     }
  107.  
  108.     class GameDeck
  109.     {        
  110.         private List<Card> _cards = new List<Card>(24);
  111.  
  112.         private Random _random = new Random();
  113.  
  114.         public void InitializeDeck()
  115.         {
  116.             string[] suits = { "Черви", "Бубны", "Крести", "Пики" };
  117.             string[] meanings = {"9", "10", "Валет", "Дама", "Король", "Туз" };
  118.  
  119.             foreach (string suit in suits)
  120.             {
  121.                 foreach (string meaning in meanings)
  122.                 {
  123.                     _cards.Add(new Card(suit, meaning));
  124.                 }
  125.             }
  126.  
  127.             Shuffle();
  128.         }
  129.  
  130.         public bool TryGiveCard(out Card card)
  131.         {
  132.             if (_cards.Count > 0)
  133.             {
  134.                 card = _cards[0];
  135.                 _cards.RemoveAt(0);
  136.                 return true;
  137.             }
  138.  
  139.             card = null;
  140.             return false;
  141.         }
  142.  
  143.         private void Shuffle()
  144.         {
  145.             for (int i = _cards.Count - 1; i >= 1; i--)
  146.             {
  147.                 int j = _random.Next(i + 1);
  148.                 Card temp = _cards[j];
  149.                 _cards[j] = _cards[i];
  150.                 _cards[i] = temp;
  151.             }
  152.         }
  153.     }
  154.  
  155.     class Card
  156.     {
  157.         public Card(string suit, string meaning)
  158.         {
  159.             Suit = suit;
  160.             Meaning = meaning;
  161.         }
  162.  
  163.         public string Suit { get; private set; }
  164.         public string Meaning { get; private set; }
  165.  
  166.         public void ShowInfo()
  167.         {
  168.             Console.WriteLine($"Карта {Meaning} | {Suit }");
  169.         }
  170.     }
  171. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement