-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlack Jack.cpp
363 lines (319 loc) · 9.37 KB
/
Black Jack.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
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
// Faizan Khan
// Younes Taib
#include <iostream>
#include <ctime>
using namespace std;
// Setting up info for a black jack player
class blackJack
{
public:
void shuffleCards(bool []);
void printCards(int);
void printHands(int [], const int);
int retrieveCards(bool []);
int handScores(int [], const int);
void printHandNScore(int [], const int, int[], const int);
};
// Shuffle the cards
void blackJack::shuffleCards(bool cardsDealing[])
{
// Go through the index of the cards, check for non-existing cards
for (int index = 0; index < 52; index++)
{
cardsDealing[index] = false;
}
}
// Determine the card's rank and its suit
void blackJack::printCards(int numberOfCards)
{
// Calculate the rank by getting the remainder of the number of cards
const int cardRank = (numberOfCards % 13);
// If the rank is 0, print Ace which is A
if (cardRank == 0)
{
cout << " Ace ";
}
// If the rank is less than 9, increase the rank by 1
else if (cardRank < 9)
{
cout << cardRank + 1;
}
// If the rank equals 9,
else if (cardRank == 9)
{
cout << " Toke ";
}
// If the rank equals 10, print J for Jack
else if (cardRank == 10)
{
cout << " Jack ";
}
// If the rank is 11, print Q for Queen
else if (cardRank == 11)
{
cout << " Queen ";
}
// Otherwise for the rank print King
else
{
cout << " King ";
}
// The card suit is to divide the number of cards by 13
const int cardSuit = (numberOfCards/13);
// If the suit is 0, print C for cut
if (cardSuit == 0)
{
cout << " Cut ";
}
// If the suit is 1, print D for double/double down
else if (cardSuit == 1)
{
cout << " Double ";
}
// If the suit is 2, print H for hit
else if (cardSuit == 2)
{
cout << " Hit ";
}
// Else print S for stand
else
{
cout << " Stand ";
}
}
// Print the cards that have the player has requested a hand
void blackJack::printHands(int hands[], const int countingCards)
{
// Go through the count of cards
for (int index = 0; index < countingCards; index++)
{
// Find the next card in the deck of cards, the print the next card
const int nextCard = hands[index];
printCards(nextCard);
cout << " ";
}
cout << endl;
}
// Retrieve the cards from the deck
int blackJack::retrieveCards(bool inCardDeck[])
{
// See if the card selected in the deck is true, otherwise default its -1
bool deckOfCards = true;
int newCard = -1;
// Randomly generate the new card with the entire deck of cards
do
{
newCard = (rand() % 52 + 1);
// If the new card is not the deck set it to false as its non-existing
if (!inCardDeck[newCard])
{
deckOfCards = false;
}
}
while (deckOfCards);
// Return the existing new card
return newCard;
}
// Get the house scores by retrieve the deck of cards in hands and the # count
int blackJack::handScores(int hands[], const int countingCards)
{
// Set the aces and scores to 0 at the start
int numOfAces = 0;
int numScores = 0;
// Go through the index of counting the cards thats being in the deck
for (int index = 0; index < countingCards; ++index)
{
// Retrieve the next card in the deck for choosing a hand and its rank
const int nextCard = hands[index];
const int nextRank = (nextCard % 13);
// If the rank is 0, then increment the aces and score before retrieving
if (nextRank == 0)
{
++numOfAces;
++numScores;
}
// If nextRank < 9 then calculate scores to itself then the rank + 1
else if (nextRank < 9)
{
numScores = numScores + (nextRank + 1);
}
// Otherwise increase the scores by 10
else
{
numScores = numScores + 10;
}
}
// If the aces > 0 && scores < 12 decrement aces and add scores to 10
while (numOfAces > 0 && numScores < 12)
{
--numOfAces;
numScores = numScores + 10;
}
// Return the scores
return numScores;
}
// Print the hand scores and count for player and house
void blackJack::printHandNScore(int hands[], const int hcCount, int pHand[],
const int playingCount)
{
cout << "House(AI)'s Hand Score: " << handScores(hands, hcCount);
cout << "-------------------------------" << endl;
printHands(hands, hcCount);
cout << "Player's Hand Score: " << handScores(hands, playingCount);
cout << "-------------------------------" << endl;
printHands(hands, playingCount);
cout << endl;
}
// Main function
int main()
{
char playerOptions;
// Prompt the user to enter a choice for the black jack game in our casino
cout << "Hello, player welcome to Empire City Casino!" << endl;
cout << "Want to play blackJack (y/n)";
cin >> playerOptions;
// If they selected yes(y), then the game will run
while (playerOptions == 'y')
{
// Create a random number generator
time_t cardDealing;
time(&cardDealing);
srand(cardDealing);
// Black jack data(deck, house cards, player cards, count # of cards)
bool dealTheCards[52];
int countHouseCards = 0;
int houseHands[12];
int countPlayerCards = 0;
int playerHands[12];
blackJack user;
// Call the cards to be shuffled for both player and house, (count = 2)
user.shuffleCards(dealTheCards);
playerHands[0] = user.retrieveCards(dealTheCards);
houseHands[0] = user.retrieveCards(dealTheCards);
playerHands[1] = user.retrieveCards(dealTheCards);
houseHands[1] = user.retrieveCards(dealTheCards);
countHouseCards = 2;
countPlayerCards = 2;
// Hands section
cout << " Hands " << endl;
cout << "------------------------------------" << endl;
cout << endl;
// Player options and if they want a hit or a stand
bool playerHits = true;
// The player scores stores the # of their counts and the hands they took
int pScore = user.handScores(playerHands, countPlayerCards);
// Show the house and player's hands in a continuous loop
do
{
// Count the house's cards
cout << "House(AI)'s Hand" << endl;
cout << "House(AI): ";
user.printCards(countHouseCards);
cout << endl;
// Count the player's hand and its cards
cout << "Player's Hand "
<< user.handScores(playerHands, countPlayerCards)
<< endl;
user.printCards(countPlayerCards);
user.printHands(playerHands, countPlayerCards);
// Ask the player if they want a hit(extra card) or stand(no card)
cout << "Hit or Stay (H/S): ";
cin >> playerOptions;
// If the player chooses a hit retrieve a card and add to their deck
if (playerOptions == 'H')
{
playerHands[countPlayerCards] = user.retrieveCards(dealTheCards);
++countPlayerCards;
}
// If they pick stand keep the count of their cards whatever they have
else if (playerOptions == 'S')
{
playerHits = false;
}
// Otherwise the choice is not valid
else
{
cout << "The choice entered is not valid! Try again!";
}
cout << endl;
// Retrieve the player's total hand scores
pScore = user.handScores(playerHands, countPlayerCards);
// If the player gets 21, they win the game
if (pScore == 21)
{
cout << "The Player wins the Game!" << endl;
}
// If the player gets greater than 21, then the house wins
else if (pScore > 21)
{
cout << "The House(AI) wins the Game!" << endl;
user.printHandNScore(houseHands, countHouseCards, playerHands,
countPlayerCards);
}
// If the player gets less than 21, then evaluate the following
else if (pScore < 21)
{
int hScore = user.handScores(houseHands, countHouseCards);
// Get the cards of the house if they have less than 17
while (hScore < 17)
{
houseHands[countHouseCards] = user.retrieveCards(dealTheCards);
++countHouseCards;
hScore = user.handScores(houseHands, countHouseCards);
}
// If the house goes over 21, then the player wins the game
bool houseBusts = (hScore > 21);
if (houseBusts)
{
cout << "The Player wins the Game!" << endl;
user.printHandNScore(houseHands, countHouseCards, playerHands,
countPlayerCards);
}
// if the pscore > hscore or pscore < 21, the player wins
else if (pScore > hScore && pScore < 21)
{
cout << "The Player wins the Game!" << endl;
}
// Otherwise evaluate the other conditions
else
{
// If the player score is qual to house score, its a tie
if (pScore == hScore)
{
cout << "Tie(Push)!" << endl;
user.printHandNScore(houseHands, countHouseCards, playerHands,
countPlayerCards);
}
// If the player score is greater than house score, player wins
else if (pScore > hScore)
{
cout << "The Player wins the Game!" << endl;
user.printHandNScore(houseHands, countHouseCards, playerHands,
countPlayerCards);
}
// Otherwise the house wins
else
{
cout << "The House(AI) wins the Game!" << endl;
user.printHandNScore(houseHands, countHouseCards, playerHands,
countPlayerCards);
}
}
}
}
// Keep looping if the the player's score and hits is less than 22
while (playerHits && pScore < 22);
}
// Otherwise print the exit message
if (playerOptions == 'n')
{
cout << "Have a good day and tell a friend about our casino!" << endl;
}
// Otherwise the choice is not valid
else
{
cout << "The choice entered is not valid! Try again!" << endl;
}
system("PAUSE");
return 0;
}