Advertisement
Rodunskiy

Untitled

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