lovelyvook

Unit_40

Jul 15th, 2024 (edited)
372
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.71 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace Ijunior
  5. {
  6.     internal class Program
  7.     {
  8.         static void Main(string[] args)
  9.         {
  10.             Game game = new Game();
  11.             game.Play();
  12.         }
  13.     }
  14.  
  15.     class Game
  16.     {
  17.         private Player _player;
  18.         private Deck _deck;
  19.  
  20.         public Game()
  21.         {
  22.             _player = new Player();
  23.             _deck = new Deck();
  24.         }
  25.  
  26.         public void Play()
  27.         {
  28.             const string CommandExit = "exit";
  29.  
  30.             bool isWork = true;
  31.             string userInput;
  32.  
  33.             while (isWork)
  34.             {
  35.                 _player.ShowCards();
  36.  
  37.                 if (_deck.Lenght > 0)
  38.                 {
  39.                     Console.Write($"\n\nВ колоде {_deck.Lenght} карт" +
  40.                                   $"\nСколько карт вы хотите добрать (введите {CommandExit} для выхода)? ");
  41.                     userInput = Console.ReadLine();
  42.  
  43.                     if (int.TryParse(userInput, out int countCards))
  44.                     {
  45.                         if (_deck.TryGetCard(countCards, out List<Card> cards))
  46.                             _player.PutInHand(cards);
  47.                     }
  48.                     else if (userInput.ToLower() == CommandExit)
  49.                     {
  50.                         isWork = false;
  51.                     }
  52.                     else
  53.                     {
  54.                         Console.Write("Некорректный ввод");
  55.                         Console.ReadKey();
  56.                     }
  57.                 }
  58.                 else
  59.                 {
  60.                     isWork = false;
  61.                     Console.WriteLine("\n\nИгра закончена");
  62.                     Console.ReadKey();
  63.                 }
  64.  
  65.                 Console.Clear();
  66.             }
  67.         }
  68.     }
  69.  
  70.     class Player
  71.     {
  72.         private List<Card> _cards = new List<Card>();
  73.  
  74.         public void PutInHand(List<Card> cards)
  75.         {
  76.             if (cards != null)
  77.                 _cards.AddRange(cards);
  78.         }
  79.  
  80.         public void ShowCards()
  81.         {
  82.             if (_cards.Count > 0)
  83.             {
  84.                 Console.WriteLine("Карты на руке:");
  85.  
  86.                 foreach (var card in _cards)
  87.                 {
  88.                     card.ShowInfo();
  89.                 }
  90.             }
  91.             else
  92.             {
  93.                 Console.Write("У игрока пока нет карт");
  94.             }
  95.         }
  96.     }
  97.  
  98.     class Deck
  99.     {
  100.         private List<Card> _deck = new List<Card>();
  101.  
  102.         public Deck()
  103.         {
  104.             Fill();
  105.             Shuffle();
  106.         }
  107.  
  108.         public int Lenght => _deck.Count;
  109.  
  110.         public bool TryGetCard(int countCards, out List<Card> cards)
  111.         {
  112.             cards = new List<Card>();
  113.  
  114.             if (_deck.Count >= countCards)
  115.             {
  116.                 for (int i = 0; i < countCards; i++)
  117.                 {
  118.                     cards.Add(_deck[0]);
  119.                     _deck.RemoveAt(0);
  120.                 }
  121.  
  122.                 return true;
  123.             }
  124.             else
  125.             {
  126.                 Console.Write("В колоде недостаточно карт");
  127.                 Console.ReadKey();
  128.             }
  129.  
  130.             return false;
  131.         }
  132.  
  133.         private void Fill()
  134.         {
  135.             string[] values = new string[] { "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" };
  136.             char[] suits = new char[] { '♣', '♥', '♦', '♠' };
  137.  
  138.             foreach (char suit in suits)
  139.             {
  140.                 foreach (string value in values)
  141.                 {
  142.                     _deck.Add(new Card(value, suit));
  143.                 }
  144.             }
  145.         }
  146.  
  147.         private void Shuffle()
  148.         {
  149.             int randomIndex;
  150.             Card tempCard;
  151.  
  152.             for (int i = 0; i < _deck.Count; i++)
  153.             {
  154.                 tempCard = _deck[i];
  155.                 randomIndex = Utils.GetRandomNumber(_deck.Count);
  156.                 _deck[i] = _deck[randomIndex];
  157.                 _deck[randomIndex] = tempCard;
  158.             }
  159.         }
  160.     }
  161.  
  162.     class Card
  163.     {
  164.         private string _value;
  165.         private char _suit;
  166.  
  167.         public Card(string value, char suit)
  168.         {
  169.             _value = value;
  170.             _suit = suit;
  171.         }
  172.  
  173.         public void ShowInfo()
  174.         {
  175.             Console.Write(_value + _suit + " ");
  176.         }
  177.     }
  178.  
  179.     class Utils
  180.     {
  181.         private static Random s_random = new Random();
  182.  
  183.         public static int GetRandomNumber(int number)
  184.         {
  185.             return s_random.Next(number);
  186.         }
  187.     }
  188. }
Advertisement
Add Comment
Please, Sign In to add comment