Implemented function to shuffle the deck.
This commit is contained in:
parent
b94341b70d
commit
e6a8651fb2
29
lib/deck.c
29
lib/deck.c
@ -1,4 +1,6 @@
|
|||||||
|
#include <stdlib.h>
|
||||||
#include <malloc.h>
|
#include <malloc.h>
|
||||||
|
#include <time.h>
|
||||||
#include "deck.h"
|
#include "deck.h"
|
||||||
|
|
||||||
void allocate_deck(struct deck **deck) {
|
void allocate_deck(struct deck **deck) {
|
||||||
@ -127,3 +129,30 @@ void fill_deck(struct deck *deck) {
|
|||||||
|
|
||||||
return;
|
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;
|
||||||
|
}
|
||||||
|
@ -26,5 +26,6 @@ void allocate_deck(struct deck **);
|
|||||||
void initialize_deck(struct deck *);
|
void initialize_deck(struct deck *);
|
||||||
void delete_deck(struct deck *);
|
void delete_deck(struct deck *);
|
||||||
void fill_deck(struct deck *);
|
void fill_deck(struct deck *);
|
||||||
|
void shuffle_deck(struct deck *);
|
||||||
|
|
||||||
#endif
|
#endif
|
||||||
|
Loading…
Reference in New Issue
Block a user