0

I am trying to shuffle a deck of cards after initializing a set.

The array is initialized correctly, however when I step through the debugger within the shuffle method a few cards are never even initialized.

I have also noticed 3 random cards are always un-initialized after calling Shuffle.


The swap/shuffle functions:

void SwapCards(Card * cardOne, Card * cardTwo) //sending in address of two cards you want to swap
{
    Card temp = *cardOne;

    *cardOne = *cardTwo;
    *cardTwo = temp;

}

//create a new array to be filled with randomized cards
void Shuffle(Card Deck[], int iSize)
{
    int i;
    srand(time(NULL));

    for (i = 0; i < iSize; i++)
    {
        int randCard = rand() % 52;

        SwapCards(Deck + i, Deck + randCard);

        printf("The random number is %d\n", randCard);

    }
}

main:

int main(int argc, char** argv)
{
    const int SuitSize = 14;
    const int iSize = 52;
    int i;
    Face j;
    Suits k;
    Card Deck[52];

    printf("\nUnshuffled Deck...\n");

    //outer for loop is for suits = 4 x 13 = 52
    for (k = Clubs; k <= Spades; k++)
    {
        //inner for loop is for face
        for (j = Deuce; j <= Ace; j++)
        {
            Deck[j - Deuce + k * SuitSize].suit = k; //j-Deuce is 2 - face pos plus k * 13 k
            Deck[j - Deuce + k * SuitSize].value = j;

            ShowFace(j);
            printf(" of ");
            ShowSuit(k);
        }
    }

    printf("\nShuffled Deck...\n");
    Shuffle(Deck, iSize);

    //outer loop for suits
    for (k = Clubs; k <= Spades; k++)
    {
        //inner loop is for face
        for (j = Deuce; j <= Ace; j++)
        {
            ShowFace(Deck[j - Deuce + k * SuitSize].value);
            printf(" of ");
            ShowSuit(Deck[j - Deuce + k * SuitSize].suit);
        }
    }

    getchar();

}

The enum/struct declarations:

typedef enum { Clubs , Diamonds, Hearts, Spades } Suits;
typedef enum { Deuce = 2, Three, Four, Five, Six, Seven, Eight, Nine, Ten, Jack, Queen, King, Ace } Face;

typedef struct {
    Suits suit;
    Face value;
} Card;
V.M
  • 71
  • 1
  • 9
  • 2
    The algorithm you used to shuffle the array is wrong, check [here](https://en.m.wikipedia.org/wiki/Fisher–Yates_shuffle#The_modern_algorithm) for thr ```fisher-yates shuffle```. – JiaHao Xu Nov 03 '18 at 23:55
  • what part of the algorithm is wrong? the swap function or the for loop? – V.M Nov 04 '18 at 00:07
  • There are 13 cards in a suit. Think again wait `SuitSize` should be. – user58697 Nov 04 '18 at 00:09
  • I see my error now. Thank you! – V.M Nov 04 '18 at 00:10
  • 1
    See (amongst others) [Shuffle array in C](https://stackoverflow.com/questions/6127503) and [Is this implementation of the Fisher-Yates shuffle in C correct](https://stackoverflow.com/questions/3343797) and also Coding Horror on [The Danger of Naïveté](http://www.codinghorror.com/blog/2007/12/the-danger-of-naivete.html), not to mention [SO 5145-7357](https://stackoverflow.com/questions/51457357/) where I also pointed to the other links. – Jonathan Leffler Nov 04 '18 at 00:19
  • 1
    Someone should also point out that `srand(time(NULL))` will give you results that can be predicted quite easily, and `rand() % 52` is also slightly biased towards smaller values (unless RAND_MAX+1 is a multiple of 52, which is very unlikely). If you want to shuffle the cards properly, use Fisher–Yates as suggested, and use `/dev/urandom` as a source of random numbers. (To get a random number in the range 0–51, fetch a byte `x` from `/dev/urandom` and calculate `x & 63`. Repeat until you get a result lower than 52.) – r3mainer Nov 04 '18 at 00:20
  • 1
    @squeamishossifrage: Not to mention the fact that a 32-bit linear congruential pseudo-random number generator cannot generate 52! different sequences of numbers, so you can never generate all possible permutations with the average `rand()` implementation. There are better ones available (the Mersenne-Twister algorithms, for example), but they're frequently not what's used behind `rand()`. – Jonathan Leffler Nov 04 '18 at 00:23

0 Answers0