0
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.

Mathias R. Jessen
  • 157,619
  • 12
  • 148
  • 206
  • 1
    Which half? First half? Second half? Every other? – Mathias R. Jessen Apr 30 '21 at 11:41
  • Shuffling a list is a solved problem. Have you considered using [one of the existing solutions](https://stackoverflow.com/q/273313/87698), or do you want to roll your own for educational purposes? – Heinzi Apr 30 '21 at 11:42
  • 3
    One of the problems is probably the fact that you have `TotsalCards--` at the beginning of your loop and this is also the value used to determine when the loop ends. So if TotsalCards starts at 10 then after 5 iterations it will be 5 and PlC (which is being incremented every loop) will be 5 and your loop will finish. Almost certainly not what you want to happen. As Heinzi says though you probably want to either use an existing good implementation of shuffling or at least look at them for ideas on how to do it if you want to write your own. – Chris Apr 30 '21 at 11:46
  • 2
    Also it strikes me that had you debugged your code you could easily have seen that the loop didn't run as many times as you expected and you could have looked at exactly why. Learn to debug. It will save you so much time in the long run. – Chris Apr 30 '21 at 11:48
  • Out of the numbers which were being selected randomly, only 5 of them were being selected. Yes, I wanted to write code for my own educational purposes that is why I was avoiding seeing the already given solutions but I did not have any more ideas. – Ajjajajjaja May 01 '21 at 12:15
  • I will also look into debugging my code. Thanks to everyone for all your help. – Ajjajajjaja May 01 '21 at 12:15

1 Answers1

2

The generic way to shuffle is called a Fisher Yates Shuffle and is easy to implement on a List<T>

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

Just pass your list to the method and it will shuffle the list. It works with a List<int> or a List<Card> or any other list you can think of.

Live example (using a list of 10 numbers): https://dotnetfiddle.net/UaS3wO

So with this I think your logic (If I understand correctly) would be

  1. Shuffle the deck
  2. Give a card to player and opponent and player in turns
  3. Return both the player and opponent deck

--

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

Note: There's actually no need to return the player and opponent decks - if you've passed in an empty List<int> to the above method it will get filled. So this would do just as well:

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])
    }
}
Jamiec
  • 133,658
  • 13
  • 134
  • 193