-2
        string[] cards = { "s1", "s2", "s3","s4","s5","m1", "m2", "m3","m4","m5", "k1", "k2", "k3","k4","k5", "RD", "RD", "RD" };
        string[] player1 = new string[6];
        string[] player2 = new string[6];
        string[] player3 = new string[6];

Hi, I'd like to apportion array's variables to another arrays(as random) in c#. For example:

player1 has {“s2”,”RD”,”m1”,”m2”,”k3”,”s5”}

player2 has something other than player1's variables or player3's. hope you understand what I say.

so what is the solution? could you explain? thank you very much.

Mat
  • 202,337
  • 40
  • 393
  • 406
  • The way you worded it is extremely confusing, but I think you mean you want to shuffle the deck and deal 6 cards to each player. You want to take care that you don't accidentally make a mistake that creates two or more instances of the same card. Right? – Wyck May 11 '20 at 18:14
  • First you put the cards in a deck list, then [shuffle](https://stackoverflow.com/questions/273313/randomize-a-listt) the deck. Then you take cards of the deck list and put them into the player's hand list. Where are you stuck? – Wyck May 11 '20 at 18:18
  • exactly, that's what i want. I tried it too different way. I even can't explain my way but I will try to your way. Before start, what do you mean about "deck list"? Im not native sorry. – İrfan Çalışır May 11 '20 at 18:33
  • Post the code you have already tried. Then we can see where you might be getting stuck. – Barns May 11 '20 at 20:37
  • An _array_ is not the right data structure in this case. So, by "deck list" I mean a `List` that has one element for each card that is still in the deck. If you deal a card, you remove it from the `List` that represents the _deck_ and add it to a different `List` that represents the _player's hand_. – Wyck May 12 '20 at 12:49

1 Answers1

0

Try something like this:

    static string[] cards = { "s1", "s2", "s3","s4","s5","m1", "m2", "m3","m4","m5", "k1", "k2", "k3","k4","k5", "RD", "RD", "RD" };
    private static Dictionary<int, string[]> DealTheCards(int numberOfPlayers, int numberOfCardsPerPlayer)
    {

        var playerDict = new Dictionary<int, string[]>();
        for(int i = 0; i < numberOfPlayers; i++)
        {
            string[] player = new string[numberOfCardsPerPlayer];
            playerDict.Add(i, player);
        }

        var cardsList = cards.ToList();
        var cardsNeeded = playerDict.Count() * numberOfCardsPerPlayer;
        if(cardsNeeded > cards.Count())
        {
            Console.WriteLine("Not enough Cards");
        } 
        else
        {
            for(int i = 0; i < numberOfCardsPerPlayer; i ++)
            {
                foreach(var player in playerDict)
                {
                    var rand = new Random();
                    var x = rand.Next(cardsList.Count);
                    var card = cardsList.ElementAt(x);
                    cardsList.Remove(card);
                    player.Value[i] = card;
                }
            }
        }
        return playerDict;
    }

This code is a bit more flexible. Simply call the method DealTheCards from your code like:

    var numberOfPlayer = 3;
    var numberOfCardsPerPlayer = 6;
    var playersCards = DealTheCards(numberOfPlayer, numberOfCardsPerPlayer);

The return object is a Dictionary object with a zero-based index for each player (player1 has an index = 0, player2 index = 1...)

This code allows you to be a bit more flexible in the number of players and number of cards each player can have--you just need to be certain to have enough cards in the deck to distribute.

This code mimics what you would expect when the cards are being dealt: The dealer would have the cards (outer for loop). Foreach player a card would be selected (removed from the deck) and given to the player. Then the next card would be selected (removed from the deck) and given to the next player... until all the players receive a card and then we start at the first player, again...until each player has received the number of cards that each player should have.


Note:

There is some criticism of how good Random is at randomizing. There are other methods, but this should be sufficient for your application. Substituting a different randomizer should not be difficult.

Also: The methods are static because I set this up for a console application.

Please let me know if you have any questions.

Barns
  • 4,850
  • 3
  • 17
  • 31