I'm currently creating a playing card memory game in my programming class, and I'm having a lot of difficulty creating a shuffle function for the deck.
The way that I set up the program was such that I have two arrays of 52 elements. The first array, unshuffleddeck[52] was filled with numerical values 1-52 in order, and the second array, shuffleddeck[52] was left blank. The function is supposed to pick a random element from unshuffleddeck, and copy it to shuffleddeck, unless that particular element of unshuffleddeck is equal to zero, in which case it loops until a non-zero value is chosen. When a non-zero value is chosen, it should copy that value to shuffleddeck, and directly after that reset that specific element of unshuffleddeck to zero, thus removing it from as a possibility for the next element of shuffleddeck. That's what I intended to do, here's what I ended up with:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
void shuffle(void)
{
cout<<"Shuffling, Please wait......\n";
for (n=1;n<53;n++)
{
do
{
temp = ((rand()%52)+1);
if (unshuffleddeck[temp]==0) //Checks for repeats. See below
dupe = true;
else
{
dupe = false;
shuffleddeck[n]=temp; /*supposed to fill concurrent elements of shuffleddeck with values that aren't repeated.*/
unshuffleddeck[temp]=0;/*Supposed to *remove* the selected value as a possibility for the next element of unshuffleddeck*/
cout<<endl<<temp<<endl<<n;
}
}
while (dupe == true);
}
cout<<"shuffling complete.\n";//Just here to see if the loop finished
return ;
}
|
As far as I can see, it should assign values for shuffleddeck which don't repeat, but it does not. When I run it, it will fill shuffleddeck with random values, but it doesn't prevent repeats. I have spent several classes attempting to make the function work, but I'm starting to fall behind.
I would appreciate if someone could tell me how I can change this specific function to have it work, but if it's completely illogical I will scrap it and try something else. Thanks.