【问题标题】:I want to randomly shuffle a List containg 10 cards(will increase later to 52) using c#. But i am having problems.Only half of them are beingshuffled我想使用 c# 随机洗牌包含 10 张牌的列表(稍后将增加到 52 张)。但我有问题。只有一半被洗牌
【发布时间】:2021-04-30 11:35:20
【问题描述】:
static List<List<int>> CardDistribution(List<int> Deck,List<int> PlayerCard, List<int> OpponentCard)
{
    int TotsalCards = Deck.Count;
    int CardTaken;
    int IndexofCardTaken;
    int value;

    Random CardTaker = new Random();

    for (int PlC = 0; PlC < TotsalCards; PlC++)
    {
        TotsalCards--;
        if (TotsalCards > 1)
        {
            if (PlC % 2 == 0)
            {
            Redo:
                IndexofCardTaken = CardTaker.Next(0, TotsalCards + 1);
                CardTaken = Deck[IndexofCardTaken];
                if (PlayerCard.Contains(CardTaken))
                {
                    goto Redo;
                }
                else
                {
                    PlayerCard.Add(CardTaken);
                    value = Deck[IndexofCardTaken];
                    Deck[IndexofCardTaken] = Deck[TotsalCards];
                    Deck[TotsalCards] = value;
                }
            }
            else if (PlC % 2 != 0)
            {
            Redo:
                IndexofCardTaken = CardTaker.Next(0, TotsalCards + 1);
                CardTaken = Deck[IndexofCardTaken];
                if (OpponentCard.Contains(CardTaken))
                {
                    goto Redo;
                }
                else
                {
                    OpponentCard.Add(CardTaken);
                    value = Deck[IndexofCardTaken];
                    Deck[IndexofCardTaken] = Deck[TotsalCards];
                    Deck[TotsalCards] = value;
                }
            }
        }
        else if(TotsalCards == 1)
        {
            OpponentCard.Add(Deck[0]);
        }
    }
    List<List<int>> ReturnDecks = new List<List<int>>();
    ReturnDecks.Add(PlayerCard);
    ReturnDecks.Add(OpponentCard);
    return ReturnDecks;
}

我正在使用另外两个函数来填充原始 Deck 列表并检查所有列表都收到了哪些卡片。

我看到只有一半的值被洗牌。
请帮忙,因为我在这里看到了所有其他类似的问题,但它们在这种情况下没有帮助。

【问题讨论】:

  • 哪一半?上半场?下半场?每隔一个?
  • 洗牌是一个已解决的问题。您是否考虑过使用one of the existing solutions,或者您是否想出于教育目的自行推出?
  • 其中一个问题可能是您在循环开始时有TotsalCards--,这也是用于确定循环何时结束的值。因此,如果 TotsalCards 从 10 开始,那么在 5 次迭代之后它将是 5,而 PlC(每个循环都会递增)将是 5,并且您的循环将完成。几乎可以肯定不是你想要发生的事情。正如 Heinzi 所说,尽管您可能想使用现有的良好的改组实现,或者如果您想编写自己的,至少可以查看它们以了解如何做到这一点。
  • 另外让我感到震惊的是,如果您调试了代码,您很容易发现循环没有像您预期的那样运行多次,并且您可以确切地查看原因。学习调试。从长远来看,它会为您节省很多时间。
  • 在随机选择的数字中,只有 5 个被选中。是的,我想为自己的教育目的编写代码,这就是为什么我避免看到已经给出的解决方案,但我没有更多的想法。

标签: c# list random


【解决方案1】:

洗牌的通用方法称为Fisher Yates Shuffle,很容易在List&lt;T&gt;上实现

static Random rnd = new Random();

public static void Shuffle<T>(List<T> list)
{
    for(var i=0;i<list.Count-2;i++) 
    {
        var j = rnd.Next(i, list.Count);
        var temp = list[i];
        list[i] = list[j];
        list[j] = temp;
    }
}

只需将您的列表传递给该方法,它就会对列表进行洗牌。它适用于 List&lt;int&gt;List&lt;Card&gt; 或您能想到的任何其他列表。

实时示例(使用 10 个数字的列表):https://dotnetfiddle.net/UaS3wO

所以我认为你的逻辑(如果我理解正确的话)是

  1. 洗牌
  2. 轮流给玩家和对手和玩家一张牌
  3. 归还玩家和对手套牌

--

static List<List<int>> CardDistribution(List<int> Deck,List<int> PlayerCard, List<int> OpponentCard)
{
    Shuffle(Deck);
    for(var i=0;i<Deck.Count;i++)
    { 
        if((i % 2) == 0) PlayerCard.Add(Deck[i])
        else OpponentCard.Add(Deck[i])
    }
    return new List<List<int>>
    {
         PlayerCard,
         OpponentCard
    }
}

注意:实际上没有必要返回玩家和对手的牌组 - 如果您将空的List&lt;int&gt; 传递给上述方法,它将被填满。所以这也可以:

static void CardDistribution(List<int> Deck,List<int> PlayerCard, List<int> OpponentCard)
{
    Shuffle(Deck);
    for(var i=0;i<Deck.Count;i++)
    { 
        if((i % 2) == 0) PlayerCard.Add(Deck[i])
        else OpponentCard.Add(Deck[i])
    }
}

【讨论】:

  • 侧面观察:交换元素现在可以写成(list[i], list[j]) = (list[j], list[i]);
  • @LasseV.Karlsen 不错。没想到
  • 是的,您正确理解了逻辑。我在使用 Fisher-Yates Shuffle Part 时遇到了问题。我非常感谢你的帮助。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-04-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-09-01
  • 1970-01-01
相关资源
最近更新 更多