-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcdeck.c
193 lines (125 loc) · 4.32 KB
/
cdeck.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
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
#define MAX 10 // for names
#define SPADES "spades"
#define HEARTS "hearts"
#define CLUBS "clubs"
#define DIAMONDS "diamonds"
#define Ace 1 //royalty card vals
#define Jack 11
#define Queen 12
#define King 13
struct Card{
//char type[MAX];
char suit[MAX];
int value;
// struct Card *next; //next pointer
}a_card;
//struct Card *topCard=NULL; // head of Deck
struct Card *cardArray[52]; // add card array for random access ... random pointers
double randTime=0; //since the time that is gathered is so the same for each rand,
//we also add this to the given time that we call srand on
void shuffle(); // rearranges the array of cards in a different order
void freeDeck(struct Card *card[52]); // frees deck
void printDeck();//prints deck
void createDeck();
// operational functions
int arrayIter(int num);
int main (){
//printf("deck\n");
createDeck();
// shuffle();
// shuffle();
printDeck();
//printf("The topCard is %d of %s.\n", topCard->value, topCard->suit);
freeDeck(cardArray);
return 0;
}
void createDeck(){ // initializes and creates the deck (in order)
//first should be of topCard (head)
char suitArray[4][9]; // array of each suit
strcpy(suitArray[0], SPADES);
strcpy(suitArray[1],DIAMONDS);
strcpy(suitArray[2],CLUBS);
strcpy(suitArray[3],HEARTS);
// printf("%s\n",suitArray[0]);
int suitPosition=0; // position in the suit string array, allowing us to not repeat code
int cardValueInd=0; // numbers 1-13, resets after each batch of 13
for(int i=0;i<52;i++){ // for loop of 52
cardArray[i]=(struct Card*) malloc(sizeof(struct Card)); // malloc memory of that card
strcpy(cardArray[i]->suit, suitArray[suitPosition]); // assign suit
cardArray[i]->value=cardValueInd+1; // assign numeric value
// printf("%d of %s \n", cardArray[deckCounter]->value, cardArray[deckCounter]->suit);
cardValueInd++; //increment of a card
if((i+1)%13==0){ //counting the card that is marked ... could also use cardValueInd here
suitPosition++;
cardValueInd=0; // reset to 0
}
}
} // end createDeck()
void shuffle(){ // randomly rearranges the cards
// int numberBoard[52];
// for(int i=0;i<52;i++){
// numberBoard[i]=i;
// }
struct Card *newArray[52]; // new array of Card pointers
int newArrInd=0;
randTime+=4.67;
time_t t;
srand((unsigned) time(&t)+randTime); // should only be used once
for(int i=0;i<52;i++){
int randInd= (rand()%52); // returns random num from 0 to 52
struct Card *location= cardArray[randInd];
while(location==NULL){ // while the random location that we end up at is null
randInd= arrayIter(randInd); // increments random index by one
location= cardArray[randInd];
//printf(" repeated num %d \n ", randInd);
}
newArray[newArrInd]=cardArray[randInd];
newArrInd++;
cardArray[randInd]=NULL; // sets the the random object to NULL
//printf("%d \n", randInd);
}
memcpy(&cardArray, &newArray, (sizeof(newArray)));// assign cardArray to the new array
//https://stackoverflow.com/questions/1693853/copying-arrays-of-structs-in-c
//printf("------\n");
}
int arrayIter(int index){ // array iteration function
if(index <52){
index+=1;
return index;
}
else{
return 0;
}
}
void freeDeck(struct Card *card[52]){ // frees the structs
for(int i=0;i<52;i++){
free(card[i]);
}
}
void printDeck(){ //prints the LL
for(int i=0;i<52;i++){
struct Card *currentCard= cardArray[i];
int val= currentCard->value;
switch(val){
case 1: // ace
printf("ace of %s \n",currentCard->suit);
break; // break is necessary here so that we dont print a case and the default
case 11: // jack
printf("jack of %s \n", currentCard->suit);
break;
case 12: //queen
printf("queen of %s \n", currentCard->suit);
break;
case 13: //king
printf("king of %s \n", currentCard->suit);
break;
default: // any other num
printf("%d of %s \n", currentCard->value,currentCard->suit);
}
}
printf("------\n");
}