Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- class Program
- {
- static void Main(string[] args)
- {
- Deck deck = new Deck();
- Player player = new Player("Player 1");
- bool isCorrectInput;
- Console.Write("Сколько раз игрок должен вытащить карту?(введите число): ");
- string userInput = Console.ReadLine();
- isCorrectInput = Int32.TryParse(userInput, out int countCards);
- if (isCorrectInput)
- {
- if (countCards != 0 && countCards > 0)
- {
- deck.Shuffle();
- for (int i = 0; i < countCards; i++)
- {
- player.TakeCard(deck.GiveCard());
- }
- }
- else
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("\aНекорректный ввод! Вы проиграли.");
- Console.ForegroundColor = ConsoleColor.White;
- }
- }
- else
- {
- Console.ForegroundColor = ConsoleColor.Red;
- Console.WriteLine("\a Нужно вводить число! Вы проиграли.");
- Console.ForegroundColor = ConsoleColor.White;
- }
- deck.Show();
- player.ShowCards();
- Console.ReadKey();
- }
- }
- class Card
- {
- private string _suit;
- private string _rank;
- public Card(string suit, string rank)
- {
- _suit = suit;
- _rank = rank;
- }
- public void Show()
- {
- Console.WriteLine($"Масть - {_suit}, достоинство - {_rank}");
- }
- }
- class Deck
- {
- private List<Card> _cards = new List<Card>();
- private Random _random = new Random();
- public Deck()
- {
- List<string> _suit = new List<string>(new string[] { "Diamonds", "Spades", "Hearts", "Clubs" });
- List<string> _rank = new List<string>((new string[] { "Ace", "Two", "Three", "Four", "Five", "Six",
- "Seven", "Eight", "Nine", "Ten", "Jack", "Queen", "King" }));
- for (int i = 0; i < _suit.Count; i++)
- {
- for (int j = 0; j < _rank.Count; j++)
- {
- _cards.Add(new Card(_suit[i], _rank[j]));
- }
- }
- }
- public void Show()
- {
- Console.WriteLine("\nПоказываю всю колоду карт:");
- for (int i = 0; i < _cards.Count; i++)
- {
- Console.Write($"{i + 1} карта - ");
- _cards[i].Show();
- }
- }
- public void Shuffle()
- {
- for (int i = 0; i < _cards.Count; i++)
- {
- int randomIndex = _random.Next(_cards.Count + 1 - i);
- Card tempCard = _cards[i];
- _cards[i] = _cards[randomIndex];
- _cards[randomIndex] = tempCard;
- }
- }
- public Card GiveCard()
- {
- Card tempCard = null;
- if (_cards.Count > 0)
- {
- tempCard = _cards[0];
- _cards.Remove(tempCard);
- }
- else
- {
- Console.WriteLine($"\aКарты в колоде закончились.");
- }
- return tempCard;
- }
- }
- class Player
- {
- private List<Card> _cards = new List<Card>();
- public string Name { get; private set; }
- public Player(string name)
- {
- Name = name;
- }
- public void TakeCard(Card card)
- {
- _cards.Add(card);
- }
- public void ShowCards()
- {
- Console.WriteLine("\nПоказываю карты игрока:");
- for (int i = 0; i < _cards.Count; i++)
- {
- Console.Write($"{i + 1} карта: ");
- _cards[i].Show();
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement