-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlackjack.cpp
320 lines (311 loc) · 9.73 KB
/
Blackjack.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
#include <iostream>
#include <cmath>
#include <array>
#include <vector>
#include <string>
#include <map>
#include <iterator>
#include <stack>
#include <unordered_set>
#include <set>
#include <unistd.h>
#include <ctime>
using namespace std;
const string signs[4] = {"\u2665", "\u2660", "\u2663", "\u2666"}; //heart, spade, club, diamond respectively
const string sett[13] = {"A", "2", "3", "4","5", "6", "7", "8", "9", "10", "J", "Q", "K"};
vector<string> dealer_score;
vector<string> player_score;
int dealer_drawing();
int player_drawing();
int hit(int score);
bool blackjack(vector<string> check);
int dealer_final_display(int score);
int main()
{
int asset = 100, amount_bet;
cout << "Welcome to the world of Blackjack: Created by Ethan Nguyen." << endl;
sleep(1);
srand(time(0)); // run random num every time program is executed
cout << "You are given $" << asset << ". Use wisely!" << endl;
sleep(1);
while (asset > 0)
{
cout << "You own $" << asset << " How much would you like to bet? ";
cin >> amount_bet;
while (true) // protect against invalid input, such as characters
{
if (cin.fail())
{
cout << "Invalid Input. Please try again: ";
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(),'\n');
cin >> amount_bet;
}
else
{
break;
}
}
while (amount_bet > asset)
{
cout << endl << "The amount you entered exceeds your currect asset. Please choose a smaller amount: ";
cin >> amount_bet;
}
sleep(1);
cout << "OK! Let's play." << endl;
cout << "Dealer: ";
sleep(1);
int DealerScore = dealer_drawing();
sleep(1);
cout << endl << "You: ";
int PlayerScore = player_drawing();
cout << endl;
if (blackjack(player_score) == true && blackjack(dealer_score) == true)
{ // both parties have blackjack
for (int i = 0; i < dealer_score.size(); i++)
{
cout << dealer_score[i];
}
cout << endl << "You both have blackjack. TIE." << endl;
sleep(1);
}
else if (blackjack(player_score) == true && blackjack(dealer_score) == false)
{ // player win blackjack
cout << "Congrats! You win!" << endl;
sleep(1);
asset = asset + (amount_bet * 1.5) + amount_bet;
}
else if (blackjack(player_score) == false && blackjack(dealer_score) == true)
{ // player lost blackjack
for (int i = 0; i < dealer_score.size(); i++)
{
cout << dealer_score[i];
}
cout << "Blackjack..." << " You lost" << endl;
asset -= amount_bet;
}
else // the actual playing starts if no blackjack from both parties recognized
{
char input;
while (true)
{
cout << "Please press H to hit, S to stay, D to Double Down, and Q to quit the game: ";
cin >> input;
if (toupper(input) == 'H') // hit
{
PlayerScore = hit(PlayerScore);
if (PlayerScore > 21)
{
asset -= amount_bet;
cout << "Dealer final result: ";
for (int j = 0; j < dealer_score.size(); j++)
{
cout << dealer_score[j];
}
cout << endl << "Sorry... You lost... Please try again" << endl;
break;
}
}
else if (toupper(input) == 'S') //stay
{
break;
}
else if (toupper(input) == 'D' && player_score.size() == 4) // double down (only on the first hit)
{
amount_bet *= 2;
PlayerScore = hit(PlayerScore);
if (PlayerScore > 21)
{
asset -= amount_bet;
cout << "Dealer final result: ";
for (int j = 0; j < dealer_score.size(); j++)
{
cout << dealer_score[j];
}
cout << endl << "Sorry... You lost... Please try again" << endl;
break;
}
break;
}
else if (toupper(input) == 'Q') // quit
{
cout << "Good bye. Come back at anytime. Although, remember that you've lost everything." << endl;
return 1;
}
}
if (PlayerScore < 22) // player still in game
{
cout << "Dealer final result: ";
DealerScore = dealer_final_display(DealerScore);
sleep(2);
if (DealerScore > 21) // dealer break first
{
asset += amount_bet;
sleep(1);
cout << "YOU WON!! CONGRATS!" << endl;
}
else if (DealerScore > PlayerScore) // both in game: dealer won, player lost
{
asset -= amount_bet;
sleep(1);
cout << "You lost... Please try again" << endl;
}
else if (DealerScore < PlayerScore) // both in game: dealer lost, player won
{
asset += amount_bet;
sleep(1);
cout << "YOU WON!! CONGRATS!" << endl;
}
else //both in game: tie
{
sleep(1);
cout << "TIE." << endl;
}
}
}
player_score.clear(); // clear vector before restart the game
dealer_score.clear();
}
if (asset <= 0)
{
cout << "Sorry! Come back soon." << endl;
}
return 0;
}
int dealer_drawing() // initial drawing for the dealer, return total score
{
int total = 0;
for (int i = 0; i < 2; i++)
{
// card drawing for dealer
int dealer_sign_generator = rand() % 4;
int dealer_sett_generator = rand() % 13;
int dealer_card = 0;
if (dealer_sett_generator == 0)
{
dealer_card = 1;
}
else if (dealer_sett_generator > 9)
{
dealer_card = 10;
}
else
{
dealer_card = stoi(sett[dealer_sett_generator]);
}
//for display purposes
if (i == 0)
{
cout << sett[dealer_sett_generator] << signs[dealer_sign_generator] << endl;
}
dealer_score.push_back(sett[dealer_sett_generator]);
dealer_score.push_back(signs[dealer_sign_generator]);
total += dealer_card;
}
return total;
}
int player_drawing() //initial drawing for the player
{
int total = 0;
for (int i = 0; i < 2; i++)
{
// card drawing for player
int player_sign_generator = rand() % 4;
int player_sett_generator = rand() % 13;
int player_card = 0;
if (player_sett_generator == 0)
{
player_card = 1;
}
else if (player_sett_generator > 9)
{
player_card = 10;
}
else
{
player_card = stoi(sett[player_sett_generator]);
}
//for display purposes
cout << sett[player_sett_generator] << signs[player_sign_generator];
if (i == 1)
{
cout << endl;
}
player_score.push_back(sett[player_sett_generator]);
player_score.push_back(signs[player_sign_generator]);
total += player_card;
}
return total;
}
int hit(int score)
{
//drawing a card for the player
int player_sign_generator = rand() % 4;
int player_sett_generator = rand() % 13;
int player_card;
if (player_sett_generator == 0)
{
player_card = 1;
}
else if (player_sett_generator > 9)
{
player_card = 10;
}
else
{
player_card = stoi(sett[player_sett_generator]);
}
player_score.push_back(sett[player_sett_generator]);
player_score.push_back(signs[player_sign_generator]);
for (int j = 0; j < player_score.size(); j++)
{
cout << player_score[j];
}
cout << endl;
return score + player_card;
}
bool blackjack(vector<string> check)
{
if (check[0] == "A" && ((check[2] == "10"||check[2] == "J"||check[2] == "Q"||check[2] == "K")))
{
return true;
}
else if (check[2] == "A" && ((check[0] == "10"||check[0] == "J"||check[0] == "Q"||check[0] == "K")))
{
return true;
}
return false;
}
int dealer_final_display(int score)
{
//dealer display one last time before decide to hit or not
for (int j = 0; j < dealer_score.size(); j++)
{
cout << dealer_score[j];
}
sleep(2);
//dealer hits until total >= 17
while (score < 17)
{
int dealer_sign_generator = rand() % 4;
int dealer_sett_generator = rand() % 13;
int dealer_card = 0;
if (dealer_sett_generator == 0)
{
dealer_card = 1;
}
else if (dealer_sett_generator > 9)
{
dealer_card = 10;
}
else
{
dealer_card = stoi(sett[dealer_sett_generator]);
}
cout << sett[dealer_sett_generator] << signs[dealer_sign_generator];
score += dealer_card;
sleep(1);
}
cout << endl;
return score;
}