Implemented function to shuffle the deck.

This commit is contained in:
Murilo Soares Pereira 2010-04-08 22:33:26 -03:00
parent b94341b70d
commit e6a8651fb2
2 changed files with 30 additions and 0 deletions

View File

@ -1,4 +1,6 @@
#include <stdlib.h>
#include <malloc.h>
#include <time.h>
#include "deck.h"
void allocate_deck(struct deck **deck) {
@ -127,3 +129,30 @@ void fill_deck(struct deck *deck) {
return;
}
void shuffle_deck(struct deck *deck) {
struct stack **stack = NULL;
struct stack tmp;
int random;
stack = malloc(NUMBER_OF_CARDS * sizeof(*stack));
for (int i = 0; i < NUMBER_OF_CARDS; i++) {
stack[i] = pop(&(deck->stock));
}
srand(time(NULL));
for (int i = 0; i < NUMBER_OF_CARDS - 1; i++) {
random = i + (rand() % (NUMBER_OF_CARDS) - i);
tmp = (*stack[i]);
(*stack[i]) = (*stack[random]);
(*stack[random]) = tmp;
}
for (int i = 0; i < NUMBER_OF_CARDS; i++) {
push(&(deck->stock), stack[i]->card);
}
return;
}

View File

@ -26,5 +26,6 @@ void allocate_deck(struct deck **);
void initialize_deck(struct deck *);
void delete_deck(struct deck *);
void fill_deck(struct deck *);
void shuffle_deck(struct deck *);
#endif