纸牌游戏(在主要组中制作随机4组数字) [英] playing cards game (make Random 4 groups of Numbers in Main Group)
问题描述
我想制作纸牌游戏。我给每张卡一个1到52的数字
现在我需要给4个玩家的牌(每个玩家有13张牌)。那么我如何制作随机4组(每组包含13个数字)和数字从1-52并且没有组具有相同的数字?
Hi ,
I want to make playing cards game . I give every card a number from 1 to 52
now I need give cards to 4 players (every player has 13 cards) . so how I can make random 4 groups (every groups contain with 13 number ) and number from 1-52 and no group has the same number ?
推荐答案
最简单的方法是模拟一副真正的卡片:
将你的套牌作为整数列表:
The easiest way is to emulate a real deck of cards:
Take your "deck" as a list of integers:
private List<int> deck = new List<int>();
private const int cardsCount = 52;
private Random rand = new Random();
然后用所有数字填写:
Then fill it with all the numbers:
deck.AddRange(Enumerable.Range(1, cardsCount));
然后写一个shuffle方法:
Then write a shuffle method:
List<int> shuffled = new List<int>();
for (int i = 0; i < cardsCount; i++)
{
int index = rand.Next(deck.Count);
shuffled.Add(deck[index]);
deck.RemoveAt(index);
}
deck = shuffled;
然后你可以从顶部处理卡片并获得保证随机和唯一的价值。
(我可能会创建一个Card类,以便将数字翻译成套装并且值更好,但这取决于你)
You can then deal the "cards" off the top and get guaranteed random and unique values.
(I'd probably create a Card class so that the number translated to a suit and value better, but that's up to you)
诀窍在于:从甲板上随机抽取5张卡片 [ ^ ](C ++
代码,对不起)。
在你的情况下,它应该是这样的:
初始化
- 以这种方式分配数组:
c(0)= 1,c(1) = 2,..,c(51)= 52
。
The trick is here: "Random extraction of 5 cards from a deck"[^] (C++
code, sorry).
In your case, it should work this:
Initalization
- Assign an array this way:
c(0)=1, c(1)=2, .., c(51)=52
.
- 在
0
和51之间选择一个随机数
(包含)说 r 。 - 使用
c(51)
交换c(r)
。
- pick a random number between
0
and51
(included) say r. - swap
c(r)
withc(51)
..
- 在
0
和之间选择一个随机数( r ) 50
(已包含)。 - 使用
c(r)
> c(50)。
- pick a random number (r) between
0
and50
(included). - swap
c(r)
withc(50)
.
它可能与游戏无关,但是OriginalGriff的解决方案1正在做很多额外的工作(在每次迭代时将列表内容移动到被删除元素上方)。使用数组而不是列表 Fisher-Yates shuffle [ ^ ]可以使用由内到外在O(n)中完成version。
It probably is irrelevant for a game, but OriginalGriff's Solution 1 is doing a lot of extra work (moving the list contents above the removed element at each iteration). Using an array instead of a list the Fisher-Yates shuffle[^] can be done in O(n) using the "inside-out" version.
private const int cardsCount = 52;
private Random rand = new Random();
private static int[] deck = Enumerable.Range(1, cardsCount).ToArray();
private static int[] Shuffle(int[] deck)
{
int[] shuffled = new int[deck.Length];
shuffled[0] = deck[0];
for (int i = 1; i < deck.Length; i++)
{
int j = rand.Next(i + 1);
if (j != i)
shuffled[i] = shuffled[j];
shuffled[j] = deck[i];
}
return shuffled;
}
使用类似:
Use like:
int[] shuffled = Shuffle(deck);
=============
编辑:2016-14-6 MTH
这有点旧,但值得注意的是:
如果避免产生shuffles中的偏差很重要,请特别注意Fisher-Yates shuffle:潜在的偏见来源。
(例如,为了有可能产生52卡片组的所有可能的改组,伪随机数发生器必须至少有226位内部状态!)
=============
2016-14-6 MTH
This is a bit old, but it's worth noting:
If avoiding bias in the resultant "shuffles" is important, pay particular attention to the Fisher-Yates shuffle: Potential sources of bias in the Wikipedia article.
(For example, to have the possibility of producing all possible shufflings of a 52 card deck the pseudo random number generator must have at least 226 bits of internal state!)
这篇关于纸牌游戏(在主要组中制作随机4组数字)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!