Advertisement
Rodunskiy

Untitled

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