Добавить карту в массив - C#

Узнай цену своей работы

Формулировка задачи:

Привет! Изучаю классы. Как добавить карту в колоду?
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. namespace Poker
  7. {
  8. class Program
  9. {
  10. static void Main()
  11. {
  12. Console.WriteLine();
  13. Console.ReadKey();
  14. }
  15. public class Card
  16. {
  17. public int Rank;
  18. public int Suit;
  19. }
  20. public class Deck
  21. {
  22. public Deck[] deck = new Deck[52];
  23. public Card card = new Card();
  24. public void CreateDeck()
  25. {
  26. for(int a = 0; a < 4; a++)
  27. {
  28. for(int b = 0; b < 13; b++)
  29. {
  30. card.Suit = a;
  31. card.Rank = b;
  32. }
  33. }
  34. }
  35. }

Решение задачи: «Добавить карту в массив»

textual
Листинг программы
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Poker
  8. {
  9.     class Program
  10.     {
  11.         static void Main()
  12.         {
  13.             Deck deck = new Deck();
  14.             deck.ShuffleDeck();
  15.  
  16.             for(int a = 0; a < 52; a++)
  17.             {
  18.                 Console.Write("{0} ", deck.Deal());
  19.             }
  20.  
  21.             Console.ReadKey();
  22.         }
  23.  
  24.         public class Card
  25.         {
  26.             private string rank, suit;
  27.            
  28.             public Card (string Rank, string Suit)
  29.             {
  30.                 rank = Rank;
  31.                 suit = Suit;
  32.             }
  33.  
  34.             public override string ToString()
  35.             {
  36.                 return rank + suit;
  37.             }
  38.         }
  39.  
  40.         public class Deck
  41.         {
  42.             private Card[] deck;
  43.             private int currentCard;
  44.             private const int ALL_CARDS = 52;
  45.             private Random random;
  46.  
  47.             public Deck()
  48.             {
  49.                 string[] ranks = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
  50.                 string[] suits = { "c", "h", "s", "d" };
  51.  
  52.                 deck = new Card[ALL_CARDS];
  53.                 currentCard = 0;
  54.                 random = new Random();
  55.  
  56.                 for(int a = 0; a < deck.Length; a++)
  57.                 {
  58.                     deck[a] = new Card(ranks[a % 13], suits[a / 13]);
  59.                 }
  60.             }
  61.             public void ShuffleDeck()
  62.             {
  63.                 currentCard = 0;
  64.  
  65.                 for (int a = 0; a < deck.Length; a++)
  66.                 {
  67.                     int b = random.Next(ALL_CARDS);
  68.  
  69.                     Card temp = deck[a];
  70.                     deck[a] = deck[b];
  71.                     deck[b] = temp;
  72.                 }
  73.             }
  74.  
  75.             public Card Deal()
  76.             {
  77.                 if (currentCard < deck.Length)
  78.                     return deck[currentCard++];
  79.                 else
  80.                     return null;
  81.             }
  82.  
  83.         }
  84.  
  85.     }
  86. }

ИИ поможет Вам:


  • решить любую задачу по программированию
  • объяснить код
  • расставить комментарии в коде
  • и т.д
Попробуйте бесплатно

Оцени полезность:

15   голосов , оценка 3.667 из 5

Нужна аналогичная работа?

Оформи быстрый заказ и узнай стоимость

Бесплатно
Оформите заказ и авторы начнут откликаться уже через 10 минут