-
Notifications
You must be signed in to change notification settings - Fork 0
/
carddeck.c
82 lines (72 loc) · 1.46 KB
/
carddeck.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
78
79
80
81
#include "carddeck.h"
#include "randomgenerator.h"
#include <string.h>
char unique(deck_struct* deck) {
unsigned i, j;
for (i=0; i < 52; i++) {
for (j = i+1; j < 52; j++) {
assert_local(deck->cards[i] != deck->cards[j]);
}
}
return 1;
}
void init_deck(deck_struct* deck) {
unsigned int i;
for (i=0; i < 52; i++) {
deck->cards[i] = (char) i;
}
deck->remaining = 52;
}
char draw_card(deck_struct* deck, const char idx) {
char swap = deck->cards[(int) idx];
deck->remaining--;
deck->cards[(int) idx] = deck->cards[deck->remaining];
deck->cards[deck->remaining] = swap;
return swap;
}
void remove_card(deck_struct* deck, const char card) {
// guess that the card is at the proper position
char idx = card;
while (1) {
if (deck->cards[(int) idx] == card) {
draw_card(deck, idx);
return;
}
idx = (idx + 1) % deck->remaining;
}
}
char parse_card(char* str) {
if (strlen(str) != 2) {
printf("Invalid card string %s\n", str);
exit(1);
}
char r = 0;
unsigned int i, f = 0;
for (i=0; i < 13; i++) {
if (str[0] == CARD[i]) {
f++;
r = i << 2;
break;
}
}
for (i=0; i < 4; i++) {
if (str[1] == COLOR[i]) {
f++;
r += i;
break;
}
}
if (f != 2) {
printf("Invalid card string %s\n", str);
exit(1);
}
return r;
}
char _deckswap;
char draw_random_card(deck_struct *deck) {
char idx = get_random_card(deck->remaining);
return draw_card(deck, idx);
}
void reset_deck(deck_struct *deck) {
deck->remaining = 52;
}