[英]c# Blackjack - Need Help
This C# program is suppose to be a blackjack program but I need to 'SHUFFLE' the card and just display the 'HAND' 该C#程序假定是二十一点程序,但我需要“装卸”卡并仅显示“ HAND”
The rest i think I can manage... can someone help me? 其余的我认为我可以解决...有人可以帮助我吗?
One way to shuffle is to create new array and move the cards into that array in a random order 随机播放的一种方法是创建新阵列,然后将卡以随机顺序移动到该阵列中
List<Card> unshuffled = new List<Card>(pack);
pack = new Card[NUM_CARDS];
Random r = new Random()
for(int card = 0; card < NUM_CARDS; card++)
{
pack[card] = unshuffled[r.Next(0, unshuffled.Count -1)];
unshuffled.remove(pack[card]);
}
You might want to make the Random
instance global, as creating a new random each time reduces entropy somewhat. 您可能想使Random
实例成为全局实例,因为每次创建一个新的Random都会一定程度地减少熵。 This might not be important if you are not shuffling a lot. 如果您不经常改组,这可能并不重要。
I would recommend just switching each element to random other, heres how: 我建议仅将每个元素切换为随机元素,方法如下:
private void switchElements(Card[] pack, int nr_1, int nr_2) {
Card temp = pack[nr_1];
pack[nr_1] = pack[nr_2];
pack[nr_2] = temp;
}
public void shuffle(Card[] pack) {
for (int i = pack.length - 1; i > 0; i--)
switchElements(pack, i,random.Next(0,i));
}
您可以使用以下简单的方法“拖曳”包装:
var shuffled = pack.OrderBy(c => random.NextDouble());
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.