Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System.Net.Security;
- using System.Numerics;
- namespace deck_of_cards
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Dealer dealer = new Dealer();
- dealer.Work();
- }
- }
- class Player
- {
- private List<Card> _cards = new List<Card>();
- public void TakeCard(Card card)
- {
- if (card == null)
- {
- Console.WriteLine("Колода пуста.");
- return;
- }
- _cards.Add(card);
- }
- public void Show()
- {
- foreach (var cardItem in _cards)
- {
- Console.WriteLine(cardItem.Suit + " " + cardItem.Rank);
- }
- }
- }
- class Card
- {
- public Card(string suit, string rank)
- {
- Suit = suit;
- Rank = rank;
- }
- public string Suit { get; private set; }
- public string Rank { get; private set; }
- }
- class Deck
- {
- private List<Card> _cards = new List<Card>();
- public Deck()
- {
- Cards = _cards;
- Create();
- Shuffle();
- }
- public List<Card> Cards { get; private set; } = new List<Card>();
- private void Create()
- {
- string[] ranks = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" };
- string[] suits = { "Clubs", "Hearts", "Diamonds", "Spades" };
- for (int i = 0; i < suits.Length; i++)
- {
- for (int j = 0; j < ranks.Length; j++)
- {
- Cards.Add(new Card(suits[i], ranks[j]));
- }
- }
- }
- private void Shuffle()
- {
- Random random = new Random();
- for (int j = Cards.Count - 1; j >= 1; j--)
- {
- int numberOfRandomCard = random.Next(Cards.Count);
- Card tempCard = Cards[numberOfRandomCard];
- Cards[numberOfRandomCard] = Cards[j];
- Cards[j] = tempCard;
- }
- }
- }
- class Dealer
- {
- private Player _player = new Player();
- private Deck _deck = new Deck();
- public void Work()
- {
- bool isWorking = true;
- int numbersOfCard;
- Console.WriteLine("Карты в колоде:");
- ShowCards();
- Console.WriteLine($"\nКарты у игрока:");
- _player.Show();
- while (isWorking)
- {
- Console.WriteLine("Введите сколько карт дать игроку:");
- numbersOfCard = ReadInt();
- if (numbersOfCard <= 0 || numbersOfCard > _deck.Cards.Count)
- {
- Console.WriteLine($"Необходимо ввести положителньное число, чтобы оно не превышало {_deck.Cards.Count}.");
- Console.ReadKey();
- Console.Clear();
- }
- else
- {
- for (int i = 0; i < numbersOfCard; i++)
- {
- _player.TakeCard(GiveCard());
- }
- Console.Clear();
- Console.WriteLine("Карты в колоде:");
- ShowCards();
- Console.WriteLine($"\nКарты у игрока:");
- _player.Show();
- Console.WriteLine("До скорых встреч.");
- Console.ReadKey();
- Environment.Exit(0);
- }
- }
- }
- private Card GiveCard()
- {
- if (_deck.Cards.Count == 0)
- {
- Console.WriteLine("В колде не осталось карт.");
- return null;
- }
- Card card = _deck.Cards[_deck.Cards.Count - 1];
- _deck.Cards.Remove(card);
- return card;
- }
- private void ShowCards()
- {
- foreach (var cardItem in _deck.Cards)
- {
- Console.WriteLine($"{cardItem.Suit} {cardItem.Rank}");
- }
- }
- private int ReadInt()
- {
- bool isWorking = true;
- int numberOfCards = 0;
- while (isWorking)
- {
- string userInput = Console.ReadLine();
- bool isNumber = int.TryParse(userInput, out numberOfCards);
- if (isNumber == false)
- {
- Console.Write("Введите число:");
- }
- else
- {
- isWorking = false;
- }
- }
- return numberOfCards;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement