Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- internal class Program
- {
- static void Main(string[] args)
- {
- const string TakeCardCommand = "1";
- const string EndGameCommand = "2";
- Player player = new Player();
- Deck deck = new Deck();
- Game game = new Game(player, deck);
- bool isWork = true;
- while (isWork)
- {
- Console.WriteLine("====== Blackjack игра ========\n");
- Console.WriteLine($"{TakeCardCommand}. Вытянуть карту\n{EndGameCommand}. Остановиться\n");
- player.ShowHand();
- Console.Write("\nВыбирите действие: ");
- string userInput = Console.ReadLine();
- switch (userInput)
- {
- case TakeCardCommand:
- game.PlayerTakeCard();
- break;
- case EndGameCommand:
- game.ShowResultGame();
- isWork = false;
- break;
- default:
- Console.WriteLine("Некорректный ввод");
- break;
- }
- Console.ReadKey();
- Console.Clear();
- }
- }
- }
- class Card
- {
- public Card(string name, int point)
- {
- Name = name;
- Point = point;
- }
- public string Name { get; private set; }
- public int Point { get; private set; }
- }
- class Deck
- {
- private List<Card> _cards = new List<Card>();
- private Random _random = new Random();
- public Deck()
- {
- CreateDeck();
- Shuffle();
- }
- private void CreateDeck()
- {
- int _maxDeckSize = 12;
- for (int i = 0; i < _maxDeckSize; i++)
- {
- _cards.Add(new Card($"Карта {i + 1}", AddCardPoints()));
- }
- }
- private int AddCardPoints()
- {
- int minPoint = 1;
- int maxPoint = 11;
- int point = _random.Next(minPoint, maxPoint);
- return point;
- }
- private void Shuffle()
- {
- for (int i = _cards.Count - 1; i >= 1; i--)
- {
- int randomCardPosition = _random.Next(i + 1);
- (_cards[i], _cards[randomCardPosition]) = (_cards[randomCardPosition], _cards[i]);
- }
- }
- public bool TryGetCard(out Card currentCard)
- {
- currentCard = null;
- if (_cards.Count > 0)
- {
- currentCard = _cards[0];
- _cards.Remove(currentCard);
- return true;
- }
- return false;
- }
- }
- class Player
- {
- private List<Card> _hand = new List<Card>();
- public int GetCountPoints()
- {
- int countPoints = 0;
- for (int i = 0; i < _hand.Count; i++)
- {
- countPoints += _hand[i].Point;
- }
- return countPoints;
- }
- public void TakeCard(Card card)
- {
- _hand.Add(card);
- }
- public void ShowHand()
- {
- Console.WriteLine("Ваши карты:");
- foreach (Card card in _hand)
- {
- Console.WriteLine(card.Name);
- }
- Console.WriteLine($"Всего очков: {GetCountPoints()}");
- }
- }
- class Game
- {
- private Player _player;
- private Deck _deck;
- public Game(Player player, Deck deck)
- {
- _player = player;
- _deck = deck;
- }
- public void PlayerTakeCard()
- {
- if (_deck.TryGetCard(out Card card))
- {
- _player.TakeCard(card);
- }
- else
- {
- Console.WriteLine("В колоде закончились карты");
- }
- }
- public void ShowResultGame()
- {
- int playerPoints = _player.GetCountPoints();
- int maxPointsToWin = 21;
- if (playerPoints <= maxPointsToWin)
- {
- Console.WriteLine($"Вы набрали {playerPoints} очков");
- Console.WriteLine("Вы выиграли!");
- }
- else
- {
- Console.WriteLine($"Вы набрали {playerPoints} очков");
- Console.WriteLine("Вы проиграли!");
- }
- }
- }
Add Comment
Please, Sign In to add comment