-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeck.c
222 lines (110 loc) · 4.54 KB
/
Deck.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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
/*-----------------------------------------------------------------------------+
File Name: Deck.c
+------------------------------------------------------------------------------+
+------------------------------------------------------------------------------+
Programming III COP4338
Author: Daniel Gonzalez P#4926400
assignment 3: Deck of Cards
Date: 10/20/2016
program description
Input: Accept input via the command-line arguments. Validate command-line
input. Input will be the number of cards/hand and the number of
hands(players), in that order.
Output: Display of a deck of cards, cards of each hands after dealing
and a winner hand..
+-------------------------------------------------------------------------+
| I Daniel Gonzalez #4926400 hereby certify that this collective work is |
| my own and none of it is the work of any other person or entity. |
+-------------------------------------------------------------------------+
how to compile and execute:
1.Open the terminal
Go to the program folder
Run the following command "make"
2.Open the terminal
Go to the program folder
Run the following command
"gcc -Wall -w -lm Main.c Game.c Deck.c Card.c Player.c -o poker"
Program execution:
From the terminal enter “./poker [#Cards][#Players]"
both arguments are required
+-----------------------------------------------------------------------------*/
#include "Main.h"
/* Include structures, prototypes and libraries for the program */
/*-----------------------------------------------------------------------------+
* [createDeck initialized a new organized deck
* @param deck deck to be initialized
*/
void createDeck(Deck *deck){
int faceIndex;
int suitIndex;
int deckIndex = 0;
deck->cards = (Card *)malloc(sizeof(Card) * DECK_CARDS);
deck->cardsAmt = DECK_CARDS;
for (suitIndex = 0; suitIndex < NUM_SUITS; suitIndex++) {
for (faceIndex = 0 ; faceIndex < NUM_FACES; faceIndex++) {
deck->cards[deckIndex].face = faceIndex;
deck->cards[deckIndex].suit = suitIndex;
deckIndex++;
}
}
}
/*-----------------------------------------------------------------------------+
* [displayDeck Displays all the cards in the deck
* @param deck Deck to be displayed
*/
void displayDeck(Deck *deck){
int index;
int topCards, bottomCards;
int deckRemainder = deck->cardsAmt % NUM_FACES;
Boolean nextLine;
printf(
"\n\n\n%s\n|%50s%38s|\n%s\n",
"+--------------------------------------------"
"--------------------------------------------+",
"CURRENT DECK",
"",
"+--------------------------------------------"
"--------------------------------------------+"
);
displayTopCards((NUM_FACES >= deck->cardsAmt )? deckRemainder : NUM_FACES );
for ( index = 0; index < deck->cardsAmt ; index++ ) {
nextLine = ((( index + OFFSET ) % NUM_FACES ) == 0 )? TRUE : FALSE;
displayCard( &( deck->cards[index] ));
if ( nextLine && index < ( deck->cardsAmt - OFFSET ) ){
displayBottomCards( NUM_FACES % deck->cardsAmt );
topCards = (index >= ( deck->cardsAmt - NUM_FACES ))?
(( deckRemainder == 0 )?
NUM_FACES :
deckRemainder ):
NUM_FACES;
displayTopCards(topCards);
}
}
bottomCards = ( deckRemainder == 0 )? NUM_FACES : deckRemainder;
displayBottomCards( bottomCards );
}
/*-----------------------------------------------------------------------------+
* [shuffle Shuffles the cards from the deck
* @param deck Deck to be shuffled
*/
void shuffle(Deck *deck){
int pos = 0;
int index = 0;
time_t t;
srand((unsigned) time(&t));
for( index = 0 ; index < DECK_CARDS ; index++ ) {
pos = rand() % DECK_CARDS;
swap(&(deck->cards[index]), &(deck->cards[pos]));
}
}
/*-----------------------------------------------------------------------------+
* [swap helper function for shuffle swaps tow cards in a deck
* @param card1 card1 to be swap
* @param card2 card to be swap with card1
*/
void swap(Card *card1, Card *card2 ){
Card tempCard;
tempCard = *card1;
*card1 = *card2;
*card2 = tempCard;
}