Home > Software design >  Make a deck of cards using randomizedlist. Does not work
Make a deck of cards using randomizedlist. Does not work

Time:10-19

So I have 4 players, and they should each have 26 cards. I have already made an enum of cards, and that is working. It is the last line of code, that is giving me trouble. This thing here: player[i].Add(randomizedlist[x]);. How can I write that another way, so that it works. I want to give each player[i] 26 cards from my randomizedlist.

    public List<Card> AllCards = new List<Card>();
     
    public void GenerateDeck() // Must use this to populate deck
    {
        int AntalKortspil = 0;
        int place = 0; // tracks which card number
        if (AntalKortspil < 2)
        {
            for (int i = 0; i < Enum.GetValues(typeof(CardKind)).Length; i  )
            {
                for (int f = 0; f < Enum.GetValues(typeof(Cardvalues)).Length; f  )
                {
                    Card card = new Card();
                    AllCards.Add(card);
                    AllCards[place].kind = (CardKind)i;
                    AllCards[place].values = (Cardvalues)f;
                    place  ;
                    
                    if ( place == 52)
                    {
                        AntalKortspil  ;
                        i = 0;
                        f = 0;
                    }
                }
            }
        }
    }

    public List<Card>[] player = new List<Card>[3];

    public void ShuffleAndGive()
    {
        // Give cards to players

        var rnd = new Random();
        var randomizedlist = AllCards.OrderBy(item => rnd.Next());

           for (int i = 0; i < 4; i  )
           {
            player[i] = new List<Card>(26);
            for (int x = 0; x < 27; x  )
               {
                player[i].Add(randomizedlist[x]);

            }

           }

CodePudding user response:

The OrderBy method is lazy. It returns an IEnumerable<Card> and doesn't actually execute anything until you try to iterate over the results. The interface IEnumerable<Card> lets you get the next card in the sequence, but you can't skip ahead, so you can't use the indexing operator randomizedlist[x].

In your example, you would want to iterate over the results and put them into a list, so call AllCards.OrderBy(item => rnd.Next()).ToList().

Aside from the compiler error, I've noticed a few other areas in the code that could be improved.

  • The Generate() method uses if (AntalKortspil < 2) instead of while (AntalKortspil < 2), so it will only generate 52 cards
  • The Generate() method gets all values in the enums, then uses a for loop instead of using the values it just got
  • The ShuffleAndGive() method gives the same 26 cards to all players

Have a look at this fiddle for an example of how you could structure your code

CodePudding user response:

You can populate the lists in a loop.

public void ShuffleAndGive()
{
    // Give cards to players

    for (int i = 0; i < 4; i  )
    {
        player[i] = new List<Card>(26);

        foreach (var card in AllCards)
        {
            player[i].Add(card);
        }
    }
}

Then, you can shuffle the lists: player[i].OrderBy(p => Guid.NewGuid()).Shuffle().ToList()

To shuffle, I used the excellent OrderBy method from LINQ's GroupBy.

CodePudding user response:

If you only want to randomize the generated deck and give the players random cards, you can do something like:

public void ShuffleAndGive()
{
    // Give cards to players

    var rnd = new Random();
    int n = AllCards.Count;  
    List<Card> randomizedList = new List<Card>();

    while (n > 1) {  
        n--;  
        int k = rnd.Next(n   1);  
        Card value = AllCards[k];  
        AllCards[k] = AllCards[n];  
        AllCards[n] = value;  
    }

    for (int i = 0; i < 4; i  )
    {
        player[i] = new List<Card>(26);
        for (int x = 0; x < 27; x  )
        {
            player[i].Add(randomizedlist[x   i * 26]);
        }
    }

}
  •  Tags:  
  • c#
  • Related