vendredi 30 avril 2021

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

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;
}

I am using two other functions to populate the original Deck list and check what cards do all the lists receive.

I Have seen that only half of the values are being shuffled.
Please help as I have seen all the other similar questions on here but they are not helpful in this situation.




Aucun commentaire:

Enregistrer un commentaire