Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace CSLight
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Player player = new Player();
- Table table = new Table();
- Console.WriteLine("Сколько карт дать игроку?");
- string userInput = Console.ReadLine();
- if (int.TryParse(userInput, out int count))
- {
- table.TransferCardsToPlayer(count, player);
- }
- player.ShowCards();
- Console.ReadKey();
- }
- }
- class Table
- {
- private Deck _deck = new Deck();
- public void TransferCardsToPlayer(int count, Player player)
- {
- if (count > 0)
- {
- if (count > _deck.GetCardsCount())
- {
- count = _deck.GetCardsCount();
- Console.WriteLine($"В колоде недостаточно карт. Будет выдано {count} карт");
- }
- for (int i = 0; i < count; i++)
- {
- player.AddCard(_deck.GiveCard());
- }
- }
- else
- {
- Console.WriteLine("НЕВЕРНЫЙ ВВОД");
- }
- }
- }
- class Player
- {
- private List<Card> _cards = new List<Card>();
- public void AddCard(Card card)
- {
- if (card != null)
- {
- _cards.Add(card);
- }
- }
- public void ShowCards()
- {
- Console.WriteLine("Карты на руке игрока: ");
- foreach (Card card in _cards)
- {
- Console.Write(card.Suit + " " + card.Value + " ");
- }
- }
- }
- class Deck
- {
- private Random _random;
- private Stack<Card> _cards;
- public Deck()
- {
- _cards = new Stack<Card>();
- _random = new Random();
- Generate();
- Shuffle();
- }
- public int MaxCardsCount { get; private set; }
- public int GetCardsCount()
- {
- return _cards.Count;
- }
- public Card GiveCard()
- {
- return _cards.Pop();
- }
- private void Generate()
- {
- string[] suits = { "♠", "♣", "♦", "♥" };
- string[] values = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" };
- for (int i = 0; i < suits.Length; i++)
- {
- for (int j = 0; j < values.Length; j++)
- {
- _cards.Push(new Card(suits[i], values[j]));
- }
- }
- }
- private void Shuffle()
- {
- Card[] tempArray = _cards.ToArray();
- for (int i = 0; i < tempArray.Length; i++)
- {
- Card tempCard;
- int randomIndex = _random.Next(tempArray.Length);
- tempCard = tempArray[randomIndex];
- tempArray[randomIndex] = tempArray[i];
- tempArray[i] = tempCard;
- }
- _cards = new Stack<Card>();
- foreach (Card card in tempArray)
- {
- _cards.Push(card);
- }
- }
- }
- class Card
- {
- public Card(string suit, string value)
- {
- Suit = suit;
- Value = value;
- }
- public string Suit { get; private set; }
- public string Value { get; private set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement