Skip to content

Instantly share code, notes, and snippets.

@Dead-in-side
Last active April 18, 2024 18:32
Show Gist options
  • Save Dead-in-side/4d43d082f691ffa02eecd7cf9681e5b9 to your computer and use it in GitHub Desktop.
Save Dead-in-side/4d43d082f691ffa02eecd7cf9681e5b9 to your computer and use it in GitHub Desktop.
using System.Runtime.InteropServices;
namespace IJunior
{
public class Program
{
private static void Main(string[] args)
{
Croupier croupier = new Croupier();
croupier.GetNewDeck();
Console.WriteLine("Введите кол-во карт для раздачи");
if (int.TryParse(Console.ReadLine(), out int numberOfCards))
{
croupier.DealCards(numberOfCards);
croupier.Player.ShowCards();
}
}
}
public class Croupier
{
private Deck _deck = new Deck();
private Player _player = new Player();
public Player Player
{
get
{
return _player;
}
private set
{
_player = value;
}
}
public void GetNewDeck()
{
_deck.FillWithCard();
_deck.Mix();
}
public void DealCards(int quantityOfCards)
{
for (int i = 0; i < quantityOfCards; i++)
{
if (_deck.TryGetCard(out Card card))
{
_player.AddCard(card);
}
else
{
Console.WriteLine("Карты закончились!");
}
}
}
}
public class Player
{
private List<Card> _cards = new List<Card>();
public void AddCard(Card card)
{
_cards.Add(card);
}
public void ShowCards()
{
foreach (Card card in _cards)
{
card.ShowInfo();
}
}
}
public class Deck
{
private Stack<Card> _cards = new Stack<Card>();
private List<Card> _newCards;
private int _maxQuantityOfCadrs = 52;
private List<char> _symbolsCard = ['@', '#', '$', '%'];
public void FillWithCard()
{
_newCards = new List<Card>();
for (int i = 0; i < _symbolsCard.Count; i++)
{
for (int j = 1; j <= _maxQuantityOfCadrs / _symbolsCard.Count; j++)
{
Card card = new Card(j, _symbolsCard[i]);
_newCards.Add(card);
}
}
}
public void Mix()
{
Random random = new Random();
random.Shuffle(CollectionsMarshal.AsSpan(_newCards));
foreach (Card card in _newCards)
{
_cards.Push(card);
}
}
public bool TryGetCard(out Card card)
{
card = null;
if(_cards.Count > 0)
{
card = _cards.Pop();
}
return _cards.Count > 0;
}
}
public class Card
{
private int _value;
private char _symbol;
public Card(int value, char symbole)
{
_value = value;
_symbol = symbole;
}
public void ShowInfo()
{
Console.WriteLine($"Карта: {_value}, масть {_symbol}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment