-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDeckOfCards.cpp
310 lines (239 loc) · 9.14 KB
/
DeckOfCards.cpp
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
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/******************************
** DeckOfCards.cpp **
** Created by Charles Davis **
** CPSC 246-01 **
******************************
***************************************************************************
** DeckOfCards.cpp is the implemtation file for the DeckOfCards class **
** Since a deck of cards is in this case just an array of integers we **
** had to make some special cases for the face value of the cards. **
** **
** If we take the int value of the card from 0-51 and mod it by 13 we **
** get the face value of the card and if we divide that same integer by **
** 13 it will give use the suit value of the card **
** **
** FACE VALUE = int card % 13 SUIT VALUE = int card / 13 **
** **
** Based on this logic we can assign special values to a few cards in **
** the deck and special values to the suits of the cards **
** **
** KING = 0 ACE = 1 "2-10" = 2-10 JACK = 11 QUEEN = 12 **
** **
** CLUBS = 0 DIAMONS = 1 HEARTS = 2 SPADES = 3 **
** **
** See the cardToString function for further clarification on this code **
** Everything else is explained clearly in the rest of the file. **
** **
** Good Luck and have fun playing card games! **
***************************************************************************/
#include <iostream>
#include <string>
#include "DeckOfCards.h"
#include "time.h"
using namespace std;
/*************************** DeckOfCards default ****************************/
DeckOfCards::DeckOfCards()
{
// The default constructor for a deck of cards object. It creates
// a new DeckOfCards with cards with 52 cards not shuffled. It also
// seeds a random number generator for shuffling.
srand((unsigned) time(0)); // Seed a random number generator
// based on the time
numCards = MAX; // set the maximum deck size
for(int i = 0; i < MAX; i++) //init the array from 0-51
{
ary[i] = i;
}
}//end constructor
/*************************** DeckOfCards alternate ***************************/
DeckOfCards::DeckOfCards(int n)
{
// The alternate constructor for a deck of cards object. It
// creates a new DeckOfCards with n cards. Currently it is not
// implemented to be able to handle anthying but an empty deck of
// 0 cards. It also seed a random number generator for shuffling.
srand((unsigned) time(0)); // Seed a random number generator
if(n==0) // based on the time
{
numCards = 0; // make an empty hand with zero cards
}else
{
cerr << "This function hasn't been developed yet. " << endl;
cerr << "Creating a deck with 0 cards..." << endl;
numCards = 0;
}
} //end constructor 2
/********************************* shuffle ***********************************/
void DeckOfCards::shuffle()
{
// shuffle creates two random numbers to be used as array indices
// and swaps them in the array. It does this 1,000 times to
// randomize well.
int randomIndex1, randomIndex2;
int temp;
for(int i = 0; i < 1000; i++) //swap random indicies 1,000 times
{
randomIndex1 = rand() % MAX; // get 1st random from 0 to MAX
randomIndex2 = rand() % MAX; // get 2nd random from 0 to MAX
temp = ary[randomIndex1]; // swap them
ary[randomIndex1] = ary[randomIndex2];
ary[randomIndex2] = temp;
}
} // end shuffle
/*********************************** cut *************************************/
void DeckOfCards::cut()
{
// cut creates two partitions of the deck array using a cut point
// within the middle third of the deck and swaps them.
int tempAry[MAX];
int oneThird, twoThirds, cutPoint;
int loc = 0; // temp var to indicate the current index in array
oneThird = numCards / 3; // find the first 3rd
twoThirds = (numCards * 2) / 3; // find the second third
cutPoint = (rand()%((twoThirds-oneThird)+1)); // find a cut point
cutPoint += oneThird; // within the middle 3rd
// and add the bottom 3rd
for(int i = cutPoint; i < numCards; i++) // take the top and add
{ // it to the temp array
tempAry[loc] = ary[i];
loc++; // increment the location in temp array
}
for(int i = 0; i < cutPoint; i++) // start from location in temp ary
{
tempAry[loc] = ary[i]; // get the bottom partition
loc++; // and add it to the temp array
} // from the temp location to cut point
for(int i = 0; i < numCards; i++) // copy temp array back to original
{
ary[i] = tempAry[i];
}
} // end cut
/******************************* dealACard ***********************************/
int DeckOfCards::dealACard()
{
// deals a card from the top of the array and decrements the number of
// cards in the deck
int tempCard = ary[numCards-1]; // from the top get a int card
numCards--; // decrement the num cards in deck
return tempCard; // return the card to caller
}
/******************************* acceptCard **********************************/
void DeckOfCards::acceptCard(int card)
{
// accepts an integer card and adds it to the deck if the
// deck is not full and then increments the number of cards
// if the card is not negative and is within range of MAX
if(card >= 0 && card < MAX)
{
if(numCards < MAX) // and there is room in the deck
{
ary[numCards] = card; // add a card to the top of the deck
numCards++; // increment the number of cards
}
else // the deck is full
{
cerr << "Sorry the deck is full, cannot add card" << endl;
}
}
else // invalid card value
{
cerr << "Sorry the card value you added is not within valid range" << endl;
}
} // end acceptCard
/******************************* isEmpty *************************************/
bool DeckOfCards::isEmpty()
{
// returns true if the deck is empty
if(numCards > 0)
return false;
else
return true;
}
/****************************** getNumCards **********************************/
int DeckOfCards::getNumCards()
{
// returns the current number of cards in the deck
return numCards;
}
/********************************* reset *************************************/
void DeckOfCards::reset()
{
// resets the deck to the original deck of MAX cards and puts them
// into the original order 0-51
numCards = MAX;
for(int i = 0; i < MAX; i++)
{
ary[i] = i;
}
}
/******************************** reset empty ********************************/
void DeckOfCards::reset(bool empty)
{
// if empty is true it resets an empty deck to its original
// empty state with 0 cards otherwise it resets the deck to a fresh
// MAX sized deck by calling the other reset function
if(empty)
numCards = 0;
else
reset();
}
/******************************** printDeck **********************************/
void DeckOfCards::printDeck()
{
// prints the current deck out by calling the cardToString function
// for each element in the current deck array
cout << endl;
for(int i = numCards-1; i >= 0; i--) // from the top of the deck
{
// print out the string version of the card at index i;
cout << cardToString(ary[i]) << " " << endl;
// every 13 cards start a new row
if((i % 13) == 0)
{
cout << endl;
}
}
}
/******************************* cardToString ********************************/
string DeckOfCards::cardToString(int card)
{
// returns the string value of a card's integer value by dividing and
// modding the integer value to get the suit and face value of the card
string strValue = " "; // temp string to be concatonated for result
int cardValue = card % 13; // get face value
int cardSuit = card / 13; // get card suit
switch (cardValue) // depending on the face value get string value
{ // add the string value to the temp string
case 0: strValue += "King of "; break;
case 1: strValue += "Ace of "; break;
case 2: strValue += "Two of "; break;
case 3: strValue += "Three of "; break;
case 4: strValue += "Four of "; break;
case 5: strValue += "Five of "; break;
case 6: strValue += "Six of "; break;
case 7: strValue += "Seven of "; break;
case 8: strValue += "Eight of "; break;
case 9: strValue += "Nine of "; break;
case 10: strValue += "Ten of " ; break;
case 11: strValue += "Jack of "; break;
case 12: strValue += "Queen of "; break;
default: strValue += "Invalid "; break;
}
switch (cardSuit) // depending on the suit get string suit
{ // append the suit string to the temp string
case 0: strValue += "Clubs\n\n"; break;
case 1: strValue += "Diamonds\n\n"; break;
case 2: strValue += "Hearts\n\n"; break;
case 3: strValue += "Spades\n\n"; break;
default: strValue += "Invalid "; break;
}
// return the final resulting string value of the int card
return strValue;
} // end cardToString
/********************************** showCard *********************************/
int DeckOfCards::showCard(int cardIndex)
{
// shows the card to the user without dealing and destroying the deck
// useful for checking hand types in poker
return ary[cardIndex];
}