Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.ComponentModel;
- using System.Globalization;
- namespace CSLight
- {
- class Program
- {
- static void Main(string[] args)
- {
- GameDeck deck = new GameDeck();
- deck.InitializeDeck();
- GamingTable gamingTable = new GamingTable(deck);
- gamingTable.GiveCards();
- gamingTable.ShowPlayerCards();
- }
- }
- class GamingTable
- {
- private GameDeck _gameDeck;
- private Player _player = new Player();
- private const int MaxCardsInDeck = 24;
- private bool _isInputValid = true;
- private int _cardsToDistribute = 0;
- public GamingTable(GameDeck gameDeck)
- {
- _gameDeck = gameDeck;
- }
- public void GiveCards()
- {
- while (_isInputValid)
- {
- Console.WriteLine("Сколько карт вы хотите получить?(в колоде 24 карты)");
- _cardsToDistribute = ReadInt();
- if (_cardsToDistribute > MaxCardsInDeck)
- {
- Console.WriteLine("Вы ввели больше 24 карт. Попробуйте еще раз.");
- }
- else if (_cardsToDistribute <= 0)
- {
- Console.WriteLine("Введите положительное число карт.");
- }
- else
- {
- _isInputValid = false;
- }
- }
- for (int i = 0; i < _cardsToDistribute; i++)
- {
- if (_gameDeck.TryGetCard(out Card card))
- {
- _player.TakeCards(card);
- }
- }
- }
- public void ShowPlayerCards()
- {
- _player.ShowCards();
- }
- private int ReadInt()
- {
- int result;
- while (!int.TryParse(Console.ReadLine(), out result))
- {
- Console.WriteLine("Ошибка.Попробуйте еще раз:");
- }
- return result;
- }
- }
- class Player
- {
- private List<Card> _cards = new List<Card>();
- public void TakeCards(Card card)
- {
- _cards.Add(card);
- }
- public void ShowCards()
- {
- if (_cards.Count == 0)
- {
- Console.WriteLine("У игрока нет карт");
- return;
- }
- foreach (var card in _cards)
- {
- card.ShowInfo();
- }
- }
- }
- class GameDeck
- {
- private List<Card> _cards = new List<Card>(24);
- private Random _random = new Random();
- public void InitializeDeck()
- {
- string[] suits = { "Черви", "Бубны", "Крести", "Пики" };
- string[] meanings = {"9", "10", "Валет", "Дама", "Король", "Туз" };
- foreach (string suit in suits)
- {
- foreach (string meaning in meanings)
- {
- _cards.Add(new Card(suit, meaning));
- }
- }
- Shuffle();
- }
- private void Shuffle()
- {
- for (int i = _cards.Count - 1; i >= 1; i--)
- {
- int j = _random.Next(i + 1);
- Card temp = _cards[j];
- _cards[j] = _cards[i];
- _cards[i] = temp;
- }
- }
- public bool TryGetCard(out Card card)
- {
- if (_cards.Count > 0)
- {
- card = _cards[0];
- _cards.RemoveAt(0);
- return true;
- }
- else
- {
- card = null;
- return false;
- }
- }
- }
- class Card
- {
- public Card(string suit, string meaning)
- {
- _suit = suit;
- _meaning = meaning;
- }
- public string _suit { get; private set; }
- public string _meaning { get; private set; }
- public void ShowInfo()
- {
- Console.WriteLine($"Карта {_meaning} | {_suit }");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement