jeudi 25 avril 2019

Can't figure out why my while loop is getting stuck

Currently working on writing code for a Windows Forms version of the card game war. I needed a way to shuffle the list to randomize the deck. I have already defined the lists for deck and shuffledDeck as a list of strings.

When I run createDeck(), I had it output to a textbox in my windows form to ensure that it is infact creating the list properly. I also had it test to ensure that deck.Count() was equal to 52

private void shuffle()
        {
            createDeck();
            shuffledDeck = deck;
            Random r = new Random();
            int randomIndex = 0;
            while (deck.Count > 0)
            {
                randomIndex = r.Next(0, deck.Count);
                shuffledDeck.Add(deck[randomIndex]);
                deck.RemoveAt(randomIndex);
            }

This is my testing that the deck is in fact shuffled

            for (int a = 0; a <= 51; a++)
            {
                textBox1.Text += " " + shuffledDeck[a];
            }

I would expect that I would see a shuffled deck each time however whenever I run it, Visual Studio freezes and I am have to force quit the program to exit.




Aucun commentaire:

Enregistrer un commentaire