Advertisement
Rodunskiy

Untitled

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