-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgrabbag.c
77 lines (68 loc) · 1.85 KB
/
grabbag.c
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
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "grabbag.h"
#include "piece.h"
/* Shuffles the contents of the grab bag. Used whenever a grab bag is
* initialized the end of the shuffled list is reached. */
void shuffle(GrabBag* bag);
GrabBag* grabbag_init(size_t n) {
GrabBag* bag = malloc(sizeof(GrabBag));
if (bag == NULL) {
return NULL;
}
bag->contents = malloc(sizeof(enum PieceType)*n*PIECE_TYPES_COUNT);
if (bag->contents == NULL) {
free(bag);
return NULL;
}
// initialize the random number generator for later use
srand((unsigned int)time(NULL));
bag->length = PIECE_TYPES_COUNT*n;
bag->current = 0;
size_t i;
for (i = 0; i < n; i++) {
size_t j;
for (j = L_L_PIECE; j <= LONG_PIECE; j++) {
size_t index = (i*PIECE_TYPES_COUNT) + (j-1);
bag->contents[index] = (enum PieceType)j;
}
}
shuffle(bag);
return bag;
}
void grabbag_free(GrabBag* bag) {
if (bag != NULL) {
if (bag->contents != NULL) {
free(bag->contents);
}
free(bag);
}
}
void grabbag_next(GrabBag* bag, enum PieceType* type) {
if (bag == NULL || bag->contents == NULL) {
return;
}
if (bag->current >= bag->length) {
shuffle(bag);
bag->current = 0;
}
*type = bag->contents[bag->current];
bag->current++;
}
void shuffle(GrabBag* bag) {
if (bag == NULL || bag->contents == NULL) {
return;
}
// Fisher-Yates shuffle
size_t i;
for (i = 0; i < bag->length-1; i++) {
size_t maxRange = bag->length-(i+1);
size_t swapI = i + ((size_t)rand())%maxRange;
if (i != swapI) {
enum PieceType temp = bag->contents[i];
bag->contents[i] = bag->contents[swapI];
bag->contents[swapI] = temp;
}
}
}