Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- public class Program
- {
- public static void Main(string[] args)
- {
- Random random = new Random();
- Deck deck = new Deck(random);
- Player player = new Player();
- bool isRun = true;
- string answerPositive = "yes";
- string answerNegative = "no";
- while(isRun)
- {
- Console.WriteLine("\nYou have {0} cards in your deck.", player.AmountCards);
- Console.WriteLine("{0} cards in deck.", deck.AmountCards);
- Console.WriteLine("Do you want take a card?");
- Console.WriteLine("1. {0}.", answerPositive);
- Console.WriteLine("2. {0}.", answerNegative);
- string answer = Console.ReadLine();
- answer = answer.ToLower();
- if(answer == answerPositive)
- {
- if(deck.IsOver)
- {
- Console.WriteLine("Deck is Empty!");
- isRun = false;
- }
- else
- {
- Card card = deck.GiveCard();
- player.AddCard(card);
- }
- }
- else if(answer == answerNegative)
- {
- isRun = false;
- }
- else
- {
- Console.WriteLine("\nWrong answer");
- }
- }
- player.PrintAllCards();
- }
- }
- public enum CardTypes : int
- {
- Queen = 0,
- King = 1,
- Ace = 2,
- Jack = 3,
- Normal
- };
- public enum SuitTypes
- {
- Spades,
- Hearts,
- Diamonds,
- Clubs
- }
- public class Card
- {
- public const int NumberDefault = 0;
- public CardTypes Type { get; private set; }
- public SuitTypes Suit { get; private set; }
- public int Number { get; private set; }
- public Card(SuitTypes suit, CardTypes type, int number)
- {
- Type = type;
- Suit = suit;
- Number = number;
- }
- }
- public class Deck
- {
- private List<Card> _cards;
- private Random _random;
- public bool IsOver
- {
- get
- {
- return _cards.Count == 0;
- }
- }
- public int AmountCards
- {
- get
- {
- return _cards.Count;
- }
- }
- public Deck(Random random)
- {
- _cards = new List<Card>();
- _random = random;
- CreateDeck();
- Shuffle();
- }
- public Card GiveCard()
- {
- int indexStart = 0;
- Card card = null;
- if(_cards.Count > 0)
- {
- card = _cards[indexStart];
- _cards.RemoveAt(indexStart);
- }
- return card;
- }
- private void CreateDeck()
- {
- CardTypes[] cardsTypes = { CardTypes.Queen, CardTypes.King, CardTypes.Ace, CardTypes.Jack };
- SuitTypes[] cardsSuits = { SuitTypes.Spades, SuitTypes.Hearts, SuitTypes.Diamonds, SuitTypes.Clubs };
- int numberNormalCardsStart = 2;
- int numberNormalCardsEnd = 10;
- for(int typeIndex = 0; typeIndex < cardsTypes.Length; ++typeIndex)
- {
- for(int suitIndex = 0; suitIndex < cardsSuits.Length; ++suitIndex)
- {
- Card card = new Card(cardsSuits[suitIndex], cardsTypes[typeIndex], Card.NumberDefault);
- _cards.Add(card);
- }
- }
- for(int suitIndex = 0; suitIndex < cardsSuits.Length; ++suitIndex)
- {
- for(int cardNumber = numberNormalCardsStart; cardNumber <= numberNormalCardsEnd; ++cardNumber)
- {
- Card card = new Card(cardsSuits[suitIndex], CardTypes.Normal, cardNumber);
- _cards.Add(card);
- }
- }
- }
- private void Shuffle()
- {
- for(int cardIndex = 0; cardIndex < _cards.Count; ++cardIndex)
- {
- int numberShuffle = _random.Next(_cards.Count);
- Card cardSwap = _cards[cardIndex];
- _cards[cardIndex] = _cards[numberShuffle];
- _cards[numberShuffle] = cardSwap;
- }
- }
- }
- public class Player
- {
- private List<Card> _cards;
- public int AmountCards
- {
- get
- {
- return _cards.Count;
- }
- }
- public Player()
- {
- _cards = new List<Card>();
- }
- public void AddCard(Card card)
- {
- _cards.Add(card);
- }
- public void PrintAllCards()
- {
- string[] cardTypesNames = { "Queen", "King", "Ace", "Jack" };
- Console.WriteLine("\nAll cards in your deck : ");
- Console.WriteLine("Amount of cards : " + _cards.Count);
- string cardOutput;
- int spacesAmount = 2;
- for(int cardIndex = 0; cardIndex < _cards.Count; ++cardIndex)
- {
- if(_cards[cardIndex].Type == CardTypes.Normal)
- cardOutput = String.Format("{0," + spacesAmount + "}. {1} {2}",
- cardIndex, _cards[cardIndex].Number, _cards[cardIndex].Suit);
- else
- cardOutput = String.Format("{0, " + spacesAmount + "}. {1} {2} ",
- cardIndex, cardTypesNames[(int) _cards[cardIndex].Type], _cards[cardIndex].Suit);
- Console.WriteLine(cardOutput);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement