Добавить карту в массив - C#
Формулировка задачи:
Привет! Изучаю классы. Как добавить карту в колоду?
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Poker
- {
- class Program
- {
- static void Main()
- {
- Console.WriteLine();
- Console.ReadKey();
- }
- public class Card
- {
- public int Rank;
- public int Suit;
- }
- public class Deck
- {
- public Deck[] deck = new Deck[52];
- public Card card = new Card();
- public void CreateDeck()
- {
- for(int a = 0; a < 4; a++)
- {
- for(int b = 0; b < 13; b++)
- {
- card.Suit = a;
- card.Rank = b;
- }
- }
- }
- }
Решение задачи: «Добавить карту в массив»
textual
Листинг программы
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- namespace Poker
- {
- class Program
- {
- static void Main()
- {
- Deck deck = new Deck();
- deck.ShuffleDeck();
- for(int a = 0; a < 52; a++)
- {
- Console.Write("{0} ", deck.Deal());
- }
- Console.ReadKey();
- }
- public class Card
- {
- private string rank, suit;
- public Card (string Rank, string Suit)
- {
- rank = Rank;
- suit = Suit;
- }
- public override string ToString()
- {
- return rank + suit;
- }
- }
- public class Deck
- {
- private Card[] deck;
- private int currentCard;
- private const int ALL_CARDS = 52;
- private Random random;
- public Deck()
- {
- string[] ranks = { "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K", "A" };
- string[] suits = { "c", "h", "s", "d" };
- deck = new Card[ALL_CARDS];
- currentCard = 0;
- random = new Random();
- for(int a = 0; a < deck.Length; a++)
- {
- deck[a] = new Card(ranks[a % 13], suits[a / 13]);
- }
- }
- public void ShuffleDeck()
- {
- currentCard = 0;
- for (int a = 0; a < deck.Length; a++)
- {
- int b = random.Next(ALL_CARDS);
- Card temp = deck[a];
- deck[a] = deck[b];
- deck[b] = temp;
- }
- }
- public Card Deal()
- {
- if (currentCard < deck.Length)
- return deck[currentCard++];
- else
- return null;
- }
- }
- }
- }
ИИ поможет Вам:
- решить любую задачу по программированию
- объяснить код
- расставить комментарии в коде
- и т.д