Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace CSLight // Note: actual namespace depends on the project name.
- {
- public class Program
- {
- private static void Main(string[] args)
- {
- Pack pack = new Pack();
- Player player = new Player(pack);
- bool isWorks = true;
- while (isWorks)
- {
- Console.WriteLine("1. Взять еще одну карту\n" +
- "2. Взять еще какое-то количество карт\n" +
- "3. Посмотреть, сколько карт осталось в колоде\n" +
- "4. Посмотреть, какие карты в руке\n" +
- "5. Выход\n");
- int inputCommand = InputToCorrectness("Введите номер команды: ",
- "Некорректная команда, попробуйте еще раз\n");
- switch (inputCommand)
- {
- case 1:
- if(player.TryGetCard() == false)
- Console.WriteLine("Карт больше нет");
- break;
- case 2:
- int countCards = InputToCorrectness("Сколько карт вы хотите взять?: ",
- "Некорректное число, попробуйте еще раз\n");
- if(player.TryGetCards(countCards) == false)
- Console.WriteLine("Невозможно взять столько карт");
- break;
- case 3:
- Console.WriteLine($"В колоде осталось {pack.Count} карт");
- break;
- case 4:
- player.ShowCards(Console.WriteLine);
- break;
- case 5:
- isWorks = false;
- break;
- default:
- Console.WriteLine("Такой команды не существует");
- break;
- }
- Console.Write("\nНажмите любую клавишу...");
- Console.ReadKey();
- Console.Clear();
- }
- }
- private static int InputToCorrectness(string output, string error)
- {
- int inputNumber = 0;
- bool isCorrectInput = true;
- do
- {
- isCorrectInput = TryInputInt(out inputNumber, output);
- if (!isCorrectInput)
- Console.WriteLine(error);
- } while (!isCorrectInput);
- return inputNumber;
- }
- private static bool TryInputInt(out int inputInt, string output = "")
- {
- Console.Write(output);
- string input = Console.ReadLine() ?? "";
- return int.TryParse(input, out inputInt);
- }
- }
- }
- public class Player
- {
- private List<Card> _cards;
- private Pack _pack;
- public Player(Pack pack)
- {
- _cards = new List<Card>();
- _pack = pack;
- }
- public bool TryGetCards(int count)
- {
- if (_pack.Count < count)
- return false;
- for (int i = 0; i < count; i++)
- {
- TryGetCard();
- }
- return true;
- }
- public bool TryGetCard()
- {
- bool canGet = _pack.TryGet(out Card card);
- if (canGet)
- _cards.Add(card);
- return canGet;
- }
- public void ShowCards(Action<string> showMethod)
- {
- foreach (Card card in _cards)
- {
- showMethod?.Invoke($"{card.Rank} - {card.Suit}");
- }
- }
- }
- public class Pack
- {
- private Stack<Card> _cards;
- public int Count => _cards.Count;
- public bool TryGet(out Card card)
- {
- return _cards.TryPop(out card);
- }
- public Pack()
- {
- Card[] cards = GetAllCards();
- Shuffle(cards);
- SetCards(cards);
- }
- private void SetCards(Card[] cards)
- {
- _cards = new Stack<Card>(cards);
- }
- private void Shuffle<T>(IList<T> array)
- {
- Random random = new Random();
- for (int i = 0; i < array.Count; i++)
- {
- int randomIndex = random.Next(0, array.Count);
- (array[randomIndex], array[i]) = (array[i], array[randomIndex]);
- }
- }
- private Card[] GetAllCards()
- {
- var ranks = Enum.GetNames(typeof(Rank));
- var suits = Enum.GetNames(typeof(Suit));
- Card[] cards = new Card[ranks.Length * suits.Length];
- for (int i = 0; i < ranks.Length; i++)
- {
- for (int j = 0; j < suits.Length; j++)
- {
- int cardIndex = i * suits.Length + j;
- cards[cardIndex] = new Card((Rank) i, (Suit) j);
- }
- }
- return cards;
- }
- }
- public class Card
- {
- public Rank Rank { get; private set; }
- public Suit Suit { get; private set; }
- public Card(Rank rank, Suit suit)
- {
- Rank = rank;
- Suit = suit;
- }
- }
- public enum Rank
- {
- Two,
- Three,
- Four,
- Five,
- Six,
- Seven,
- Eight,
- Nine,
- Ten,
- Jack,
- Queen,
- King,
- Ace
- }
- public enum Suit
- {
- Clubs,
- Diamonds,
- Hearts,
- Spades
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement