RedFlys

Home work - card game

Mar 30th, 2022 (edited)
723
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.26 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 Home_Work
  8. {
  9.     class Program
  10.     {
  11.         static void Main()
  12.         {
  13.             CardGame cardGame = new CardGame();
  14.             cardGame.Play();
  15.         }
  16.     }
  17.  
  18.     class CardGame
  19.     {
  20.         private Player _player;
  21.         private CardDeck _cardDeck;
  22.  
  23.         public void Play()
  24.         {
  25.             bool isPlaying = true;
  26.  
  27.             Console.WriteLine("Приветствуем тебя. " +
  28.                 "\nТебе нужно брать карты, пока они не закончаться или ты не захочешь выйти.");
  29.  
  30.             while (isPlaying)
  31.             {
  32.                 int deckSize = ChooseDeckSize();
  33.                 _cardDeck = new CardDeck(deckSize);
  34.                 _player = new Player();
  35.  
  36.                 TakeCards();
  37.                 _player.ShowCards();
  38.  
  39.                 isPlaying = IsExit() == false;
  40.  
  41.                 Console.Clear();
  42.             }
  43.         }
  44.  
  45.         private bool IsExit()
  46.         {
  47.             Console.WriteLine("Нажмите Esc для выхода. Нажмите любую клавишу для того, что бы начать заново.");
  48.  
  49.             return (Console.ReadKey().Key == ConsoleKey.Escape);
  50.         }
  51.  
  52.         private void TakeCards()
  53.         {
  54.             bool isContinueTake = true;
  55.  
  56.             while (isContinueTake)
  57.             {
  58.                 Console.WriteLine("Что бы взять карту нажмите 1, что бы выйти, нажмите 2.");
  59.  
  60.                 switch (Console.ReadLine())
  61.                 {
  62.                     case "1":
  63.                         _player.AddCard(_cardDeck.GetRandomCard());
  64.                         break;
  65.  
  66.                     case "2":
  67.                         isContinueTake = false;
  68.                         break;
  69.                 }
  70.  
  71.                 if (_cardDeck.IsEmpty == true)
  72.                 {
  73.                     isContinueTake = false;
  74.                     Console.WriteLine("Карты в колоде закончились.");
  75.                 }
  76.  
  77.                 Console.Clear();
  78.             }
  79.         }
  80.  
  81.         private int ChooseDeckSize()
  82.         {
  83.             int sizeSmallDeck = Enum.GetNames(typeof(CardSuit)).Length * (int)CardValue.Six;
  84.             int sizeBigDeck = Enum.GetNames(typeof(CardSuit)).Length * (int)CardValue.Two;
  85.             bool isChoosed = false;
  86.  
  87.             while (isChoosed == false)
  88.             {
  89.                 Console.WriteLine($"Выберите размер колоды:" +
  90.                     $"\n1. {sizeSmallDeck} карт." +
  91.                     $"\n2. {sizeBigDeck} карты.");
  92.  
  93.                 switch (Console.ReadLine())
  94.                 {
  95.                     case "1":
  96.                         return sizeSmallDeck;
  97.  
  98.                     case "2":
  99.                         return sizeSmallDeck;
  100.                 }
  101.  
  102.                 Console.Clear();
  103.             }
  104.  
  105.             return 0;
  106.         }
  107.     }
  108.  
  109.     class Player
  110.     {
  111.         private List<Card> _cards;
  112.  
  113.         public Player()
  114.         {
  115.             _cards = new List<Card>();
  116.         }
  117.  
  118.         public void AddCard(Card card)
  119.         {
  120.             _cards.Add(card);
  121.  
  122.             Console.WriteLine("Вы взяли карту.");
  123.         }
  124.  
  125.         public void ShowCards()
  126.         {
  127.             Console.WriteLine("У вас следующие карты:");
  128.  
  129.             foreach (Card card in _cards)
  130.             {
  131.                 card.ShowInfo();
  132.             }
  133.         }
  134.     }
  135.  
  136.     class CardDeck
  137.     {
  138.         private List<Card> _cards;
  139.  
  140.         public CardDeck(int countCards)
  141.         {
  142.             _cards = new List<Card>();
  143.             Create(countCards);
  144.         }
  145.  
  146.         public int CountCards => _cards.Count;
  147.         public bool IsEmpty => CountCards < 1;
  148.  
  149.         public Card GetRandomCard()
  150.         {
  151.             Random random = new Random();
  152.  
  153.             Card card = _cards[random.Next(CountCards)];
  154.             _cards.Remove(card);
  155.  
  156.             return card;
  157.         }
  158.  
  159.         private void Create(int countCards)
  160.         {
  161.             int countSuit = Enum.GetNames(typeof(CardSuit)).Length;
  162.             int countCardsInEverySuit = countCards / countSuit;
  163.  
  164.             for (int i = 1; i <= countCardsInEverySuit; i++)
  165.             {
  166.                 for (int j = 1; j <= countSuit; j++)
  167.                 {
  168.                     _cards.Add(new Card((CardSuit)j, (CardValue)i));
  169.                 }
  170.             }
  171.         }
  172.     }
  173.  
  174.     class Card
  175.     {
  176.         private CardSuit _suit;
  177.         private CardValue _value;
  178.  
  179.         public Card(CardSuit suit, CardValue value)
  180.         {
  181.             _suit = suit;
  182.             _value = value;
  183.         }
  184.  
  185.         public void ShowInfo()
  186.         {
  187.             Console.WriteLine(_value + " " + _suit);
  188.         }
  189.     }
  190.  
  191.     enum CardValue
  192.     {
  193.         Ace = 1,
  194.         King,
  195.         Queen,
  196.         Jack,
  197.         Ten,
  198.         Nine,
  199.         Eight,
  200.         Seven,
  201.         Six,
  202.         Five,
  203.         Four,
  204.         Three,
  205.         Two
  206.     }
  207.  
  208.     enum CardSuit
  209.     {
  210.         Hearts = 1,
  211.         Spades,
  212.         Diamonds,
  213.         Clubs
  214.     }
  215. }
Advertisement
Add Comment
Please, Sign In to add comment