What I want to do is shuffle the cards like a person does. Split the deck roughly in half, then shuffle the two halves together from the bottom up by alternating between the havles and placing 1-4 cards from the halves in the orginal deck until all 52 cards are back in the original deck. Then repeat a good number of times to get the deck shuffled as if it has been used for years. I thought I had this all set. But if I try to shuffle it more than five times this way I get a segmentatin fault, and when I do it five times it isnt shuffled enough. So, with out further waiting here is the code.
card struct
Code:
enum suit {hearts,diamonds,spades,clubs};
struct card{
suit s; //takes values of the enum
int value; //1-13, where 1=A,11-13=J,Q,K
bool dis_or_played; //has the card been discarded/played from a hand?
};
deck class
Code:
class Deck{
friend class Player;
public:
Deck(); //create deck
void shuffle(); //shuffle deck
void deal(); //deal to players and the kitty
private:
card d[52]; //the deck
int next_card; //next card to be dealt
};
shuffle function(maybe be a little hard to understand with the way I have it set up)
Code:
void Deck::shuffle(){
time_t seconds;
time(&seconds);
srand((unsigned int)seconds);
random_shuffle(d[0],d[51]);
int b = rand() % (32 - 20 + 1) + 20;
int t = (52 - b);
card *temp1[b];
card *temp2[t];
int j=0;
int c = rand() % (4 - 1 + 1) +1;
int temp1_index = b-1;
int temp2_index = t-1;
int deck_index = 51;
for(int h=0; h<5; h++){
for(int i=0; i<b; i++){
temp1[i] = &d[i];
}
for(int i=b; i<52; i++){
temp2[j] = &d[i];
j++;
}
while(deck_index>=0){
if(temp1_index>=0 && (temp1_index + 1)>=c){
for(int i=0; i<c; i++){
d[deck_index] = *temp1[temp1_index];
temp1_index--;
deck_index--;
}
}
else{
int rest = temp1_index + 1;
for(int i=0; i<rest; i++){
d[deck_index] = *temp1[temp1_index];
temp1_index--;
deck_index--;
}
}
if(temp2_index>=0 && (temp2_index + 1)>=c){
for(int i=0; i<c; i++){
d[deck_index] = *temp2[temp2_index];
temp2_index--;
deck_index--;
}
}
else{
int rest = temp2_index +1;
for(int i=0; i<rest; i++){
d[deck_index] = *temp2[temp2_index];
temp2_index--;
deck_index--;
}
}
}
}
}