-2

Ok, So I am new to programming in general but getting a start with C#. I am still a near absolute beginner and just writing small projects to get comfortable with the language. I decided this was what I wanted to play with, SO first I created a class Deck, that holds a list of cards. This function is what I wanted to use to shuffle the cards. I do realize this is not the most efficient way but I wanted to accomplish a "Realistic" Shuffle. This is also a rough draft off the top of my head. The code works 1 time through, but Once i call deck.RemoveRange(0,52); It removes everything, from every list, Including NewDeck, FirstCut, SecondCut. (This is just a learning experiment for me and has nothing to do with "What is useful")

Edit: Apologies for the confusing code, I still vomit whatever is in my head until the computer understands. I meant to mention I knew that the issue was an issue with Refrence Types vs. Value Types, I was just under the impression that I was creating new Items when I was in fact, Not doing so. So I wondered how that applied to .RemoveRange and if that remove just an element or removed the base reference(Books and video's never explained this so I am trying to learn from this mistake).

        public static List<Card> GetNewDeck(int ShuffleTimes)
    {
        Deck MainDeck = new Deck();
        List<Card> NewDeck = MainDeck.DeckOfCards;
        List<Card> ShuffledDeck = new List<Card>();
        for (int i = 0; i <= ShuffleTimes; i++)
        {
            Random Shuffle = new Random();
            var FirstCut = NewDeck;
            var SecondCut = new List<Card>();
            var deck = new List<Card>();
            var cut = Shuffle.Next(15, 37);
            for (int c = 0; c < cut; c++)
            {
                    SecondCut.Add(FirstCut[0]);
                    FirstCut.RemoveAt(0);
            }
            while (deck.Count != 52)
            {
                var rif = Shuffle.Next(1, 4);
                if (rif < SecondCut.Count)
                {
                    for (int r = 0; r < rif; r++)
                    {
                        deck.Add(SecondCut[0]);
                        SecondCut.RemoveAt(0);
                    }
                }
                    if (rif < FirstCut.Count)
                    {
                    for (int d = 0; d < rif; d++)
                    {
                        deck.Add(FirstCut[0]);
                        FirstCut.RemoveAt(0);
                    }
                }
                if (rif >= SecondCut.Count && SecondCut.Count != 0)
                {
                    for (int ri = 0; ri <= SecondCut.Count; ri++)
                    {
                        deck.Add(SecondCut[0]);
                        SecondCut.RemoveAt(0);
                    }
                }
                    if (rif >= FirstCut.Count && FirstCut.Count != 0)
                    {
                        for (int si = 0; si <= FirstCut.Count; si++)
                        {
                            deck.Add(FirstCut[0]);
                            FirstCut.RemoveAt(0);
                        }
                    }
                    if (deck.Count == 52)
                {
                    NewDeck = deck;
                }
            }
            deck.RemoveRange(0, 52);
            ShuffledDeck = NewDeck;
        }
        return ShuffledDeck;
    }
Ryu
  • 52
  • 5
  • This code is very confusing. But I suspect the issue has to do with the concept of object references. You're talking about multiple "decks", but notice that you only ever call `new Deck()` once. So you only have one actual deck. Any modifications you make to that deck would be observable in any other references to that deck. – David Apr 10 '17 at 22:09
  • You need to learn the difference between [reference types and value types](http://www.albahari.com/valuevsreftypes.aspx). – Dour High Arch Apr 10 '17 at 22:11
  • Nah, I figured my issue was with reference types and value types, I was just confused because I thought I called a New List several times and was wondering where I made the mistake. Its funny that once you point out what I did it became obvious and completely answered my question. Thank you very much. – Ryu Apr 10 '17 at 23:23

1 Answers1

0

When you assign one list to another, you are not making a copy of it, you are literally assigning a reference to the same list over and over. So, each time you do that, they all point to the same place. Thus, clearing one clears all.

What you probably want is to make a copy of the list. For example:

NewDeck = new List<Card>(deck);

This won't make a copy of the card objects themselves, but will create a new list that references the cards, which is probably fine for your needs.

Erik Funkenbusch
  • 92,674
  • 28
  • 195
  • 291
  • It took some thinking, but this actually did also answer my base question. I do apologize for it being such confusing code. This is only the second thing I've attempted to write from the top of my head with the goal being to understand C# (Not necessarily make something useful) Books and Video's only took me so far but by doing this I was able to actually understand Reference Types vs. Value Types. So thank you. From this i have been able to refactor everything down to 10 lines of code. – Ryu Apr 11 '17 at 18:01