Advertisement
Askor

NewHw40

Apr 15th, 2022 (edited)
811
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.81 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class Program
  5. {
  6.     static void Main(string[] args)
  7.     {
  8.         Game game = new Game();
  9.         game.Start();
  10.         Console.ReadKey();
  11.     }
  12. }
  13.  
  14. class Game
  15. {
  16.     private static Random _random = new Random();
  17.  
  18.     private CardDeck _deck = new CardDeck(_random);
  19.     private Player _player = new Player();
  20.     private bool _isLimited = true;
  21.     private int _limit = 5;
  22.  
  23.     public void Start()
  24.     {
  25.         int count = 0;
  26.         bool isStoped = false;
  27.         ConsoleKeyInfo keyInfo;
  28.  
  29.         do
  30.         {
  31.             Console.WriteLine("Choose card index: ");
  32.             GetCardByIndex();
  33.             count++;
  34.  
  35.             Console.WriteLine("Input |e| for show cards <> any key exept |e| to continue: ");
  36.             keyInfo = Console.ReadKey();
  37.  
  38.             if (keyInfo.Key == ConsoleKey.E)
  39.             {
  40.                 isStoped = true;
  41.             }
  42.  
  43.             Console.Clear();
  44.         }
  45.         while (_isLimited && count < _limit && isStoped == false);
  46.  
  47.         _player.ShowCards();
  48.     }
  49.  
  50.     private void GetCardByIndex()
  51.     {
  52.         ShowDeck();
  53.  
  54.         bool isNumber;
  55.         int index;
  56.  
  57.         do
  58.         {
  59.             Console.WriteLine("Input card index: ");
  60.             string userInput = Console.ReadLine();
  61.  
  62.             isNumber = int.TryParse(userInput, out index);
  63.         }
  64.         while (isNumber == false);
  65.  
  66.         if (_deck.TryRemoveCard(index, out Card card))
  67.         {
  68.             _player.TakeCard(card);
  69.         }
  70.         else
  71.         {
  72.             Console.WriteLine("Invalid index");
  73.         }
  74.  
  75.     }
  76.  
  77.     private void ShowDeck()
  78.     {
  79.         _deck.ShowCardsList();
  80.     }
  81. }
  82.  
  83. class Player
  84. {
  85.     private List<Card> _cards = new List<Card>();
  86.  
  87.     public void TakeCard(Card card)
  88.     {
  89.         _cards.Add(card);
  90.     }
  91.  
  92.     public void ShowCards()
  93.     {
  94.         foreach (var card in _cards)
  95.         {
  96.             Console.WriteLine($"{card.Name}");
  97.         }
  98.     }
  99. }
  100.  
  101. class Card
  102. {
  103.     public string Name { get; private set; }
  104.  
  105.     public Card(string name)
  106.     {
  107.         Name = name;
  108.     }
  109. }
  110.  
  111. class CardDeck
  112. {
  113.     private string[] _cardsName = { "a", "b", "c", "d", "e", "f", "g", "x", "y", "w"};
  114.  
  115.     private Dictionary<int, Card> _cardDeck = new Dictionary<int, Card>();
  116.     private int _capacity = 15;
  117.     private Random _random;
  118.  
  119.     public int Capacity => _capacity;
  120.  
  121.     public CardDeck(Random random)
  122.     {
  123.         _random = random;
  124.         Fill();
  125.         CheckCorrect();
  126.     }
  127.  
  128.     public void ShowCardsInfo()
  129.     {
  130.         foreach (var card in _cardDeck)
  131.         {
  132.             Console.WriteLine($"{card.Key}. {card.Value.Name}");
  133.         }
  134.     }
  135.  
  136.     public void ShowCardsList()
  137.     {
  138.         foreach (var card in _cardDeck)
  139.         {
  140.             Console.Write($"{card.Key}, ");
  141.         }
  142.  
  143.         Console.WriteLine();
  144.     }
  145.  
  146.     public bool TryRemoveCard(int key, out Card card)
  147.     {
  148.         if (_cardDeck.ContainsKey(key))
  149.         {
  150.             card = _cardDeck[key];
  151.             _cardDeck.Remove(key);
  152.  
  153.             return true;
  154.         }
  155.  
  156.         card = null;
  157.         return false;
  158.     }
  159.  
  160.     private void CheckCorrect()
  161.     {
  162.         int count = 0;
  163.  
  164.         for (int i = 1; i < _capacity; i++)
  165.         {
  166.             if (_cardDeck[0] == _cardDeck[i])
  167.             {
  168.                 count++;
  169.             }
  170.         }
  171.  
  172.         if (count == _capacity - 1)
  173.         {
  174.             Fill();
  175.         }
  176.     }
  177.  
  178.     private void Fill()
  179.     {
  180.         for (int i = 0; i < _capacity; i++)
  181.         {
  182.             _cardDeck.Add(i, new Card(GetRandomCardName(_random)));
  183.         }
  184.     }
  185.  
  186.     private string GetRandomCardName(Random random)
  187.     {
  188.         int index = random.Next(_cardsName.Length);
  189.  
  190.         return _cardsName[index];
  191.     }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement