Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class Program
- {
- static void Main(string[] args)
- {
- Game game = new Game();
- game.Start();
- Console.ReadKey();
- }
- }
- class Game
- {
- private static Random _random = new Random();
- private CardDeck _deck = new CardDeck(_random);
- private Player _player = new Player();
- private bool _isLimited = true;
- private int _limit = 5;
- public void Start()
- {
- int count = 0;
- bool isStoped = false;
- ConsoleKeyInfo keyInfo;
- do
- {
- Console.WriteLine("Choose card index: ");
- GetCardByIndex();
- count++;
- Console.WriteLine("Input |e| for show cards <> any key exept |e| to continue: ");
- keyInfo = Console.ReadKey();
- if (keyInfo.Key == ConsoleKey.E)
- {
- isStoped = true;
- }
- Console.Clear();
- }
- while (_isLimited && count < _limit && isStoped == false);
- _player.ShowCards();
- }
- private void GetCardByIndex()
- {
- ShowDeck();
- bool isNumber;
- int index;
- do
- {
- Console.WriteLine("Input card index: ");
- string userInput = Console.ReadLine();
- isNumber = int.TryParse(userInput, out index);
- }
- while (isNumber == false);
- if (_deck.TryRemoveCard(index, out Card card))
- {
- _player.TakeCard(card);
- }
- else
- {
- Console.WriteLine("Invalid index");
- }
- }
- private void ShowDeck()
- {
- _deck.ShowCardsList();
- }
- }
- class Player
- {
- private List<Card> _cards = new List<Card>();
- public void TakeCard(Card card)
- {
- _cards.Add(card);
- }
- public void ShowCards()
- {
- foreach (var card in _cards)
- {
- Console.WriteLine($"{card.Name}");
- }
- }
- }
- class Card
- {
- public string Name { get; private set; }
- public Card(string name)
- {
- Name = name;
- }
- }
- class CardDeck
- {
- private string[] _cardsName = { "a", "b", "c", "d", "e", "f", "g", "x", "y", "w"};
- private Dictionary<int, Card> _cardDeck = new Dictionary<int, Card>();
- private int _capacity = 15;
- private Random _random;
- public int Capacity => _capacity;
- public CardDeck(Random random)
- {
- _random = random;
- Fill();
- CheckCorrect();
- }
- public void ShowCardsInfo()
- {
- foreach (var card in _cardDeck)
- {
- Console.WriteLine($"{card.Key}. {card.Value.Name}");
- }
- }
- public void ShowCardsList()
- {
- foreach (var card in _cardDeck)
- {
- Console.Write($"{card.Key}, ");
- }
- Console.WriteLine();
- }
- public bool TryRemoveCard(int key, out Card card)
- {
- if (_cardDeck.ContainsKey(key))
- {
- card = _cardDeck[key];
- _cardDeck.Remove(key);
- return true;
- }
- card = null;
- return false;
- }
- private void CheckCorrect()
- {
- int count = 0;
- for (int i = 1; i < _capacity; i++)
- {
- if (_cardDeck[0] == _cardDeck[i])
- {
- count++;
- }
- }
- if (count == _capacity - 1)
- {
- Fill();
- }
- }
- private void Fill()
- {
- for (int i = 0; i < _capacity; i++)
- {
- _cardDeck.Add(i, new Card(GetRandomCardName(_random)));
- }
- }
- private string GetRandomCardName(Random random)
- {
- int index = random.Next(_cardsName.Length);
- return _cardsName[index];
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement