Skip to content

Instantly share code, notes, and snippets.

@Strelok78
Last active May 17, 2023 07:49
Show Gist options
  • Save Strelok78/4e20151f1eb571968f371b94498edab9 to your computer and use it in GitHub Desktop.
Save Strelok78/4e20151f1eb571968f371b94498edab9 to your computer and use it in GitHub Desktop.
There is a deck with cards. The player takes out the cards until he decides that he has enough cards. After that, all information about the drawn cards is displayed.
internal class Program
{
public static void Main()
{
Player player = new Player();
player.ShowMenu();
}
}
class Card
{
public Card(string cardValue, string suitNumber)
{
Suit = suitNumber;
CardValue = cardValue;
}
public string Suit { get; private set; }
public string CardValue { get; private set; }
}
class Deck
{
private List<Card> _cards = new List<Card>();
public Deck()
{
GenerateCards();
Shuffle();
}
public List<Card> Cards
{
get { return _cards; }
}
public void Shuffle()
{
int indexFirst;
Card temporary;
Random random = new Random();
for (int i = 0; i < _cards.Count; i++)
{
indexFirst = random.Next(_cards.Count);
temporary = _cards[i];
_cards[i] = _cards[indexFirst];
_cards[indexFirst] = temporary;
}
}
public Card GiveCard()
{
Card pickedCard = _cards[_cards.Count - 1];
_cards.Remove(pickedCard);
return pickedCard;
}
private void GenerateCards()
{
string[] suits = { "♥", "♠", "♦", "♣" };
string[] namedValues = { "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "Jack", "Queen", "King", "Ace" };
for (int i = 0; i < suits.Length; i++)
{
for (int j = 0; j < namedValues.Length; j++)
{
Card card = new Card(namedValues[j], suits[i]);
_cards.Add(card);
}
}
}
}
class Player
{
private int _maxCardsPicked = 5;
private List<Card> _pickedCards = new List<Card>();
private Deck _deck = new Deck();
public void ShowMenu()
{
const string CommandGetCard = "1";
const string CommandShowCards = "2";
const string CommandShuffleCards = "3";
const string CommandExit = "exit";
bool isOpen = true;
string text;
string menu = $"Press:\n" +
$"{CommandGetCard} - to get a card\n" +
$"{CommandShowCards} - to show cards\n" +
$"{CommandShuffleCards} - to shuffle cards\n" +
$"{CommandExit} - to leave application";
while (isOpen)
{
Console.WriteLine($"{_pickedCards.Count} Cards picked.\n" +
$"{_deck.Cards.Count} Cards left in the deck.\n");
Console.WriteLine(menu);
Console.WriteLine("\n\nEnter your command:");
text = Console.ReadLine();
switch (text)
{
case CommandGetCard:
TakeCard();
break;
case CommandShowCards:
ShowCards();
break;
case CommandShuffleCards:
_deck.Shuffle();
break;
case CommandExit:
isOpen = false;
break;
default:
Console.WriteLine("Try again.");
break;
}
if (_pickedCards.Count == _maxCardsPicked)
{
Console.Clear();
Console.WriteLine("Your reached max number of cards picked");
Console.WriteLine("Your cards: \n");
ShowCards();
Console.ReadKey();
isOpen = false;
}
Console.ReadKey();
Console.Clear();
}
}
private void TakeCard()
{
_pickedCards.Add(_deck.GiveCard());
}
private void ShowCards()
{
foreach (Card card in _pickedCards)
{
Console.WriteLine($"{card.CardValue} of {card.Suit}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment