1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
|
//shuffle class attempt
#include <iostream>
using namespace std;
class shuffle //class declaration
{public:
shuffle(int card_deck[], int number, int now);
void mix();
private:
int deck[];
int n;
int current_card; //declare and initialize current array member
}
shuffle::shuffle(int card_deck[], int number, int now) //initialize variables with constructor
{ deck[]=card_deck[];
current_card=now;
n=number;
}
shuffle::mix()
{while(current_card<=n) //while there are cards left in the deck, continue the loop
{int last_card_position=current_card-1; //declare and initialize decreasing variable for check loop to prevent duplicate cards
int b=rand_int(0,n);
int check() //loop for duplicate check
{if(b=deck[last_card_position]) //if the random # is equal to any of the previous values, rerandomize and check again.
{b=rand_int(0,n);
check();}
else if(last_card_position<=0)break; //if the random # is unique so far, and the card just checked was in the 0 position, end loop
else //if the random # is unique so far, and the card != 0, decrease the array position by one and check again
{last_card_position--;
check();}
}
deck[n]=b; //if the random # is unique thus far, write it to its position in the array
current_card++; //move to next position in array and start over
}
}
int main()
{cout<<"How many cards are in the deck?";
cin>>int cards;
int cards[]=for(int j=0;j=n;j++)cards[j]=j;
shuffle shuffle(cards[], 0, cards);
shuffle(cards[],cards);
cout<<"The values of the cards are ";
for(i=0;i=n;i++){cout<<deck[i];}
return 0;
}
|