lightb01

Колода карт

Jul 13th, 2022 (edited)
287
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.65 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace CSTemp
  8. {
  9.     internal class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             int cardsInDeck = 36;
  14.             Player player = new Player();
  15.             Deck superheroDeck = new Deck(cardsInDeck);
  16.             player.TakeCards(superheroDeck);
  17.             Console.ReadKey();
  18.         }
  19.     }
  20.  
  21.     enum Superheroes
  22.     {
  23.         Batman,
  24.         Spiderman,
  25.         Thor,
  26.         Hulk,
  27.         Wolverine,
  28.         Ironman
  29.     }
  30.  
  31.     class Player
  32.     {
  33.         private List<Card> _takenCards = new List<Card>();
  34.  
  35.         public void TakeCards(Deck deck)
  36.         {
  37.             int amount = 0;
  38.             bool correct = false;
  39.  
  40.             while (correct == false)
  41.             {
  42.                 Console.Write("Сколько карт вы хотите взять: ");
  43.                 string userInput = Console.ReadLine();
  44.  
  45.                 if (int.TryParse(userInput, out int result))
  46.                 {
  47.                     if (result <= deck.GetLenght())
  48.                     {
  49.                         amount = result;
  50.                         correct = true;
  51.                     }
  52.                     else
  53.                     {
  54.                         Console.WriteLine("В колоде меньше карт, введите другое число.");
  55.                     }
  56.                 }
  57.                 else
  58.                 {
  59.                     Console.WriteLine("Вы ввели не число.");
  60.                 }
  61.             }
  62.  
  63.             for (int i = 0; i < amount; i++)
  64.             {
  65.                 _takenCards.Add(deck.Pop());
  66.             }
  67.  
  68.             Console.WriteLine("Вы взяли:");
  69.  
  70.             foreach (var card in _takenCards)
  71.             {
  72.                 Console.WriteLine(card.Superhero);
  73.             }
  74.         }
  75.     }
  76.  
  77.     class Deck
  78.     {
  79.         private Stack<Card> _cards = new Stack<Card>();
  80.         private Random _random = new Random();
  81.  
  82.         public Deck(int number)
  83.         {
  84.             for (int i = 0; i < number; i++)
  85.             {
  86.                 _cards.Push(new Card(_random.Next(0, Enum.GetNames(typeof(Superheroes)).Length)));
  87.             }
  88.         }
  89.  
  90.         public int GetLenght()
  91.         {
  92.             return _cards.Count;
  93.         }
  94.  
  95.         public Card Pop()
  96.         {
  97.             return _cards.Pop();
  98.         }
  99.     }
  100.  
  101.     class Card
  102.     {
  103.         public Superheroes Superhero { get; private set; }
  104.  
  105.         public Card(int number)
  106.         {
  107.             Superhero = (Superheroes)number;
  108.         }
  109.     }
  110. }
Add Comment
Please, Sign In to add comment