Click here to Skip to main content
15,033,303 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello Code Project!

Well for the past week I started a new project which entails creating a library full of many different card games. Well while writing the shuffle method I had borrowed from M$'s blackjack sample project. Needless to say I didn't like the way that one shuffled. So I set out to create a new shuffle method. So I picked up a deck of cards and really looked at how I shuffled to try and recreate that using code. I'll write my pseudo code as well.

Now for my problem and the reason I need some help...

Pseudo Code:
private void Shuffle()
    while(They're still cards in a hand)
        // using offsets add(layer) 1-4 cards(from each hand) 
        // to the new shuffled deck

Current Code:
public void Shuffle()
    Random r = new Random();
    List<Card> tempDeck = new List<Card>();
    List<Card> bridgeHand = new List<Card>();
    List<Card> LeftHand = new List<Card>();
    List<Card> RightHand = new List<Card>();
    int nMinOffset = 1, nMaxOffset = 9;
    int cardHand = 0;

    for (int h = 0; h < 7; h++) // Shuffle 7 times

        // Build Left Hand - Splits Deck
        for (int i = 0; i < 25; i++)
        { LeftHand.Add(this.Draw()); }

        // Build Right Hand - Splits Deck
        for (int i = 0; i < 25; i++)
        { RightHand.Add(this.Draw()); }

        // Create Bridge
        while (true)
            cardHand = r.Next(nMinOffset, nMaxOffset);
            // This is where I get stuck at...
            // Need to do checks to make sure both hands have cards
            // If a hand doesn't have enough cards to match the random offset then add remaining cards
            // When both hands are empty break the loop...

Any and all help is greatly appriciated. This problem has been rattling my brain for a few hours now. Not sure what I need to do.

Doing my best to try and self teach/learn this language(My First).
Updated 15-Dec-12 13:00pm
Jibesh 15-Dec-12 19:14pm
Please elaborate your problem did you forget to mention the remaining code inside the while loop? and also hope you understood how the Random class works? when you set the limit 1 to 9.
Jdlogic 15-Dec-12 19:27pm
My problem is, I'm not sure what code to write to have this method work properly.
The plan was to use the Random offset values spat out by the method to decide how many cards a drawn onto the bridge deck. I'm trying to simulate the shuffling of a deck of cards when you would shuffle it in person(Split deck, add cards by alternating hands[Left,Right] humans are not perfect machines when shuffling so that is what the random offset is for).

--- EDIT ---
I now see what your talking about when it comes to the random class. I made a simple test to see what really happens with the numbers. Found that the max number is never reached.
Jibesh 15-Dec-12 19:35pm
Ok. got the point... give me some answers to the following questions:
1. There will be 25 cards on the right hands and 25 cards on the left hand
2. add the shuffled cards to the brideHand and return this ( total 50 cards)
Jdlogic 15-Dec-12 20:10pm
1. You forgot to account for 0. So its 26 cards not 26(0-25).
2. Above answer should solve for this question.
Sergey Alexandrovich Kryukov 15-Dec-12 21:40pm
Do you need to show process of shuffling with all intermediate state and imitate a shuffling by a player? Or you simply need to end up with a shuffled deck? If you only need a shuffled result, you are greatly over-engineer the process, because of an attempt to simulate a player's way. Getting just the result is way easier.

1 solution

// Adjust the random number to ensure it is within the bounds of the hand we're dealing with

if (cardhand >= leftHand.Count)
cardHand = leftHand.Count-1;
// move the next 'cardHand' cards from leftHand to bridgeHand

// repeat for right hand

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900