Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Homework41
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Croupier croupier = new Croupier();
- croupier.Play();
- }
- }
- class Croupier
- {
- private Deck _deck;
- public Croupier()
- {
- _deck = new Deck();
- }
- public void Play()
- {
- Player player = new Player();
- const string CommandDealCards = "deal";
- const string CommandShowCards = "show";
- const string CommandExitProgram = "exit";
- bool isWork = true;
- while (isWork)
- {
- Console.WriteLine("Вы за игральным столом!!!");
- Console.WriteLine($"Если хотите сыграть, введите {CommandDealCards} !");
- Console.WriteLine($"Чтобы показать карты на руке, введите {CommandShowCards} !");
- Console.WriteLine($"Если хотите выйти из-за стола, введите {CommandExitProgram}");
- string userInput = Console.ReadLine()?.Trim().ToLower();
- switch (userInput)
- {
- case CommandDealCards:
- DealCards(player);
- break;
- case CommandShowCards:
- player.ShowCards();
- break;
- case CommandExitProgram:
- isWork = ExitProgram();
- break;
- default:
- Console.WriteLine("Неверная команда, попробуйте снова.");
- break;
- }
- Console.ReadKey();
- Console.Clear();
- }
- }
- private void DealCards(Player player)
- {
- Console.Write("Введите количество карт для игрока: ");
- string userInput = Console.ReadLine();
- int numberOfCards;
- if (int.TryParse(userInput, out numberOfCards) && numberOfCards > 0)
- {
- if (numberOfCards <= _deck.CardsRemaining)
- {
- for (int i = 0; i < numberOfCards; i++)
- {
- Card card = _deck.DrawCard();
- if (card != null)
- {
- player.TakeCard(card);
- }
- }
- Console.WriteLine($"{numberOfCards} карт(ы) выданы игроку.");
- }
- else
- {
- Console.WriteLine("Недостаточно карт в колоде.");
- }
- }
- else
- {
- Console.WriteLine("Введено некорректное количество.");
- }
- }
- private bool ExitProgram()
- {
- Console.WriteLine("Вы вышли из-за стола");
- return false;
- }
- }
- class Player
- {
- private List<Card> _cards;
- public Player()
- {
- _cards = new List<Card>();
- }
- public void TakeCard(Card card)
- {
- _cards.Add(card);
- }
- public void ShowCards()
- {
- if (_cards.Count == 0)
- {
- Console.WriteLine("У вас нет карт на руке.");
- }
- else
- {
- Console.WriteLine("Карты на руке:");
- foreach (var card in _cards)
- {
- card.ShowInfo();
- }
- }
- }
- }
- class Deck
- {
- private List<Card> _cards = new List<Card>();
- private Random _random = new Random();
- public Deck()
- {
- FillCards();
- ShuffleCards();
- }
- public int CardsRemaining => _cards.Count;
- public Card DrawCard()
- {
- if (_cards.Count == 0)
- return null;
- Card card = _cards[0];
- _cards.RemoveAt(0);
- return card;
- }
- private void FillCards()
- {
- List<string> names = new List<string>
- {
- "2", "3", "4", "5", "6", "7", "8", "9", "10", "Валет", "Дама", "Король", "Туз"
- };
- List<string> suits = new List<string>
- {
- "Пики", "Черви", "Трефы", "Бубны"
- };
- foreach (var suit in suits)
- {
- foreach (var name in names)
- {
- _cards.Add(new Card(name, suit));
- }
- }
- }
- private void ShuffleCards()
- {
- for (int i = 0; i < _cards.Count; i++)
- {
- int randomIndex = _random.Next(i, _cards.Count);
- var temp = _cards[i];
- _cards[i] = _cards[randomIndex];
- _cards[randomIndex] = temp;
- }
- }
- }
- class Card
- {
- private string _name;
- private string _suit;
- public Card(string name, string suit)
- {
- _name = name;
- _suit = suit;
- }
- public void ShowInfo()
- {
- Console.WriteLine($"Название карты: {_name} | Масть: {_suit}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement