Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- namespace Ijunior
- {
- internal class Program
- {
- static void Main(string[] args)
- {
- Game game = new Game();
- game.Play();
- }
- }
- class Game
- {
- private Player _player;
- private Deck _deck;
- public Game()
- {
- _player = new Player();
- _deck = new Deck();
- }
- public void Play()
- {
- const string CommandExit = "exit";
- bool isWork = true;
- string userInput;
- while (isWork)
- {
- _player.ShowCards();
- if (_deck.Lenght > 0)
- {
- Console.Write($"\n\nВ колоде {_deck.Lenght} карт" +
- $"\nСколько карт вы хотите добрать (введите {CommandExit} для выхода)? ");
- userInput = Console.ReadLine();
- if (int.TryParse(userInput, out int countCards))
- {
- if (_deck.TryGetCard(countCards, out List<Card> cards))
- _player.PutInHand(cards);
- }
- else if (userInput.ToLower() == CommandExit)
- {
- isWork = false;
- }
- else
- {
- Console.Write("Некорректный ввод");
- Console.ReadKey();
- }
- }
- else
- {
- isWork = false;
- Console.WriteLine("\n\nИгра закончена");
- Console.ReadKey();
- }
- Console.Clear();
- }
- }
- }
- class Player
- {
- private List<Card> _cards = new List<Card>();
- public void PutInHand(List<Card> cards)
- {
- if (cards != null)
- _cards.AddRange(cards);
- }
- public void ShowCards()
- {
- if (_cards.Count > 0)
- {
- Console.WriteLine("Карты на руке:");
- foreach (var card in _cards)
- {
- card.ShowInfo();
- }
- }
- else
- {
- Console.Write("У игрока пока нет карт");
- }
- }
- }
- class Deck
- {
- private List<Card> _deck = new List<Card>();
- public Deck()
- {
- Fill();
- Shuffle();
- }
- public int Lenght => _deck.Count;
- public bool TryGetCard(int countCards, out List<Card> cards)
- {
- cards = new List<Card>();
- if (_deck.Count >= countCards)
- {
- for (int i = 0; i < countCards; i++)
- {
- cards.Add(_deck[0]);
- _deck.RemoveAt(0);
- }
- return true;
- }
- else
- {
- Console.Write("В колоде недостаточно карт");
- Console.ReadKey();
- }
- return false;
- }
- private void Fill()
- {
- string[] values = new string[] { "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" };
- char[] suits = new char[] { '♣', '♥', '♦', '♠' };
- foreach (char suit in suits)
- {
- foreach (string value in values)
- {
- _deck.Add(new Card(value, suit));
- }
- }
- }
- private void Shuffle()
- {
- int randomIndex;
- Card tempCard;
- for (int i = 0; i < _deck.Count; i++)
- {
- tempCard = _deck[i];
- randomIndex = Utils.GetRandomNumber(_deck.Count);
- _deck[i] = _deck[randomIndex];
- _deck[randomIndex] = tempCard;
- }
- }
- }
- class Card
- {
- private string _value;
- private char _suit;
- public Card(string value, char suit)
- {
- _value = value;
- _suit = suit;
- }
- public void ShowInfo()
- {
- Console.Write(_value + _suit + " ");
- }
- }
- class Utils
- {
- private static Random s_random = new Random();
- public static int GetRandomNumber(int number)
- {
- return s_random.Next(number);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment