Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace Карты
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Croupier croupier = new Croupier();
- croupier.PlayGame();
- }
- }
- class Croupier
- {
- private Deck _deck = new Deck();
- private Player _player = new Player();
- private Dictionary<string, int> _valueCardsInPoints = new Dictionary<string, int>();
- public void PlayGame()
- {
- const string GetCardMenu = "1";
- const string ShowAllCardsAndPointsMenu = "2";
- const string ShuffleMenu = "3";
- const string ExitMenu = "4";
- bool isRun = true;
- AssignPoints();
- while (isRun)
- {
- Console.WriteLine($"{GetCardMenu} - вытянуть карту\n{ShowAllCardsAndPointsMenu} - показать карты на руках\n{ShuffleMenu} - перетасовать карты\n{ExitMenu} - выход");
- string userInput = Console.ReadLine();
- switch (userInput)
- {
- case GetCardMenu:
- TransferCard();
- break;
- case ShowAllCardsAndPointsMenu:
- ShowAllCardsAndPoints();
- break;
- case ShuffleMenu:
- _deck.Shuffle();
- break;
- case ExitMenu:
- isRun = false;
- break;
- default:
- Console.WriteLine("\nНет такой команды\n");
- break;
- }
- }
- }
- private void AssignPoints()
- {
- string[] ranks = { "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
- int[] prices = { 6, 7, 8, 9, 10, 2, 3, 4, 11 };
- for (int i = 0; i < ranks.Length; i++)
- {
- _valueCardsInPoints.Add(ranks[i], prices[i]);
- }
- }
- private void ShowAllCardsAndPoints()
- {
- int minCardInDeck = 1;
- int _points = 0;
- if (_deck.CardCount() >= 1)
- {
- foreach (var card in _player.ReturnCards())
- {
- _player.ShowCard(card);
- _points += _valueCardsInPoints[card.Rank];
- }
- Console.WriteLine($"\nОбщее количество очков - {_points}");
- _points = 0;
- }
- else if (_deck.CardCount() > minCardInDeck)
- {
- foreach (var card in _player.ReturnCards())
- {
- _player.ShowCard(card);
- }
- Console.WriteLine("Карты в колоде закончились");
- }
- else
- {
- Console.WriteLine("\nСначала вытяни карту\n");
- }
- }
- private void TransferCard()
- {
- _player.ShowCard(_player.AddCard(_deck.GiveCard()));
- }
- }
- class Player
- {
- private List<Card> _cards = new List<Card>();
- public Card AddCard(Card card)
- {
- if (card != null)
- {
- _cards.Add(card);
- }
- return card;
- }
- public List<Card> ReturnCards()
- {
- return new List<Card>(_cards);
- }
- public void ShowCard(Card card)
- {
- if (card != null)
- {
- Console.WriteLine($"Масть - {card.Suit} || Номинал - {card.Rank}");
- }
- }
- }
- class Card
- {
- public Card(string rank, char suit)
- {
- Rank = rank;
- Suit = suit;
- }
- public string Rank { get; private set; }
- public char Suit { get; private set; }
- }
- class Deck
- {
- private Random _random = new Random();
- private List<Card> _cards;
- public Deck()
- {
- _cards = new List<Card>();
- CreateCards();
- }
- public int CardCount() => _cards.Count;
- public Card GiveCard()
- {
- Card card = null;
- int minCardInDeck = 1;
- int topCardIndex = 0;
- if (_cards.Count >= minCardInDeck)
- {
- card = _cards[topCardIndex];
- _cards.Remove(card);
- }
- else
- {
- Console.WriteLine("\nКарты в колоде закончились\n");
- }
- return card;
- }
- public void Shuffle()
- {
- for (int i = _cards.Count - 1; i >= 1; i--)
- {
- int randomIndexInList = _random.Next(_cards.Count);
- Card temporaryValue = _cards[randomIndexInList];
- _cards[randomIndexInList] = _cards[i];
- _cards[i] = temporaryValue;
- }
- Console.WriteLine("\nКарты перетасованы\n");
- }
- private void CreateCards()
- {
- string[] ranks = { "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
- char[] suits = { '♣', '♦', '♥', '♠' };
- for (int i = 0; i < ranks.Length; i++)
- {
- for (int j = 0; j < suits.Length; j++)
- {
- Card card = new Card(ranks[i], suits[j]);
- _cards.Add(card);
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement