0

Working on my first OOP project. I have initialized a list of cards with value and suit. So I have a list of 52 objects. I need to shuffle. I have given each object a field named ShuffleRank. I have given each card a random value between 1-100. Now I want to sort the deck by this value and return a new deck. I've tried OrderBy and Sort, but I'm having type issues. Here's my deck class:

namespace BlackJack
{

    public class Deck
    {
        //This needs to be public on the get side to be visible outside
        //of the Deck object
        public List<Card> Cards { get; private set; }
        public List<Card> Shuffled { get; set; }
        Random rnd = new Random();

        public Deck()
        {
            NewDeck();
        }


        private void NewDeck()
        {
            Cards = new List<Card>();

            for (var i = 0; i < 4; i++)
            {
                for (var j = 2; j < 13; j++)
                {
                    Cards.Add(new Card { Suit = (Suit)i, Value = (CardValue)j });
                }
            }
        }





        public void shuffleCards()
        {
            foreach (var Card in Cards)
            {

                Card.ShuffleRank = rnd.Next(100);
                Shuffled = Cards.Sort().......

            }


        }

    }
}
ScottVMeyers
  • 307
  • 2
  • 15
  • What kind of issues? Cannot you do something like `Cards = Cards.OrderBy(x => x.PropertyName).ToList()`? – varocarbas Nov 29 '15 at 09:32
  • ahh, I wasn't implementing right. So this goes ascending? Cool! – ScottVMeyers Nov 29 '15 at 09:34
  • Keep in mind that sorting is not the best way to shuffle a collection. For better options check my blog: http://csattempts.blogspot.com/2013/01/linq-shuffle-and-thread-safe.html or this answer: http://stackoverflow.com/a/1150699/283975 – Grozz Nov 29 '15 at 09:35
  • everybody in my class is going to user fisher-yates I'm sure, so I wanted to come up with something different. i've ran into problems with the random function before. I'll test it a bunch ,and probably end up implementing the version on your blog. thanks! – ScottVMeyers Nov 29 '15 at 09:40
  • a bit off topic, but do I need to return "Cards" with this? I'm still a little hazy on when I need a function to return something – ScottVMeyers Nov 29 '15 at 09:41

0 Answers0