Skip to content

Instantly share code, notes, and snippets.

@talatari
Last active November 14, 2023 07:51
Show Gist options
  • Save talatari/fffe515e9e7c20eba6bb07eedd90ce41 to your computer and use it in GitHub Desktop.
Save talatari/fffe515e9e7c20eba6bb07eedd90ce41 to your computer and use it in GitHub Desktop.
41. Task: Deck Of Cards
public class Program
{
static void Main()
{
Game game = new Game();
game.Play();
}
}
public class UserUtils
{
private static Random s_random = new Random();
public static int GetRandomNumber(int maxRange)
{
return s_random.Next(maxRange);
}
}
public class Card
{
public Card(string nominal, KeyValuePair<string, ConsoleColor> dictionarySuits)
{
Nominal = nominal;
DictionarySuits = dictionarySuits;
}
public string Nominal { get; private set; }
public KeyValuePair<string, ConsoleColor> DictionarySuits { get; private set; }
}
public class Deck
{
private List<Card> _cards = new List<Card>();
public Deck()
{
Dictionary<string, ConsoleColor> dictionarySuits = new Dictionary<string, ConsoleColor>();
dictionarySuits.Add("♠", ConsoleColor.Black);
dictionarySuits.Add("♣", ConsoleColor.Black);
dictionarySuits.Add("♦", ConsoleColor.Red);
dictionarySuits.Add("♥", ConsoleColor.Red);
string[] nominals = new string[] { " 2", " 3", " 4", " 5", " 6", " 7", " 8", " 9", "10",
" J", " Q", " K", " A" };
foreach (var dictionarySuit in dictionarySuits)
for (int i = 0; i < nominals.Length; i++)
_cards.Add(new Card(nominals[i], dictionarySuit));
}
public void Shuffle()
{
for (int i = 0; i < _cards.Count; i++)
{
int randomCardIndex = UserUtils.GetRandomNumber(_cards.Count);
Card card = _cards[i];
_cards[i] = _cards[randomCardIndex];
_cards[randomCardIndex] = card;
}
}
public Card GiveCard()
{
Card card = null;
if (_cards.Count > 0)
{
card = _cards.Last();
_cards.Remove(card);
}
return card;
}
public List<Card> GiveCards()
{
return _cards.ToList();
}
}
public class Game
{
public void Play()
{
const string ShuffleDeckCommand = "1";
const string TakeCardCommand = "2";
const string ShowAllCardsCommand = "3";
const string ExitCommand = "4";
bool isGaming = true;
bool isMyCardsOpen = false;
string showOrHideText;
Deck deck = new Deck();
Player player = new Player();
Renderer renderer = new Renderer();
while (isGaming == true)
{
Console.Clear();
Console.Write("Deck:");
renderer.DrawFrontCards(deck.GiveCards());
Console.Write("\n\nMy cards: ");
if (isMyCardsOpen == true)
{
renderer.DrawFrontCards(player.GiveCards());
showOrHideText = "Hide";
}
else
{
renderer.DrawBackCards(player.GiveCards());
showOrHideText = "Show";
}
Console.WriteLine("\n\nMenu: \n" +
$" {ShuffleDeckCommand} - Shuffle Deck\n" +
$" {TakeCardCommand} - Take a card\n" +
$" {ShowAllCardsCommand} - {showOrHideText} all cards\n" +
$" {ExitCommand} - Exit\n");
Console.Write("Enter command: ");
string userInput = Console.ReadLine();
switch (userInput)
{
case ShuffleDeckCommand:
deck.Shuffle();
break;
case TakeCardCommand:
player.TakeCard(deck.GiveCard());
break;
case ShowAllCardsCommand:
isMyCardsOpen = !isMyCardsOpen;
break;
case ExitCommand:
isGaming = false;
break;
}
}
}
}
public class Player
{
private List<Card> _cards = new List<Card>();
public void TakeCard(Card card)
{
if (card != null)
_cards.Add(card);
}
public List<Card> GiveCards()
{
return _cards.ToList();
}
}
public class Renderer
{
public void DrawFrontCard(Card card)
{
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = card.DictionarySuits.Value;
Console.Write($"{card.Nominal}{card.DictionarySuits.Key}");
Console.ResetColor();
Console.Write(" ");
}
private void DrawBackCard()
{
Console.BackgroundColor = ConsoleColor.White;
Console.ForegroundColor = ConsoleColor.DarkBlue;
Console.Write("§§§");
Console.ResetColor();
Console.Write(" ");
}
public void DrawFrontCards(List<Card> cards)
{
int amountCardForLine = 13;
for (int i = 0; i < cards.Count; i++)
{
if (i % amountCardForLine == 0)
Console.Write("\n\n");
DrawFrontCard(cards[i]);
}
}
public void DrawBackCards(List<Card> cards)
{
int amountCardForLine = 13;
for (int i = 0; i < cards.Count; i++)
{
if (i % amountCardForLine == 0)
Console.Write("\n\n");
DrawBackCard();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment