diff --git a/lib/deck.c b/lib/deck.c index b2337b3..a1677ca 100644 --- a/lib/deck.c +++ b/lib/deck.c @@ -1,4 +1,6 @@ +#include #include +#include #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; +} diff --git a/lib/deck.h b/lib/deck.h index c41e566..624a7a5 100644 --- a/lib/deck.h +++ b/lib/deck.h @@ -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