Skip to content

Instantly share code, notes, and snippets.

@KACTAHEDA
Last active May 27, 2025 11:53
Show Gist options
  • Save KACTAHEDA/14547410002679f2d21b259860a24b7a to your computer and use it in GitHub Desktop.
Save KACTAHEDA/14547410002679f2d21b259860a24b7a to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
namespace Casino
{
class Program
{
static void Main(string[] args)
{
const string PlayCommand = "1";
const string ExitCommand = "2";
Croupier croupier = new Croupier();
bool isWork = true;
while (isWork == true)
{
croupier.DisplayStartMenu(PlayCommand, ExitCommand);
switch (Console.ReadLine())
{
case PlayCommand:
croupier.PlayRound();
break;
case ExitCommand:
isWork = false;
break;
default:
Console.WriteLine("Неверная команда.Нажмите любую клавишу чтобы продолжить : ");
Console.ReadKey();
break;
}
}
}
}
class Croupier
{
private Deck _deck = new Deck();
private Player _player = new Player();
public void PlayRound()
{
DealCards();
_player.ShowCards();
Console.WriteLine("\nНажмите любую клавишу чтобы продолжить: ");
Console.ReadKey();
}
public void DisplayStartMenu(string play, string exit)
{
Console.Clear();
Console.WriteLine("\n\nДобро пожаловать в казино <<Три топора>>");
Console.WriteLine($"\nЧтобы играть введите {play} ");
Console.WriteLine($"\nЧтобы выйти введите {exit} ");
}
private void DealCards()
{
Console.WriteLine("Сколько карт Вам раздать? ");
int numberOfCards = Utilite.ReadNumber();
Console.WriteLine($"Карт заказано игроком: {numberOfCards}\n");
TryDealCards(numberOfCards);
}
private void TryDealCards(int orderedCards)
{
if (_deck.ShowCardsCount() == 0)
{
Console.WriteLine("\nВ колоде нет карт\n");
Console.WriteLine("Распакуем новую колоду.\n");
}
else if (orderedCards <= _deck.ShowCardsCount())
{
_player.ReseeveCards(_deck.GiveCards(orderedCards));
}
else
{
Console.WriteLine("Вы заказали слишком много.");
Console.WriteLine($"Карт осталось в колоде {_deck.ShowCardsCount()}");
}
}
}
class Player
{
private List<Card> _cardsInHand = new List<Card>();
public void ReseeveCards(List<Card> cards)
{
foreach (var card in cards)
{
_cardsInHand.Add(card);
}
}
public void ShowCards()
{
Console.WriteLine("Карты игрока:\n");
foreach (var card in _cardsInHand)
{
card.Show();
}
}
}
class Deck
{
private List<Card> _playingCards = new List<Card>();
public Deck()
{
Initialize();
Shuffle();
}
public List<Card> GiveCards(int cardsCountToGive)
{
List<Card> result = new List<Card>();
HandOutCards(cardsCountToGive, result);
RemoveCards(result);
Console.WriteLine($"Карт в колоде: {_playingCards.Count}\n");
return result;
}
public int ShowCardsCount()
{
return _playingCards.Count;
}
private void HandOutCards(int cardsCount, List<Card> result)
{
for (int i = 0; i < cardsCount; i++)
{
result.Add(_playingCards[i]);
}
}
private void RemoveCards(List<Card> cardsToRemove)
{
foreach (var card in cardsToRemove)
{
_playingCards.Remove(card);
}
}
private void Shuffle()
{
Random random = new Random();
for (int i = _playingCards.Count - 1; i > 0; i--)
{
int randomCardIndex = random.Next(0, i + 1);
Card tempCard = _playingCards[i];
_playingCards[i] = _playingCards[randomCardIndex];
_playingCards[randomCardIndex] = tempCard;
}
}
private void Initialize()
{
string[] suits = new string[] { "Spades", "Clubs", "Diamond", "Hearts" };
string[] valuesOfCards = new string[] { "Ace", "King", "Queen", "Jack", "Ten", "Nine", "Eight", "Seven", "Six" };
for (int i = 0; i < suits.Length; i++)
{
for (int j = 0; j < valuesOfCards.Length; j++)
{
_playingCards.Add(new Card(suits[i], valuesOfCards[j]));
}
}
}
}
class Card
{
public Card(string suit, string value)
{
Suit = suit;
Value = value;
}
public string Suit { get; private set; }
public string Value { get; private set; }
public void Show()
{
Console.WriteLine($"{Value} of {Suit}");
}
}
static class Utilite
{
public static int ReadNumber()
{
int number = 0;
bool isFine = false;
while (isFine == false)
{
isFine = Int32.TryParse(Console.ReadLine(), out int value);
if (isFine == false)
{
Console.WriteLine("Ожидалось, что Вы введете число.");
Console.WriteLine("Попробуйте еще раз: ");
}
number = value;
}
return number;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment