Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace ConsoleApp1
- {
- class Program
- {
- static void Main(string[] args)
- {
- const string CommandAddCards = "1";
- const string CommandShowCards = "2";
- const string CommandExit = "3";
- Deck deck = new Deck();
- Dealer dealer = new Dealer(deck);
- Player player = new Player("Иван");
- bool isWork = true;
- string userInput;
- while (isWork)
- {
- Console.WriteLine($"{CommandAddCards} - Взять карту.\n" +
- $"{CommandShowCards} - Показать карты.\n" +
- $"{CommandExit} - Выход.");
- Console.WriteLine("Введите команду...");
- userInput = Console.ReadLine();
- Console.Clear();
- switch (userInput)
- {
- case CommandAddCards:
- dealer.GiveCard(player);
- break;
- case CommandShowCards:
- player.ShowCards();
- break;
- case CommandExit:
- isWork = false;
- break;
- default:
- Console.WriteLine("Нет такой команды!!");
- break;
- }
- }
- }
- }
- class Card
- {
- public Card(char suit, string name)
- {
- Suit = suit;
- Name = name;
- }
- public char Suit { get; private set; }
- public string Name { get; private set; }
- }
- class Deck
- {
- private List<Card> _cards;
- public Deck()
- {
- _cards = CreateCards();
- }
- public bool TryGiveCard(out Card card)
- {
- card = null;
- if (_cards.Count != 0)
- {
- card = _cards[0];
- _cards.Remove(card);
- return true;
- }
- return false;
- }
- private List<Card> CreateCards()
- {
- List<Card> cards = new List<Card>();
- string[] names = new string[] { "6", "7", "8", "9", "10", "J", "Q", "K", "T" };
- char[] suit = new char[] { '♠', '♥', '♦', '♣' };
- for (int i = 0; i < names.Length; i++)
- {
- for (int j = 0; j < suit.Length; j++)
- {
- cards.Add(new Card(suit[j], names[i]));
- }
- }
- Shuffle(cards);
- return cards;
- }
- private void Shuffle(List<Card> cards)
- {
- Random random = new Random();
- int maxNumber = cards.Count;
- int minNumber = 0;
- for (int i = 0; i < cards.Count; i++)
- {
- int randomNumber = random.Next(minNumber, maxNumber);
- var buffer = cards[i];
- cards[i] = cards[randomNumber];
- cards[randomNumber] = buffer;
- }
- }
- }
- class Player
- {
- private List<Card> _cards;
- public Player(string name)
- {
- Name = name;
- _cards = new List<Card>();
- }
- public string Name { get; private set; }
- public void ShowCards()
- {
- for (int i = 0; i < _cards.Count; i++)
- {
- Console.WriteLine(i + " " + _cards[i].Name + " " + _cards[i].Suit);
- }
- }
- public void AddCard(Card card)
- {
- _cards.Add(card);
- }
- }
- class Dealer
- {
- private Deck _deck;
- public Dealer(Deck deck)
- {
- _deck = deck;
- }
- public void GiveCard(Player player)
- {
- Console.WriteLine($"Сколько карт {player.Name.ToUpper()} возьмете!");
- if (int.TryParse(Console.ReadLine(), out int countCard))
- {
- for (int i = 0; i < countCard; i++)
- {
- if (_deck.TryGiveCard(out Card card))
- {
- player.AddCard(card);
- }
- else
- {
- Console.WriteLine("Карты закончились!!");
- break;
- }
- }
- }
- else
- {
- Console.WriteLine("Нет такого числа!");
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement