Last active
May 7, 2025 14:56
-
-
Save VladislavSavich/bfd784cb306ef062cbc94e49054326e5 to your computer and use it in GitHub Desktop.
C# Junior Колода карт
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections; | |
using System.Collections.Generic; | |
namespace CSLight | |
{ | |
internal class Program | |
{ | |
static void Main(string[] args) | |
{ | |
Сroupier croupier = new Сroupier(); | |
croupier.Work(); | |
} | |
} | |
class Сroupier | |
{ | |
private Random _random = new Random(); | |
private Deck _deck; | |
private Player _player; | |
public Сroupier() | |
{ | |
_deck = new Deck(); | |
_player = new Player(); | |
} | |
public void Work() | |
{ | |
const string CommandTransferCards = "1"; | |
const string CommandShowPlayerCards = "2"; | |
const string CommandShowDeck = "3"; | |
const string CommandExit = "Exit"; | |
string userInput; | |
bool isWorking = true; | |
_deck.Shuffle(_random); | |
while (isWorking) | |
{ | |
Console.WriteLine("-КРУПЬЕ-"); | |
Console.WriteLine($"{CommandTransferCards} - Передать карты игроку"); | |
Console.WriteLine($"{CommandShowPlayerCards} - Показать руку игрока"); | |
Console.WriteLine($"{CommandShowDeck} - Показать карты в колоде"); | |
Console.WriteLine($"{CommandExit} - выход"); | |
userInput = Console.ReadLine(); | |
switch (userInput) | |
{ | |
case CommandTransferCards: | |
TransferCardsToPlayer(_player, _deck); | |
break; | |
case CommandShowPlayerCards: | |
_player.ShowCards(); | |
break; | |
case CommandShowDeck: | |
_deck.ShowCards(); | |
break; | |
case CommandExit: | |
isWorking = false; | |
break; | |
} | |
Console.ReadKey(); | |
Console.Clear(); | |
} | |
} | |
private void TransferCardsToPlayer(Player player, Deck deck) | |
{ | |
int number; | |
Console.WriteLine("Сколько карт передать игроку?"); | |
number = ReadInt(); | |
if (number <= 0 || number > deck.NumberOfCards) | |
{ | |
Console.WriteLine("Невозможно передать заданное количество карт!"); | |
} | |
else | |
{ | |
player.TakeCards(deck.DealCards(number)); | |
} | |
} | |
private int ReadInt() | |
{ | |
int number; | |
while (int.TryParse(Console.ReadLine(), out number) == false) | |
{ | |
Console.WriteLine("Некорректный ввод! Попробуйте снова!"); | |
} | |
return number; | |
} | |
} | |
class Player | |
{ | |
private List<Card> _cards; | |
public Player() | |
{ | |
_cards = new List<Card>(); | |
} | |
public void ShowCards() | |
{ | |
for (int i = 0; i < _cards.Count; i++) | |
{ | |
_cards[i].ShowInfo(); | |
} | |
} | |
public void TakeCards(List<Card> cards) | |
{ | |
for (int i = 0; i < cards.Count; i++) | |
{ | |
_cards.Add(cards[i]); | |
} | |
} | |
} | |
class Deck | |
{ | |
private List<Card> _cards; | |
private int _maximumCardNumber = 10; | |
private string _cardsSuits = "♠♥♦♣"; | |
public Deck() | |
{ | |
_cards = new List<Card>(); | |
for (int i = 0; i < _cardsSuits.Length; i++) | |
{ | |
for (int j = 1; j <= _maximumCardNumber; j++) | |
{ | |
_cards.Add(new Card(_cardsSuits[i], j)); | |
} | |
} | |
} | |
public int NumberOfCards | |
{ | |
get | |
{ | |
return _cards.Count; | |
} | |
private set { } | |
} | |
public void Shuffle(Random random) | |
{ | |
Card tempCard; | |
int indexShuffledCard; | |
int minValue = 0; | |
int maxValue = _cards.Count; | |
int lastCardIndex = _cards.Count - 1; | |
for (int i = lastCardIndex; i > 0; i--, maxValue--) | |
{ | |
indexShuffledCard = random.Next(minValue, maxValue); | |
tempCard = _cards[indexShuffledCard]; | |
_cards[indexShuffledCard] = _cards[i]; | |
_cards[i] = tempCard; | |
} | |
} | |
public List<Card> DealCards(int number) | |
{ | |
List<Card> cardsForDeal = new List<Card>(); | |
for (int i = 0; i < number; i++) | |
{ | |
cardsForDeal.Add(_cards[i]); | |
} | |
_cards.RemoveRange(0, number); | |
return cardsForDeal; | |
} | |
public void ShowCards() | |
{ | |
for (int i = 0; i < _cards.Count; i++) | |
{ | |
_cards[i].ShowInfo(); | |
} | |
} | |
} | |
class Card | |
{ | |
public Card(char suit, int number) | |
{ | |
Suit = suit; | |
Number = number; | |
} | |
public char Suit { get; private set; } | |
public int Number { get; private set; } | |
public void ShowInfo() | |
{ | |
Console.WriteLine($"Карта: {Number} {Suit}"); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment