【发布时间】: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 个被选中。是的,我想为自己的教育目的编写代码,这就是为什么我避免看到已经给出的解决方案,但我没有更多的想法。