TwinFrame

DeckOfCards

Mar 20th, 2022 (edited)
464
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.06 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Clight_55_DeckOfCards
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.             char[] suits = new char[4] { '@', '#', '$', '&' };
  14.             int[] nomanals = new int[] { 1, 2, 3, 4, 5, };
  15.             bool isWorks = true;
  16.  
  17.             Player player = new Player("Bot");
  18.             Random random = new Random();
  19.  
  20.             while (isWorks)
  21.             {
  22.  
  23.                 Console.CursorVisible = false;
  24.                 Console.Clear();
  25.  
  26.                 Console.Write($"Колода игрока {player.Name}: ");
  27.                 player.ShowDeck();
  28.                 Console.WriteLine("\n");
  29.                 Console.WriteLine("F1 - Взять карту.");
  30.                 Console.WriteLine("F2 - Остановиться.");
  31.  
  32.                 ConsoleKeyInfo key = Console.ReadKey();
  33.  
  34.                 switch (key.Key)
  35.                 {
  36.                     case ConsoleKey.F1:
  37.                         TakeCard(player, suits, nomanals, random);
  38.                         break;
  39.  
  40.                     case ConsoleKey.F2:
  41.                         isWorks = false;
  42.                         break;
  43.  
  44.                     default:
  45.                         break;
  46.                 }
  47.             }
  48.  
  49.             Console.Clear();
  50.             Console.Write($"Итоговая колода игрока {player.Name}: ");
  51.             player.ShowDeck();
  52.  
  53.             Console.ReadKey();
  54.         }
  55.  
  56.         public static void TakeCard(Player player, char[] suits, int[] nomanals, Random random)
  57.         {
  58.             Card tempCard = GetRandomCart(suits, nomanals, random);
  59.             player.AddCart(tempCard);
  60.  
  61.             Console.Write("\nВыпала карта: ");
  62.             tempCard.ShowCard();
  63.             Console.ForegroundColor = ConsoleColor.Yellow;
  64.             Console.WriteLine("\n\nДля продолжения нажмите любую клавишу.");
  65.             Console.ForegroundColor = ConsoleColor.Gray;
  66.             Console.ReadKey();
  67.         }
  68.  
  69.         public static Card GetRandomCart(char[] suits, int[] nomanals, Random random)
  70.         {
  71.             int numberSuit = random.Next(0, suits.Length);
  72.             int numberNomanal = random.Next(0, nomanals.Length);
  73.  
  74.             Card card = new Card(suits[numberSuit], nomanals[numberNomanal]);
  75.  
  76.             return card;
  77.         }
  78.  
  79.         public class Player
  80.         {
  81.             private Deck _deck;
  82.  
  83.             public string Name { get; private set; }
  84.  
  85.             public Player(string name)
  86.             {
  87.                 Name = name;
  88.  
  89.                 _deck = new Deck();
  90.             }
  91.  
  92.             public void AddCart(Card card)
  93.             {
  94.                 _deck.AddCart(card);
  95.             }
  96.  
  97.             public void ShowDeck()
  98.             {
  99.                 _deck.ShowDeck();
  100.             }
  101.         }
  102.  
  103.         public class Deck
  104.         {
  105.             private Card[] _cards;
  106.             private Card[] _tempCards;
  107.  
  108.             public Deck()
  109.             {
  110.                 _cards = new Card[0];
  111.             }
  112.  
  113.             public void AddCart(Card card)
  114.             {
  115.                 _tempCards = new Card[_cards.Length + 1];
  116.  
  117.                 _cards.CopyTo(_tempCards, 0);
  118.  
  119.                 _tempCards[_tempCards.Length - 1] = card;
  120.  
  121.                 _cards = new Card[_tempCards.Length];
  122.  
  123.                 _tempCards.CopyTo(_cards, 0);
  124.             }
  125.  
  126.             public void ShowDeck()
  127.             {
  128.                 foreach (var card in _cards)
  129.                 {
  130.                     card.ShowCard();
  131.                     Console.Write(" ");
  132.                 }
  133.             }
  134.         }
  135.  
  136.         public class Card
  137.         {
  138.             private char _suit;
  139.             private int _nomanal;
  140.  
  141.             public Card(char suit, int nomanal)
  142.             {
  143.                 _suit = suit;
  144.  
  145.                 _nomanal = nomanal;
  146.             }
  147.  
  148.             public void ShowCard()
  149.             {
  150.                 Console.Write("{0}-{1}", _suit, _nomanal);
  151.             }
  152.         }
  153.     }
  154. }
Add Comment
Please, Sign In to add comment