-
Notifications
You must be signed in to change notification settings - Fork 0
/
BlackJack.cpp
253 lines (208 loc) · 4.79 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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
struct card {
int card_value;
char card_name;
char card_suit;
};
struct card cards[52];
char turnP;
char turnD;
void setGame(){
/*
Establishing playing cards
*/
char suits[4] = {'S', 'C', 'D', 'H'};
for(int s = 0; s < 4; s++){
for (int i = 0; i < 13; i++){
cards[(s*13)+i].card_value = i+1;
if (i+1 == 10){
cards[(s*13)+i].card_name = 'T';
}else if(i+1 == 11){
cards[(s*13)+i].card_name = 'J';
}else if(i+1 == 12){
cards[(s*13)+i].card_name = 'Q';
}else if(i+1 == 13){
cards[(s*13)+i].card_name = 'K';
}else if(i+1 == 1){
cards[(s*13)+i].card_name = 'A';
}else
cards[(s*13)+i].card_name = (i+1) + '0';
cards[(s*13)+i].card_suit = suits[s];
}
}
/*
for(int i = 0; i < 52; i++){
printf("Value: %d, Card: %c, Suit: %c)\n", cards[i].card_value, cards[i].card_name, cards[i].card_suit);
}
*/
}
struct card drawCard(){
bool t = true;
struct card tmp;
while(t){
int ran = rand() % 52;
if (cards[ran].card_value != 0){
t = false;
tmp = cards[ran];
cards[ran].card_value = 0;
}
}
return tmp;
}
int cardCount(struct card hands[20]){
int count = 0;
int Aces = 0;
for (int i = 0; hands[i].card_value != 0; i++){
if (hands[i].card_name == 'A')
Aces++;
count = count + hands[i].card_value;
}
if (count > 21 && Aces > 0){
count = count - Aces * 10;
}
return count;
}
void dispCards(struct card hands[20]){
printf("Player: \n");
for (int i = 0; i < 20; i++){
if (hands[i].card_value != 0)
printf("[%c%c] ", hands[i].card_name, hands[i].card_suit);
else
i=20;
}
printf("\n");
}
int player(struct card hands[20]){
char c;
struct card newCard;
int pCount;
printf("What would you like to do? (H, S)\n");
scanf(" %c", &c);
dispCards(hands);
if (c == 'H'){
newCard = drawCard();
printf("Player Drew Card: %c Suit: %c\n", newCard.card_name, newCard.card_suit);
for (int i = 0; i < 20; i++){
if (hands[i].card_value == 0){
hands[i] = newCard;
i = 20;
}
}
dispCards(hands);
}else
turnP = 'S';
return cardCount(hands);
}
int dealer(struct card hands[20]){
struct card newCard;
if (cardCount(hands) <= 12){
newCard = drawCard();
for (int i = 0; i < 20; i++){
if (hands[i].card_value == 0){
hands[i] = newCard;
i=20;
}
}
turnD = 'H';
printf("Dealer Drew A Card\n");
}else if (cardCount(hands) <=17){
bool chance = rand() % 2;
if (chance){
newCard = drawCard();
for (int i = 0; i < 20; i++){
if(hands[i].card_value == 0){
hands[i] = newCard;
i = 20;
}
}
}
turnD = 'H';
printf("Dealer Drew A Card\n");
}else{
turnD = 'S';
printf("Dealer Stayed\n");
}
printf("Dealer :\n");
for(int i =0; i < 20; i++){
if (hands[i].card_value != 0)
printf("[%c%c] ", hands[i].card_name, hands[i].card_suit);
else
i = 20;
}
printf("\n");
printf("Dealer's Hand: %d\n", cardCount(hands));
return cardCount(hands);
}
int check(int dealer, int player, int balance){
if (player > 21 && dealer <= 21 && dealer != 0){
printf("Bust! Dealer Won This.\n");
return balance;
}else if (((dealer > 21 && player <21) || (player < 21 && player > dealer)) && dealer != 0 && player != dealer){
printf("Won!\n");
return (balance + 100);
}else if (player == 21 && (dealer < 21 || dealer > 21) && dealer != 0){
printf("Black Jack!\n");
return (balance + 100);
}else if (player == dealer|| (player > 21 && dealer > 21)){
printf("Split Pot, Draw\n");
return (balance + 50);
}else if (dealer > player && dealer != 0){
printf("Dealer Won\n");
return balance;
}
}
int playGame(){
int balance = 500;
bool inProg = true;
bool game = true;
char end;
struct card phand[20];
int condition;
//printf("Test Card: %d", phand[1].card_value);
struct card dhand[20];
int playerC, dealerC;
srand (time(NULL));
while(game){
if (balance < 50){
printf("You're Broke! Game Over");
break;
}else
balance -= 50;
setGame();
for (int i = 0; i < 20; i++){
phand[i].card_value = 0;
dhand[i].card_value = 0;
}
for (int i = 0; i < 2; i++){
phand[i] = drawCard();
dhand[i] = drawCard();
}
dispCards(phand);
while (inProg){
playerC = player(phand);
dealerC = dealer(dhand);
if ((turnP == 'S' && turnD == 'S') || (playerC > 21 || dealerC > 21)){
balance = check(dealerC, playerC, balance);
printf("New Balance: %d\n", balance);
inProg = false;
}
}
printf("Continue? (50 Credits)\n");
scanf(" %c", &end);
if (end != 'Y')
game = false;
else
inProg = true;
}
}
int main(){
char name[20];
printf("Welcome to Black Jack v1.0. You'll be playing against the Dealer. \n");
printf("Enter your name(Keep it below 20 characters): \n");
scanf(" %s", name);
printf("Hello %s. Enjoy your stay.\n", name);
playGame();
return 0;
}