Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Cards
- {
- class Program
- {
- static void Main(string[] args)
- {
- int enough = 0;
- int drawnCards;
- bool isDrawing = true;
- Player player = new Player();
- Deck deck = new Deck();
- deck.Mix();
- while (isDrawing)
- {
- Console.WriteLine($"У вас {player.GetCardsCount()} карт");
- Console.WriteLine("Сколько карт взять из колоды?");
- Console.WriteLine($"{enough} - Для завершения");
- string userInput = Console.ReadLine();
- bool isNumber = int.TryParse(userInput, out drawnCards);
- if (isNumber == true)
- {
- if (drawnCards == enough)
- {
- isDrawing = false;
- }
- else
- {
- for (int i = 0; i < drawnCards; i++)
- {
- if (deck.TryDrawCard(out Card card))
- player.TakeCard(card);
- }
- }
- }
- }
- player.ShowCards();
- }
- }
- class Card
- {
- private Suit _suit;
- private Level _level;
- public Card(Suit suit, Level level)
- {
- _suit = suit;
- _level = level;
- }
- public enum Suit
- {
- Hearts = 0,
- Spades,
- Diamonds,
- Clubs
- }
- public enum Level
- {
- Six = 0,
- Seven,
- Eight,
- Nine,
- Ten,
- Jack,
- Queen,
- King,
- Ace
- }
- public string GetName()
- {
- return string.Format($"{_level} {_suit}");
- }
- }
- class Deck
- {
- private List<Card> _cards;
- public Deck()
- {
- _cards = new List<Card>();
- var levels = Enum.GetValues(typeof(Card.Level));
- var suits = Enum.GetValues(typeof(Card.Suit));
- foreach (int level in levels)
- {
- foreach (int suit in suits)
- {
- Card.Suit newSuit = (Card.Suit)Enum.ToObject(typeof(Card.Suit), suit);
- Card.Level newLevel = (Card.Level)Enum.ToObject(typeof(Card.Level), level);
- Card card = new Card(newSuit, newLevel);
- _cards.Add(card);
- }
- }
- }
- public void Mix()
- {
- Random random = new Random();
- for (int i = 0; i < _cards.Count; i++)
- {
- int exchangedIndex = random.Next(_cards.Count);
- Card temporaryCard = _cards[i];
- _cards[i] = _cards[exchangedIndex];
- _cards[exchangedIndex] = temporaryCard;
- }
- }
- public bool TryDrawCard(out Card card)
- {
- if(_cards.Count > 0)
- {
- card = _cards.First();
- _cards.Remove(card);
- return true;
- }
- else
- {
- card = null;
- return false;
- }
- }
- }
- class Player
- {
- private List<Card> _cards;
- public Player()
- {
- _cards = new List<Card>();
- }
- public void ShowCards()
- {
- foreach (Card card in _cards)
- Console.WriteLine(card.GetName());
- }
- public int GetCardsCount()
- {
- return _cards.Count;
- }
- public void TakeCard(Card card)
- {
- _cards.Add(card);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement