经典扑克牌/扑克游戏问题。我有每张卡的课程(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方法,它可以传递卡的数量。我创建了公共随机顶部。 GetCards在Program.cs中调用,如下:
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;
}
答案 0 :(得分:0)
使用Random类(http://msdn.microsoft.com/en-us/library/system.random.aspx)
var r = new Random();
r.Next(min,max);