3

我正在设计一款名为 Snap 的纸牌游戏(这里有一个显示规则的链接http://boardgames.about.com/od/cardgames/a/snap.htm),在我的版本中,玩家必须点击如果一对出现在中间。我目前有 4 个类,一个用于卡片(这是一个名为 cardValue_ 的 int),一个用于玩家手牌,一个用于原始纸牌,一个用于中间的一堆卡片。所以 Deck、Pile 和 Hand 类中有一个卡片列表。我现在正在尝试为包含卡片列表的 Deck 类编写一个 shuffle 方法。它将随机选择一张卡片并将其移动到新列表,直到所有卡片都被挑选出来,然后将它们移回原始列表,从而进行简单的洗牌。到目前为止,我的方法看起来像这样......

public List<Deck> Shuffle(List<Card> cardDeck)
    {
        int index = 0;
        Random randomCard = new Random();
        List<Card> newDeck = new List<Card>();

        while (index < cardDeck.Count)
        {
            int ran = randomCard.Next(0, cardDeck.Count);
            foreach (Card card in cardDeck)
            {

            }
        }
    }

我正在尝试找出 foreach 循环中应该做什么(除非整个方法是错误的),但现在我想我已经在错误的地方声明了我的所有卡片,所有 52 张卡片当前都在表单中声明,或者应该我要在 Deck 类中声明它们?

4

2 回答 2

2

You where fairly close to how I would solve it, what I would do is copy out of the source list randomly until it is empty and then just refill it. You don't need to return a list as this will just shuffle the list you pass in.

//Move this out of the function, if you are wondering why search SO for "Not random" and choose any of the 100's of people asking "why is random not random?" :)
private static Random randomCard = new Random(); //Not thread safe, if multi-threading use locks!!!

public static void Shuffle(List<Card> cardDeck)
{
    int index = 0;
    List<Card> tempDeck = new List<Card>();

    while (cardDeck.Count > 0)
    {
        int removal = randomCard.Next(0, cardDeck.Count);
        Card tempCard = cardDeck[removal];
        cardDeck.RemoveAt(removal);
        tempDeck.Add(tempCard);
    }

    //cardDeck is empty at this point, now we refill it with our randomized deck.
    cardDeck.AddRange(tempDeck);
}

If you want to not modify the original list and you do want a new randomized list just copy the source list first.

public static List<Card> Shuffle(List<Card> cardDeck)
{
    int index = 0;
    List<Card> tempDeck = new List<Card>();
    List<Card> localCopy = new List<Card>(cardDeck);   //Creates a shallow copy of the list.      

    while (localCopy.Count > 0)
    {
        int removal = randomCard.Next(0, cardDeck.Count);
        Card tempCard = localCopy[removal];
        localCopy.RemoveAt(removal);
        tempDeck.Add(tempCard);
    }

    return tempDeck;
}

I would recommend using Richard's method. it is much simpler.

于 2013-02-08T04:58:38.770 回答
2

阅读 Jeff 的关于洗牌的博客以了解所有详细信息。

public List<Card> Shuffle(List<Card> cards)
{
  return new List<Card>(cards)
   .OrderBy(a => Guid.NewGuid());
}

更新

Scott 建议 Guid 可能不够随机,而 Crypto RNG 会更好。所以使用 BigInteger 因为它实现了 IComparable 我们得到:

RNGCryptoServiceProvider rng = new RNGCryptoServiceProvider();

public List<Card> Shuffle(List<Card> cards)
{
  var r = new byte[32];
  return new List<Card>(cards)
   .OrderBy(a => new BigInteger(rng.GetBytes(r));
}
于 2013-02-08T05:15:38.673 回答