经典的纸牌/扑克游戏问题。我为每张卡(CardClubs、CardDiamond 等)都有课程。我有一个 Rank 的枚举(Deuce = 2 等)。在我的 CardSet 类中,我有构造函数:
public SuperCard[] cardArray;
public Random myRandom = new Random();
#region Constructor
public CardSet()
{
cardArray = new SuperCard[52];
for (int i = 0; i < 13; i++)
{
cardArray[i] = new CardClubs((Rank)i + 1);
cardArray[i + 13] = new CardDiamond((Rank)i+1);
cardArray[i + 26] = new CardHeart((Rank)i + 1); ;
cardArray[i + 39] = new CardSpades((Rank)i + 1); ;
}
}
#endregion
我有 GetCards 方法,它传入要获取的卡片数量。我在顶部创建了公共随机数。在 Program.cs 中调用 GetCards,如下所示:
int howManyCards = 5; // can be whatever. used this instead of # for debug purposes
SuperCard[] computerHand = myDeck.GetCards(howManyCards); // create two hands, user/comp
SuperCard[] myHand = myDeck.GetCards(howManyCards);
我不明白如何使用随机生成随机卡。这是我当前的 GetCard 方法(它不工作)也来自 CardSet 类。现在,如果它生成相同的卡片就可以了,只是想了解随机选择的基础知识。
public SuperCard[] GetCards(int number)
{
SuperCard[] hand = new SuperCard[number];
for (int i = 0; i < number; i++)
{
hand[i] = cardArray[myRandom]((Rank)i + 1);
}
return hand;
}