Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C#: Wrote my own shuffler; keep getting "King of Spades" as last card in deck

Tags:

c#

I'm working on a blackjack program in an attempt to learn C#. I have a card class, and a deck array object consisting of cards. I used the deck array to populate a deckInPlay list (of cards), which will actually be used in-game. I chose to use a list instead of the array in-game because a list can change size dynamically. Up until now, I've been able to solve my own problems, but now that I've created a shuffleDeck method, I've got an issue that truly perplexes me.

Let's start with my code for the shuffleDeck method (I instantiated Random as rnd in the dealer class constructor):

public void shuffleDeck(int timesToShuffle)
    {
        for (int i = 0; i <= timesToShuffle; i++)
        {
            for (int j = 0; j < 52; j++)
            {
                int randomNumber = rnd.Next(0, deckInPlay.Count - 1);
                Card randomCard = deckInPlay[randomNumber];
                shuffler.Add(randomCard);
                deckInPlay.RemoveAt(randomNumber);
            }

            for (int j = 0; j < 52; j++)
            {
                int randomNumber = rnd.Next(0, shuffler.Count - 1);
                Card randomCard = shuffler[randomNumber];
                deckInPlay.Add(randomCard);
                shuffler.RemoveAt(randomNumber);
            }
        }
    }

As you can see, I'm using two lists: the deckInPlay list which is the actual deck I want to use in-game, and the shuffler list, which is primarily used to aid in shuffling the cards. I intended the code to take random cards from the deckInPlay list and add them one by one to the shuffler list, all the while removing the cards from the deckInPlay list. I then wanted the process to reverse: take random cards from the shuffler list and add them to the deckInPlay list, removing each card from the shuffler list in the process.

At first glance, it seems that the shuffler works OK. It works even better when calling shuffleDeck with a parameter higher than 1. But one thing stays the same, no matter what the parameter is: King of Spades is always the last card in the list after the shuffle process. King of Spades just so happens to be the default last card in my initial deck array and deckInPlay list.

I don't understand where I went wrong here. I'd really appreciate it if you could give me a clue. Thank you for reading!

like image 285
Dana Alcala Avatar asked Jul 16 '14 06:07

Dana Alcala


2 Answers

Max value of Next is exclusive, so it means that you'll not reach that number and I then suspect your "King of Spades" is number 52 in your deck.

like image 93
Allan S. Hansen Avatar answered Nov 15 '22 04:11

Allan S. Hansen


Use

int randomNumber = rnd.Next(0, shuffler.Count);

rnd.Next gets a number from 0 to Count - 1 in your example, but not including that upper range.

like image 22
Michel Keijzers Avatar answered Nov 15 '22 04:11

Michel Keijzers