Advertisement
Rodunskiy

Untitled

May 21st, 2025
112
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.35 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.  
  21.     class GamingTable
  22.     {
  23.         private GameDeck _gameDeck;
  24.  
  25.         private Player _player = new Player();
  26.  
  27.         private const int MaxCardsInDeck = 24;
  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.TryGetCard(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.  
  76.             while (!int.TryParse(Console.ReadLine(), out result))
  77.             {
  78.                 Console.WriteLine("Ошибка.Попробуйте еще раз:");
  79.             }
  80.  
  81.             return result;
  82.         }
  83.     }
  84.  
  85.     class Player
  86.     {
  87.         private List<Card> _cards = new List<Card>();
  88.  
  89.         public void TakeCards(Card card)
  90.         {
  91.             _cards.Add(card);
  92.         }
  93.  
  94.         public void ShowCards()
  95.         {
  96.             if (_cards.Count == 0)
  97.             {
  98.                 Console.WriteLine("У игрока нет карт");
  99.                 return;
  100.             }
  101.  
  102.             foreach (var card in _cards)
  103.             {
  104.                 card.ShowInfo();
  105.             }
  106.         }
  107.     }
  108.  
  109.     class GameDeck
  110.     {        
  111.         private List<Card> _cards = new List<Card>(24);
  112.  
  113.         private Random _random = new Random();
  114.  
  115.         public void InitializeDeck()
  116.         {
  117.             string[] suits = { "Черви", "Бубны", "Крести", "Пики" };
  118.             string[] meanings = {"9", "10", "Валет", "Дама", "Король", "Туз" };
  119.  
  120.             foreach (string suit in suits)
  121.             {
  122.                 foreach (string meaning in meanings)
  123.                 {
  124.                     _cards.Add(new Card(suit, meaning));
  125.                 }
  126.             }
  127.  
  128.             Shuffle();
  129.         }
  130.  
  131.         private void Shuffle()
  132.         {
  133.             for (int i = _cards.Count - 1; i >= 1; i--)
  134.             {
  135.                 int j = _random.Next(i + 1);
  136.                 Card temp = _cards[j];
  137.                 _cards[j] = _cards[i];
  138.                 _cards[i] = temp;
  139.             }
  140.         }
  141.  
  142.         public bool TryGetCard(out Card card)
  143.         {
  144.             if (_cards.Count > 0)
  145.             {
  146.                 card = _cards[0];
  147.                 _cards.RemoveAt(0);
  148.                 return true;
  149.             }
  150.             else
  151.             {
  152.                 card = null;
  153.                 return false;
  154.             }
  155.         }
  156.     }
  157.  
  158.     class Card
  159.     {
  160.         public Card(string suit, string meaning)
  161.         {
  162.             _suit = suit;
  163.             _meaning = meaning;
  164.         }
  165.  
  166.         public string _suit { get; private set; }
  167.         public string _meaning { get; private set; }
  168.  
  169.         public void ShowInfo()
  170.         {
  171.             Console.WriteLine($"Карта {_meaning} | {_suit }");
  172.         }
  173.     }
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement