Zlobin2021

Cards

Mar 16th, 2022 (edited)
641
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.64 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace cards
  5. {
  6.     internal class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Player player = new Player();
  11.             bool isWork = true;
  12.  
  13.             player.AddCards();
  14.  
  15.             while (isWork)
  16.             {
  17.                 Console.WriteLine("1.Взять карту.\n2.Выдать результат.\n3.Вывести колоду.\n4.Выйти\n");
  18.  
  19.                 switch (Console.ReadLine())
  20.                 {
  21.                     case "1":
  22.                         player.TakeCard();
  23.                         break;
  24.                     case "2":
  25.                         player.ShowHand();
  26.                         break;
  27.                     case "3":
  28.                         player.ShowDeck();
  29.                         break;
  30.                     case "4":
  31.                         isWork = false;
  32.                         break;
  33.                     default:
  34.                         Console.WriteLine("Ошибка!!!");
  35.                         break;
  36.                 }
  37.  
  38.                 Console.ReadLine();
  39.                 Console.Clear();
  40.             }
  41.         }
  42.     }
  43.  
  44.     class Player
  45.     {
  46.         private List<Card> _playerHand = new List<Card>();
  47.         private Deck _deck = new Deck();
  48.         private Random _random = new Random();
  49.  
  50.         public void AddCards()
  51.         {
  52.             _deck = new Deck();
  53.         }
  54.  
  55.         public void ShowDeck()
  56.         {
  57.             _deck.ShowInfo();
  58.         }
  59.  
  60.         public void ShowHand()
  61.         {
  62.             for (int i = 0; i < _playerHand.Count; i++)
  63.             {
  64.                 Console.WriteLine($"Номер карты: {_playerHand[i].Number}. Масть: {_playerHand[i].Suit}.");
  65.             }
  66.         }
  67.  
  68.         public void TakeCard()
  69.         {
  70.             int random = _random.Next(0, _deck.ReturnQuantity());
  71.             _playerHand.Add(new Card(_deck.ReturnCardNumber(random), _deck.ReturnCardBack(random)));
  72.             _deck.RemoveCard(random);
  73.         }
  74.     }
  75.  
  76.     class Deck
  77.     {
  78.         private List<Card> _cards = new List<Card>();
  79.         public int Quantity { get; private set; }
  80.  
  81.         public Deck()
  82.         {
  83.             Quantity = _cards.Count;
  84.             CreateCards();
  85.         }
  86.  
  87.         private void CreateCards()
  88.         {
  89.             int cardNumber = 10;
  90.  
  91.             List<string> suit = new List<string>();
  92.             suit.AddRange(new string[] { "Черви", "Пики", "Буби", "Крести" });
  93.  
  94.             for (int i = 0; i < suit.Count; i++)
  95.             {
  96.                 for (int j = 1; j < cardNumber; j++)
  97.                 {
  98.                     _cards.Add(new Card(j, suit[i]));
  99.                 }
  100.             }
  101.         }
  102.  
  103.         public void ShowInfo()
  104.         {
  105.             for (int i = 0; i < _cards.Count; i++)
  106.             {
  107.                 Console.WriteLine($"{i} {_cards[i].Number} {_cards[i].Suit}");
  108.             }
  109.         }
  110.  
  111.         public int ReturnCardNumber(int number)
  112.         {
  113.             return _cards[number].Number;
  114.         }
  115.  
  116.         public string ReturnCardBack(int back)
  117.         {
  118.             return _cards[back].Suit;
  119.  
  120.         }
  121.  
  122.         public void RemoveCard(int index)
  123.         {
  124.             _cards.RemoveAt(index);
  125.         }
  126.  
  127.         public int ReturnQuantity()
  128.         {
  129.             return _cards.Count;
  130.         }
  131.     }
  132.  
  133.     class Card
  134.     {
  135.         public int Number { get; private set; }
  136.         public string Suit { get; private set; }
  137.  
  138.         public Card(int number, string suit)
  139.         {
  140.             Number = number;
  141.             Suit = suit;
  142.         }
  143.     }
  144. }
Add Comment
Please, Sign In to add comment