Advertisement
Vapio

task15

Jul 28th, 2022 (edited)
1,017
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 5.28 KB | None | 0 0
  1. using System;
  2.  
  3. public class Program
  4. {
  5.     public static void Main(string[] args)
  6.     {
  7.         Random random = new Random();
  8.  
  9.         Deck deck = new Deck(random);
  10.         Player player = new Player();
  11.  
  12.         bool isRun = true;
  13.  
  14.         string answerPositive = "yes";
  15.         string answerNegative = "no";
  16.  
  17.         while(isRun)
  18.         {
  19.             Console.WriteLine("\nYou have {0} cards in your deck.", player.AmountCards);
  20.             Console.WriteLine("{0} cards in deck.", deck.AmountCards);
  21.             Console.WriteLine("Do you want take a card?");
  22.             Console.WriteLine("1. {0}.", answerPositive);
  23.             Console.WriteLine("2. {0}.", answerNegative);
  24.  
  25.             string answer = Console.ReadLine();
  26.             answer = answer.ToLower();
  27.  
  28.             if(answer == answerPositive)
  29.             {
  30.                 if(deck.IsOver)
  31.                 {
  32.                     Console.WriteLine("Deck is Empty!");
  33.                     isRun = false;
  34.                 }
  35.                 else
  36.                 {
  37.                     Card card = deck.GiveCard();
  38.                     player.AddCard(card);
  39.                 }
  40.             }
  41.             else if(answer == answerNegative)
  42.             {
  43.                 isRun = false;
  44.             }
  45.             else
  46.             {
  47.                 Console.WriteLine("\nWrong answer");
  48.             }
  49.         }
  50.  
  51.         player.PrintAllCards();
  52.     }
  53. }
  54.  
  55. public enum CardTypes : int
  56. {
  57.     Queen = 0,
  58.     King = 1,
  59.     Ace = 2,
  60.     Jack = 3,
  61.     Normal
  62. };
  63.  
  64. public enum SuitTypes
  65. {
  66.     Spades,
  67.     Hearts,
  68.     Diamonds,
  69.     Clubs
  70. }
  71.  
  72. public class Card
  73. {
  74.     public const int NumberDefault = 0;
  75.  
  76.     public CardTypes Type { get; private set; }
  77.     public SuitTypes Suit { get; private set; }
  78.     public int Number { get; private set; }
  79.  
  80.     public Card(SuitTypes suit, CardTypes type, int number)
  81.     {
  82.         Type = type;
  83.         Suit = suit;
  84.         Number = number;
  85.     }
  86. }
  87.  
  88. public class Deck
  89. {
  90.     private List<Card> _cards;
  91.     private Random _random;
  92.  
  93.     public bool IsOver
  94.     {
  95.         get
  96.         {
  97.             return _cards.Count == 0;
  98.         }
  99.     }
  100.  
  101.     public int AmountCards
  102.     {
  103.         get
  104.         {
  105.             return _cards.Count;
  106.         }
  107.     }
  108.  
  109.     public Deck(Random random)
  110.     {
  111.         _cards = new List<Card>();
  112.         _random = random;
  113.         CreateDeck();
  114.         Shuffle();
  115.     }
  116.  
  117.     public Card GiveCard()
  118.     {
  119.         int indexStart = 0;
  120.         Card card = null;
  121.  
  122.         if(_cards.Count > 0)
  123.         {
  124.             card = _cards[indexStart];
  125.             _cards.RemoveAt(indexStart);
  126.         }
  127.  
  128.         return card;
  129.     }
  130.  
  131.     private void CreateDeck()
  132.     {
  133.         CardTypes[] cardsTypes = { CardTypes.Queen, CardTypes.King, CardTypes.Ace, CardTypes.Jack };
  134.         SuitTypes[] cardsSuits = { SuitTypes.Spades, SuitTypes.Hearts, SuitTypes.Diamonds, SuitTypes.Clubs };
  135.  
  136.         int numberNormalCardsStart = 2;
  137.         int numberNormalCardsEnd = 10;
  138.  
  139.         for(int typeIndex = 0; typeIndex < cardsTypes.Length; ++typeIndex)
  140.         {
  141.             for(int suitIndex = 0; suitIndex < cardsSuits.Length; ++suitIndex)
  142.             {
  143.                 Card card = new Card(cardsSuits[suitIndex], cardsTypes[typeIndex], Card.NumberDefault);
  144.                 _cards.Add(card);
  145.             }
  146.         }
  147.  
  148.         for(int suitIndex = 0; suitIndex < cardsSuits.Length; ++suitIndex)
  149.         {
  150.             for(int cardNumber = numberNormalCardsStart; cardNumber <= numberNormalCardsEnd; ++cardNumber)
  151.             {
  152.                 Card card = new Card(cardsSuits[suitIndex], CardTypes.Normal, cardNumber);
  153.                 _cards.Add(card);
  154.             }
  155.         }
  156.     }
  157.  
  158.     private void Shuffle()
  159.     {
  160.         for(int cardIndex = 0; cardIndex < _cards.Count; ++cardIndex)
  161.         {
  162.             int numberShuffle = _random.Next(_cards.Count);
  163.             Card cardSwap = _cards[cardIndex];
  164.             _cards[cardIndex] = _cards[numberShuffle];
  165.             _cards[numberShuffle] = cardSwap;
  166.         }
  167.     }
  168. }
  169.  
  170. public class Player
  171. {
  172.     private List<Card> _cards;
  173.  
  174.     public int AmountCards
  175.     {
  176.         get
  177.         {
  178.             return _cards.Count;
  179.         }
  180.     }
  181.  
  182.     public Player()
  183.     {
  184.         _cards = new List<Card>();
  185.     }
  186.  
  187.     public void AddCard(Card card)
  188.     {
  189.         _cards.Add(card);
  190.     }
  191.  
  192.     public void PrintAllCards()
  193.     {
  194.         string[] cardTypesNames = { "Queen", "King", "Ace", "Jack" };
  195.  
  196.         Console.WriteLine("\nAll cards in your deck : ");
  197.         Console.WriteLine("Amount of cards : " + _cards.Count);
  198.  
  199.         string cardOutput;
  200.         int spacesAmount = 2;
  201.  
  202.         for(int cardIndex = 0; cardIndex < _cards.Count; ++cardIndex)
  203.         {
  204.             if(_cards[cardIndex].Type == CardTypes.Normal)
  205.                 cardOutput = String.Format("{0," + spacesAmount + "}. {1} {2}",
  206.                                              cardIndex, _cards[cardIndex].Number, _cards[cardIndex].Suit);
  207.             else
  208.                 cardOutput = String.Format("{0, " + spacesAmount + "}. {1} {2} ",
  209.                                              cardIndex, cardTypesNames[(int) _cards[cardIndex].Type], _cards[cardIndex].Suit);
  210.  
  211.             Console.WriteLine(cardOutput);
  212.         }
  213.     }
  214. }
  215.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement