Advertisement
ranee

Карты

Mar 22nd, 2023
856
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.23 KB | None | 0 0
  1. using System;
  2. using System.Numerics;
  3.  
  4. namespace DeckOfCards
  5. {
  6.     class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Deck deck = new Deck();
  11.             Player player = new Player();
  12.             Casino casino = new Casino();
  13.             deck.Shuffle();
  14.             casino.Run(deck, player);
  15.  
  16.         }
  17.     }
  18.  
  19.     class Card
  20.     {
  21.         private string _suit;
  22.         private string _name;
  23.  
  24.         public Card(string suit, string name)
  25.         {
  26.             _suit = suit;
  27.             _name = name;
  28.         }
  29.  
  30.         public void ShowInfoCard()
  31.         {
  32.             Console.Write($"{_name} {_suit} ");
  33.         }
  34.     }
  35.  
  36.     class Deck
  37.     {
  38.         private List<Card> _cards;
  39.  
  40.         public Deck()
  41.         {
  42.             _cards = new List<Card>();
  43.             string[] suits = { "Черви", "Крести", "Буби", "Пики" };
  44.             string[] names = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "D", "K", "A" };
  45.  
  46.             for (int i = 0; i < suits.Length; i++)
  47.             {
  48.                 for (int j = 0; j < names.Length; j++)
  49.                 {
  50.                     _cards.Add(new Card(suits[i], names[j]));
  51.                 }
  52.             }
  53.         }
  54.  
  55.         public void Shuffle()
  56.         {
  57.             Random random = new Random();
  58.  
  59.             for (int i = 0; i < _cards.Count; i++)
  60.             {
  61.                 int randomRange = random.Next(_cards.Count);
  62.                 Card temp = _cards[randomRange];
  63.                 _cards[randomRange] = _cards[i];
  64.                 _cards[i] = temp;
  65.             }
  66.         }
  67.  
  68.         public int TakeRemainingCards()
  69.         {
  70.             return _cards.Count;
  71.         }
  72.  
  73.         public Card DrawCard()
  74.         {
  75.             Card card = _cards[0];
  76.             _cards.Remove(card);
  77.             return card;
  78.         }
  79.     }
  80.  
  81.     class Player
  82.     {
  83.         private List<Card> _cards;
  84.  
  85.         public Player()
  86.         {
  87.             _cards = new List<Card>();
  88.         }
  89.  
  90.         public void ShowCardInHand()
  91.         {
  92.             Console.WriteLine("Карты в руках:");
  93.  
  94.             foreach (Card card in _cards)
  95.             {
  96.                 card.ShowInfoCard();
  97.             }
  98.         }
  99.  
  100.         public void TakeCard(Card card)
  101.         {
  102.             _cards.Add(card);
  103.         }
  104.     }
  105.  
  106.     class Casino
  107.     {
  108.         private bool _isWork = true;
  109.         private int _minminimumNumberCards = 1;
  110.         const  int numberForExit = 99;
  111.  
  112.         public void Run(Deck deck, Player player)
  113.         {
  114.             while (_isWork)
  115.             {
  116.                 Console.Clear();
  117.  
  118.                 if (deck.TakeRemainingCards() == 0)
  119.                 {
  120.                     _isWork = false;
  121.                     Console.WriteLine("Карты кончились.");
  122.                     player.ShowCardInHand();
  123.                     Console.ReadKey();
  124.                 }
  125.                 else
  126.                 {
  127.                     Console.WriteLine($"Карт доступно: {deck.TakeRemainingCards()}. Если карт достаточно набрать {numberForExit} ");
  128.                     Console.WriteLine("Сколько карт желаете взять: ");
  129.  
  130.                     if (int.TryParse(Console.ReadLine(), out int result))
  131.                     {
  132.                         if (result >= _minminimumNumberCards && result <= deck.TakeRemainingCards())
  133.                         {
  134.  
  135.                             for (int i = 0; i < result; i++)
  136.                             {
  137.                                 Card card = deck.DrawCard();
  138.                                 player.TakeCard(card);
  139.                             }
  140.                         }
  141.                         else if(result == numberForExit)
  142.                         {
  143.                             _isWork = false;
  144.                             player.ShowCardInHand();
  145.                             Console.ReadKey();
  146.                         }
  147.                         else
  148.                         {
  149.                             Console.WriteLine("Неверный ввод.");
  150.                             Console.ReadKey();
  151.                         }
  152.                     }
  153.                 }
  154.             }
  155.         }
  156.     }
  157. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement