Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace OOPTask4CroupieCards
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Croupier croupier = new Croupier();
- croupier.GiveCardsToPlayer();
- Console.WriteLine();
- Console.ReadKey();
- }
- }
- public class Croupier
- {
- private Deck _deck = new Deck();
- private Player _player = new Player();
- public void GiveCardsToPlayer()
- {
- bool isCorrectCount = false;
- int cardsCount = 0;
- while (isCorrectCount == false)
- {
- Console.Write("Введите сколько карт вы хотите передать игроку: ");
- cardsCount = ReadInt();
- if (cardsCount < 1)
- {
- Console.WriteLine("Невозможно выдать отрицательное число карт!");
- }
- else if (cardsCount > _deck.CardsCount)
- {
- Console.WriteLine("Это больше, чем во всей колоде!");
- }
- else
- {
- isCorrectCount = true;
- }
- }
- for (int i = 0; i < cardsCount; i++)
- {
- if(_deck.HasCard)
- {
- _player.AddCard(_deck.GiveCard());
- }
- else
- {
- Console.WriteLine("Карты в колоде каким-то образом закончились, не смотря на первую проверку");
- break;
- }
- }
- _player.ShowAllCards();
- }
- private int ReadInt()
- {
- int inputNumber;
- while (int.TryParse(Console.ReadLine(), out inputNumber) == false)
- {
- Console.Write("Это не число, попробуйте еще раз: ");
- }
- return inputNumber;
- }
- }
- public class Player
- {
- private List<Card> _cards = new List<Card>();
- public void AddCard(Card card)
- {
- _cards.Add(card);
- }
- public void ShowAllCards()
- {
- Console.WriteLine($"У игрока {_cards.Count} карт:");
- foreach(Card card in _cards)
- {
- card.ShowInfo();
- }
- }
- }
- public class Deck
- {
- private List<Card> _cards = new List<Card>();
- private Random _random = new Random();
- public Deck()
- {
- Fill();
- ShuffleCards();
- }
- public int CardsCount
- {
- get
- {
- return _cards.Count;
- }
- private set
- {
- }
- }
- public bool HasCard
- {
- get
- {
- return _cards.Count > 0;
- }
- private set
- {
- }
- }
- private void Fill()
- {
- string[] standartCardLevels = {
- "двойка",
- "тройка",
- "четверка",
- "пятерка",
- "шестерка",
- "семерка",
- "восьмерка",
- "девятка",
- "десятка",
- "валет",
- "дама",
- "король",
- "туз"
- };
- string[] standartCardSuits = {
- "черви",
- "пики",
- "буби",
- "крести"
- };
- foreach (string cardLevel in standartCardLevels)
- {
- foreach (string cardSuit in standartCardSuits)
- {
- _cards.Add(new Card(cardLevel, cardSuit));
- }
- }
- _cards.Add(new Card("джокер"));
- _cards.Add(new Card("джокер"));
- }
- private void ShuffleCards()
- {
- int randomIndex;
- Card randomCard;
- for(int i=0; i<_cards.Count; i++)
- {
- randomIndex = _random.Next(0, _cards.Count);
- randomCard = _cards[randomIndex];
- _cards[randomIndex] = _cards[i];
- _cards[i] = randomCard;
- }
- }
- public Card GiveCard()
- {
- Card card = _cards[0];
- _cards.RemoveAt(0);
- return card;
- }
- }
- public class Card
- {
- private string _level;
- private string _suit;
- public Card(string level, string suit = "")
- {
- _level = level;
- _suit = suit;
- }
- public void ShowInfo()
- {
- Console.Write($"\n{_level}");
- if(_suit != "")
- Console.Write($" {_suit}");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement