Question

I'm back again. So I have created a Deck of cards in Linked List fashion using Cards as the nodes. I am having trouble with my riffle shuffle function, however. The function should create two Deck objects that each store half of my Deck drawPile. Those two half piles should then take a card off one at a time and add it back to the drawPile. I am not getting an error, but it is not shuffling the cards at all. I call the function as such: drawPile.shuffle(drawPile);

Deck::Deck()
{
    top = NULL;
    SIZE = 0;
}

Deck::Deck(Card *n)
{
    top = n;
}
void Deck::shuffle(Deck &d)
{
    srand((unsigned)time(0));

    for (int i = 0; i < 7; i++){
        //split deck in half -- leftHalf and rightHalf
        Deck leftHalf, rightHalf;

        int j = SIZE/2;
        for (int k = SIZE; k > j; k--){
            drawCard(leftHalf);  //drawCard off Deck and addCard to leftHalf
        }
        for (j; j > 0; j--){
            drawCard(rightHalf);  //drawCard off Deck and addCard to rightHalf
        }
        while (leftHalf.getSIZE() > 0 && rightHalf.getSIZE() > 0){
            int probPile = leftHalf.getSIZE()/(leftHalf.getSIZE() + rightHalf.getSIZE())*100;
            int randomNum = rand() % 100 + 1;
            if (randomNum <= probPile){
                leftHalf.drawCard(d);   //drawCard off leftHalf and addCard to Deck
            }
            else{
                rightHalf.drawCard(d);  //drawCard off rightHalf and addCard to Deck
            }
        }
        while (leftHalf.getSIZE() > 0){
    leftHalf.drawCard(d);
        }
    while (rightHalf.getSIZE() > 0){
    rightHalf.drawCard(d);
    }
    }
}

void Deck::drawCard(Deck &d)    
{
    Card *draw = top;
    if (draw != NULL){
        top = top->getNext();
        SIZE--;
            d.addCard(draw->getVALUE(), draw->getSUIT());
    }
}//end drawCard

void Deck::addCard(int val, string s)
{
    top = new Card(val, s, top);
    SIZE++;
}//end addCard
Was it helpful?

Solution

Your problem is here:

while (leftHalf.getSIZE() > 0 && rightHalf.getSIZE() > 0){

If you have drawn all the cards from leftHalf but rightHalf still contains some cards then your cycle ends. In that case your shuffled Deck doesn't contains all cards.
After that cycle you need for example to implement two cycles like this:

while (leftHalf.getSIZE() > 0)
    // Draw all remaining cards from leftHalf
while (rightHalf.getSIZE() > 0)
    // Draw all remaining cards from rightHalf

=============================================
Also you can get a "Null pointer access" error here:

d.addCard(draw->getVALUE(), draw->getSUIT());

because draw can be null.

=============================================
Also here:

for (int l = SIZE; l > 0; l--){

you try to draw too many cards. You can start the cycle from j instead of SIZE.

==============================================
Another problem is here:

int probPile = leftHalf.getSIZE()/(leftHalf.getSIZE() + rightHalf.getSIZE())*100;

probPile is always 0 because you use integer division (e.g. 5/100 is 0 if we use integer division). One of the solution to use floating-point division. You can convert int value to double before division in some way, e.g. by multiplying to 1.0 and then convert the result back to int:

int probPile = static_cast<int>(leftHalf.getSIZE() * 1.0 /(leftHalf.getSIZE() + rightHalf.getSIZE())*100);

Or you can multiply by 100 before division:

int probPile = leftHalf.getSIZE() * 100/(leftHalf.getSIZE() + rightHalf.getSIZE());
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top