我在C#编码方面还很陌生,在显示并排打乱的卡时遇到了麻烦,而且我不确定是否首先正确地编码了卡和卡座。包括提示在内的所有内容都对您有所帮助!
我的枚举
public enum SuitEnum
{
Hearts,
Diamonds,
Clubs,
Spades
}
public enum ValueEnum
{
Ace = 1,
Two = 2,
Three = 3,
Four = 4,
Five = 5,
Six = 6,
Seven = 7,
Eight = 8,
Nine = 9,
Ten = 10,
Jack = 10,
Queen = 10,
King = 10
}
卡类
public class Card
{
SuitEnum Suit { get; set; }
ValueEnum Value { get; set; }
public Card(SuitEnum cardSuit, ValueEnum cardValue)
{
Suit = cardSuit;
Value = cardValue;
}
}
最后是我的Deck课
public class Deck
{
public List<Card> Cards { get; set; }
public Deck()
{
Cards = new List<Card>();
foreach (SuitEnum suit in Enum.GetValues(typeof(SuitEnum)))
{
foreach (ValueEnum value in Enum.GetValues(typeof(ValueEnum)))
{
Cards.Add(new Card(suit, value));
}
}
}
因此,既然我已经有了卡和卡组代码,那么如何将我的主程序代码中的所有52张卡显示在每次随机播放的位置?我必须在一个类中做另一个方法吗?还是只是在我的主代码中循环?让我知道你的想法。
答案 0 :(得分:1)
我不知道您的应用程序,但是因为您是StackOverflow的新手,所以我更改了一些代码以满足您的洗牌需求。我删除了枚举重复,并在Card类中添加了显示方法。我通过检查重复从给定的范围生成一个随机数。我希望它对您有用。
public enum SuitEnum
{
Hearts,
Diamonds,
Clubs,
Spades
}
public enum ValueEnum
{
Ace = 1,
Two = 2,
Three = 3,
Four = 4,
Five = 5,
Six = 6,
Seven = 7,
Eight = 8,
Nine = 9,
Ten = 10,
Jack = 11,
Queen = 12,
King = 13
}
public class Card
{
SuitEnum Suit { get; set; }
ValueEnum Value { get; set; }
public Card(SuitEnum cardSuit, ValueEnum cardValue)
{
Suit = cardSuit;
Value = cardValue;
}
public string Display()
{
return Suit + "" + Value.ToString();
}
}
public class Deck
{
public List<Card> Cards { get; set; }
public Deck()
{
Random rand = new Random();
Cards = new List<Card>();
List<int> possibleSuit = Enumerable.Range(1, 4).ToList();
List<int> listNumberSuite = new List<int>();
for (int i = 0; i < 4; i++)
{
int index = rand.Next(0, possibleSuit.Count);
listNumberSuite.Add(possibleSuit[index]);
SuitEnum suit = (SuitEnum)possibleSuit[index];
possibleSuit.RemoveAt(index);
List<int> possibleDeck = Enumerable.Range(1, 13).ToList();
List<int> listNumberDeck = new List<int>();
for (int j = 0; j < 13; j++)
{
int indexDeck = rand.Next(0, possibleDeck.Count);
listNumberDeck.Add(possibleDeck[indexDeck]);
ValueEnum deck = (ValueEnum)possibleDeck[indexDeck];
Cards.Add(new Card(suit, deck));
possibleDeck.RemoveAt(indexDeck);
}
}
foreach (Card c in Cards)
{
Console.WriteLine(c.Display());
}
}
}