Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections;
- using System.Collections.Generic;
- using System.Collections.ObjectModel;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using System.IO;
- namespace Основы.Практика
- {
- class Program
- {
- static void Main(string[] args)
- {
- Menu menu = new Menu();
- menu.Run();
- }
- }
- class Menu
- {
- private const string ExitCommand = "0";
- private const string RequestCardCommand = "1";
- private const string RequestToShowHandCommand = "2";
- private Player _player;
- private Deck _playingDeck;
- private bool _isRun;
- public Menu()
- {
- _player = new Player();
- _playingDeck = new Deck();
- _isRun = true;
- }
- public void Run()
- {
- while (_isRun)
- {
- Console.WriteLine("\n[1] - Достать карту\n" +
- "[2] - Показать набранную руку\n" +
- "[0] - Выход");
- Console.WriteLine($"\nУ вас в руке {_player.GetCardsCount()} карт\n");
- Console.Write("Ввод: ");
- string userInput = Console.ReadLine();
- Console.Clear();
- switch (userInput)
- {
- case RequestCardCommand:
- RequestCard();
- break;
- case RequestToShowHandCommand:
- RequestToShowHand();
- break;
- case ExitCommand:
- Console.WriteLine("Выход");
- _isRun = false;
- break;
- default:
- Console.WriteLine("Неправильный ввод!");
- break;
- }
- }
- }
- private void RequestCard()
- {
- if (_player.IsMaxHand())
- {
- return;
- }
- Card card = _playingDeck.GetCard();
- _player.TakeCard(card);
- }
- private void RequestToShowHand()
- {
- if (_player.IsHandEmpty())
- {
- return;
- }
- _player.ShowHand();
- _playingDeck.Shuffle();
- }
- }
- class Player
- {
- private const int MaxCardsNumber = 7;
- private List<Card> _cards;
- public Player()
- {
- _cards = new List<Card>();
- }
- public int GetCardsCount()
- {
- return _cards.Count;
- }
- public bool IsMaxHand()
- {
- if (_cards.Count == MaxCardsNumber)
- {
- Console.WriteLine("В руке максимальное количество карт!");
- return true;
- }
- return false;
- }
- public bool IsHandEmpty()
- {
- if (_cards.Count == 0)
- {
- Console.WriteLine("В руке нет карт!");
- return true;
- }
- return false;
- }
- public void TakeCard(Card card)
- {
- _cards.Add(card);
- }
- public void ShowHand()
- {
- Console.WriteLine("Карты в руке...");
- foreach (Card card in _cards)
- {
- Console.Write(card.Name + "; ");
- }
- _cards.Clear();
- }
- }
- class Deck
- {
- private List<Card> _playingCards;
- private List<Card> _droppedCards;
- private Random _random;
- public Deck()
- {
- _playingCards = new List<Card>()
- {
- new Card("6"),
- new Card("7"),
- new Card("8"),
- new Card("9"),
- new Card("10"),
- new Card("Валет"),
- new Card("Дама"),
- new Card("Король"),
- new Card("Туз"),
- };
- _droppedCards = new List<Card>();
- _random = new Random();
- Shuffle();
- }
- public void Shuffle()
- {
- _playingCards.AddRange(_droppedCards);
- _droppedCards.Clear();
- for (int i = _playingCards.Count - 1; i > 0; i--)
- {
- int randomIndex = _random.Next(i + 1);
- Card randomCard = _playingCards[randomIndex];
- _playingCards[randomIndex] = _playingCards[i];
- _playingCards[i] = randomCard;
- }
- }
- public Card GetCard()
- {
- int cardIndex = _random.Next(0, _playingCards.Count);
- Card randomCard = _playingCards[cardIndex];
- _droppedCards.Add(_playingCards[cardIndex]);
- _playingCards.RemoveAt(cardIndex);
- return randomCard;
- }
- }
- class Card
- {
- public Card(string name)
- {
- Name = name;
- }
- public string Name { get; private set; }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement