This C# program is suppose to be a blackjack program but I need to 'SHUFFLE' the card and just display the 'HAND'
The rest i think I can manage... can someone help me?
This C# program is suppose to be a blackjack program but I need to 'SHUFFLE' the card and just display the 'HAND'
The rest i think I can manage... can someone help me?
One way to shuffle is to create new array and move the cards into that array in a random order
List<Card> unshuffled = new List<Card>(pack);
pack = new Card[NUM_CARDS];
Random r = new Random()
for(int card = 0; card < NUM_CARDS; card++)
{
pack[card] = unshuffled[r.Next(0, unshuffled.Count -1)];
unshuffled.remove(pack[card]);
}
You might want to make the Random
instance global, as creating a new random each time reduces entropy somewhat. This might not be important if you are not shuffling a lot.
I would recommend just switching each element to random other, heres how:
private void switchElements(Card[] pack, int nr_1, int nr_2) {
Card temp = pack[nr_1];
pack[nr_1] = pack[nr_2];
pack[nr_2] = temp;
}
public void shuffle(Card[] pack) {
for (int i = pack.length - 1; i > 0; i--)
switchElements(pack, i,random.Next(0,i));
}
You could "shuffle" a pack with something as simple as:
var shuffled = pack.OrderBy(c => random.NextDouble());