Skip to content

Instantly share code, notes, and snippets.

@Strelok78
Last active June 13, 2025 19:09
Show Gist options
  • Save Strelok78/57a4378800dcdf1f5aa72addfdc88a72 to your computer and use it in GitHub Desktop.
Save Strelok78/57a4378800dcdf1f5aa72addfdc88a72 to your computer and use it in GitHub Desktop.
Card game
using System;
using System.IO;
using iJuniorPractice;
namespace iJuniorPractice
{
enum Suit
{
Hearts,
Diamonds,
Clubs,
Spades
}
enum Value
{
Ace = 1,
Two = 2,
Three = 3,
Four = 4,
Five = 5,
Six = 6,
Seven = 7,
Eight = 8,
Nine = 9,
Ten = 10,
Jack = 11,
Queen = 12,
King = 13
}
class Program
{
static void Main(string[] args)
{
GameController gameController = new GameController();
gameController.Play();
}
}
static class Utils
{
public static int ReadInt(string message = "")
{
int command;
string input;
Console.WriteLine(message);
input = Console.ReadLine();
while (int.TryParse(input, out command) == false)
{
Console.WriteLine("Incorrect input");
input = Console.ReadLine();
}
return command;
}
}
class GameController
{
private const int CommandGiveCards = 1;
private const int CommandShowPlayerCards = 2;
private const int CommandExit = 3;
private bool _isPlay = true;
public void Play()
{
Player player = new Player();
Croupier croupier = new Croupier(player);
int input;
while (_isPlay)
{
ShowMenu();
input = Utils.ReadInt("Please enter command: ");
switch (input)
{
case CommandGiveCards:
croupier.GiveCards();
break;
case CommandShowPlayerCards:
player.ShowPlayCards();
break;
case CommandExit:
End();
break;
default:
Console.WriteLine("Incorrect input.");
break;
}
Console.WriteLine("Press any key to continue...");
Console.ReadKey();
Console.Clear();
}
}
private void ShowMenu()
{
Console.WriteLine($"Main Menu:\n" +
$"Take card - {CommandGiveCards}\n" +
$"Show Cards in hand - {CommandShowPlayerCards}\n" +
$"Exit - {CommandExit}");
}
private void End()
{
_isPlay = false;
Console.WriteLine("Game is over.");
}
}
class Croupier
{
private Deck _deck;
private Player _player;
public Croupier(Player player)
{
_player = player;
_deck = new Deck();
}
public void GiveCards()
{
int cardsCount = Utils.ReadInt("Please enter number of cards: ");
if (_deck.GetCardsCount() == 0)
{
Console.WriteLine("No cards left in deck");
return;
}
if (cardsCount > _deck.GetCardsCount() && _deck.GetCardsCount() > 0)
{
Console.WriteLine($"In deck left only {_deck.GetCardsCount()} cards.");
return;
}
Card[] cards = new Card[cardsCount];
for (int i = 0; i < cardsCount; i++)
{
Card randomCard = _deck.GetRandomCard();
cards[i] = randomCard;
}
_player.RecieveCards(cards);
}
}
class Player
{
private List<Card> _cards;
public Player()
{
_cards = new List<Card>();
}
public void RecieveCards(Card[] cards)
{
for (int i = 0; i < cards.Length; i++)
{
_cards.Add(cards[i]);
}
ShowPlayCards();
}
public void ShowPlayCards()
{
foreach (var card in _cards)
{
card.WriteCardInfo();
}
}
}
class Deck
{
private int _deckMaxSize = 52;
private List<Card> _cards;
public Deck()
{
_cards = new List<Card>();
FillTheDeck();
}
private void FillTheDeck()
{
foreach (Suit suit in Enum.GetValues(typeof(Suit)))
{
foreach (Value value in Enum.GetValues(typeof(Value)))
{
var card = new Card(suit, value);
_cards.Add(card);
}
}
}
private void RemoveCard(Card cardToRemove)
{
_cards.Remove(cardToRemove);
}
public int GetCardsCount()
{
return _cards.Count;
}
public Card GetRandomCard()
{
Random random = new Random();
Card randomCard = _cards[random.Next(_cards.Count)];
RemoveCard(randomCard);
return randomCard;
}
}
class Card
{
public Suit Suit { get; private set; }
public Value Value { get; private set; }
public Card(Suit suit, Value value)
{
Suit = suit;
Value = value;
}
public void WriteCardInfo()
{
Console.WriteLine($"{Suit}: {Value}");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment