Advertisement
Torgach

PlayingCards

Jan 19th, 2023 (edited)
750
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.29 KB | None | 0 0
  1. using System;
  2. using System.Collections;
  3. using System.Collections.Generic;
  4. using System.Collections.ObjectModel;
  5. using System.Linq;
  6. using System.Text;
  7. using System.Threading.Tasks;
  8. using System.IO;
  9.  
  10. namespace Основы.Практика
  11. {
  12.     class Program
  13.     {
  14.         static void Main(string[] args)
  15.         {
  16.             Menu menu = new Menu();
  17.             menu.Run();
  18.         }
  19.     }
  20.  
  21.     class Menu
  22.     {
  23.         private const string ExitCommand = "0";
  24.         private const string RequestCardCommand = "1";
  25.         private const string RequestToShowHandCommand = "2";
  26.  
  27.         private Player _player;
  28.         private Deck _playingDeck;
  29.         private bool _isRun;
  30.  
  31.         public Menu()
  32.         {
  33.             _player = new Player();
  34.             _playingDeck = new Deck();
  35.  
  36.             _isRun = true;
  37.         }
  38.  
  39.         public void Run()
  40.         {
  41.             while (_isRun)
  42.             {
  43.                 Console.WriteLine("\n[1] - Достать карту\n" +
  44.                     "[2] - Показать набранную руку\n" +
  45.                     "[0] - Выход");
  46.  
  47.                 Console.WriteLine($"\nУ вас в руке {_player.GetCardsCount()} карт\n");
  48.                 Console.Write("Ввод: ");
  49.  
  50.                 string userInput = Console.ReadLine();
  51.                 Console.Clear();
  52.  
  53.                 switch (userInput)
  54.                 {
  55.                     case RequestCardCommand:
  56.                         RequestCard();
  57.                         break;
  58.  
  59.                     case RequestToShowHandCommand:
  60.                         RequestToShowHand();
  61.                         break;
  62.  
  63.                     case ExitCommand:
  64.                         Console.WriteLine("Выход");
  65.                         _isRun = false;
  66.                         break;
  67.  
  68.                     default:
  69.                         Console.WriteLine("Неправильный ввод!");
  70.                         break;
  71.                 }
  72.             }
  73.         }
  74.  
  75.         private void RequestCard()
  76.         {
  77.             if (_player.IsMaxHand())
  78.             {
  79.                 return;
  80.             }
  81.  
  82.             Card card = _playingDeck.GetCard();
  83.             _player.TakeCard(card);
  84.         }
  85.  
  86.         private void RequestToShowHand()
  87.         {
  88.             if (_player.IsHandEmpty())
  89.             {
  90.                 return;
  91.             }
  92.  
  93.             _player.ShowHand();
  94.             _playingDeck.Shuffle();
  95.         }
  96.     }
  97.  
  98.     class Player
  99.     {
  100.         private const int MaxCardsNumber = 7;
  101.  
  102.         private List<Card> _cards;
  103.  
  104.         public Player()
  105.         {
  106.             _cards = new List<Card>();
  107.         }
  108.  
  109.         public int GetCardsCount()
  110.         {
  111.             return _cards.Count;
  112.         }
  113.  
  114.         public bool IsMaxHand()
  115.         {
  116.             if (_cards.Count == MaxCardsNumber)
  117.             {
  118.                 Console.WriteLine("В руке максимальное количество карт!");
  119.                 return true;
  120.             }
  121.  
  122.             return false;
  123.         }
  124.  
  125.         public bool IsHandEmpty()
  126.         {
  127.             if (_cards.Count == 0)
  128.             {
  129.                 Console.WriteLine("В руке нет карт!");
  130.                 return true;
  131.             }
  132.  
  133.             return false;
  134.         }
  135.  
  136.         public void TakeCard(Card card)
  137.         {
  138.             _cards.Add(card);
  139.         }
  140.  
  141.         public void ShowHand()
  142.         {
  143.             Console.WriteLine("Карты в руке...");
  144.  
  145.             foreach (Card card in _cards)
  146.             {
  147.                 Console.Write(card.Name + "; ");
  148.             }
  149.  
  150.             _cards.Clear();
  151.         }
  152.     }
  153.  
  154.     class Deck
  155.     {
  156.         private List<Card> _playingCards;
  157.         private List<Card> _droppedCards;
  158.         private Random _random;
  159.  
  160.         public Deck()
  161.         {
  162.             _playingCards = new List<Card>()
  163.             {
  164.                 new Card("6"),
  165.                 new Card("7"),
  166.                 new Card("8"),
  167.                 new Card("9"),
  168.                 new Card("10"),
  169.                 new Card("Валет"),
  170.                 new Card("Дама"),
  171.                 new Card("Король"),
  172.                 new Card("Туз"),
  173.             };
  174.  
  175.             _droppedCards = new List<Card>();
  176.             _random = new Random();
  177.  
  178.             Shuffle();
  179.         }
  180.  
  181.         public void Shuffle()
  182.         {
  183.             _playingCards.AddRange(_droppedCards);
  184.             _droppedCards.Clear();
  185.  
  186.             for (int i = _playingCards.Count - 1; i > 0; i--)
  187.             {
  188.                 int randomIndex = _random.Next(i + 1);
  189.                 Card randomCard = _playingCards[randomIndex];
  190.                 _playingCards[randomIndex] = _playingCards[i];
  191.                 _playingCards[i] = randomCard;
  192.             }
  193.         }
  194.  
  195.         public Card GetCard()
  196.         {
  197.             int cardIndex = _random.Next(0, _playingCards.Count);
  198.             Card randomCard = _playingCards[cardIndex];
  199.  
  200.             _droppedCards.Add(_playingCards[cardIndex]);
  201.  
  202.             _playingCards.RemoveAt(cardIndex);
  203.  
  204.             return randomCard;
  205.         }
  206.     }
  207.  
  208.     class Card
  209.     {
  210.         public Card(string name)
  211.         {
  212.             Name = name;
  213.         }
  214.  
  215.         public string Name { get; private set; }
  216.     }
  217. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement