-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdeck.c
More file actions
96 lines (88 loc) · 2.03 KB
/
deck.c
File metadata and controls
96 lines (88 loc) · 2.03 KB
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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include <stdio.h>
#include <stdlib.h>
#include "deck.h"
//Hearts Diamonds Clubs Spades
char suits[4][9] = {"H", "D", "C", "S"};
char ranks[13][6] = {"A","2","3","4","5","6","7","8","9","10","J","Q","K"};
void deck_init(struct deck* d){
int s_nmb=0;
int r_nmb=0;
for(int i=0; i<DECK_SIZE; i++){
d->cards[i].suit = suits[s_nmb];
d->cards[i].rank = ranks[r_nmb];
r_nmb++;
if(i == 12){
s_nmb++;
r_nmb=0;
}
}
}
/*Fisher -Yates shuffle*/
struct deck* shuffle(struct deck* d){
for(int i=0; i<DECK_SIZE; i++){
int j = i + rand() / (RAND_MAX / (DECK_SIZE-i) + 1);
printf("j: RAND: %d %d", j, RAND_MAX);
struct card temp = d->cards[j];
d->cards[j] = d->cards[i];
d->cards[i] = temp;
}
return d;
}
void magic_shuffle(struct deck* d, int input){
/*Row 1 select:
1: 1 4 7 10 13 16 19
2: 0 3 6 9 12 15 18
3: 2 5 8 11 14 17 20
Row 2 select:
1: 0 3 6 9 12 15 18
2: 1 4 7 10 13 16 19
3: 2 5 8 11 14 17 20
Row 3 select:
1: 1 4 7 10 13 16 19
2: 2 5 8 11 14 17 20
3: 0 3 6 9 12 15 18
*/
struct deck temp;
int index = input;
if(input == 1){
index = 1;
}
if(input == 2){
index = 0;
}
if(input == 3){
index = 1;
}
for(int i=0; i<DECK_SIZE; i++){
temp.cards[i] = d->cards[index];
index += 3;
if((i+1)%7 == 0){
if(input ==1){
index = 0;
}else if(input == 2){
index = 1;
}else{
index=2;
}
}
if((i+1)%14 == 0){
if(input == 1){
index = 2;
}else if(input == 2){
index = 2;
}else{
index = 0;
}
}
}
//Assign the sorted deck to the playing-deck
*d = temp;
}
void print_deck(struct deck* d){
for(int i=1; i<=DECK_SIZE; i++){
printf("%s%s ", d->cards[i-1].suit, d->cards[i-1].rank);
if(i%3 == 0){
printf("\n");
}
}
}