-
Notifications
You must be signed in to change notification settings - Fork 5
/
blackjack.c
348 lines (316 loc) · 6.6 KB
/
blackjack.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
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
#include <stdio.h>
#include <time.h>
#include <stdlib.h>
int shuff(int cards[])
{
int t;
int i;
int desk[52];
for (i=0;i<52;i++)
desk[i] = (i/13+3)*100 + i%13 + 1;
srand(time(NULL));
for (i = 0; i < 52; i++)
{
do
{
t = rand() % 52;
} while (desk[t] == 0);
cards[i] = desk[t];
desk[t] = 0;
}
return 0;
}
int convert_jkq(int a)
{
if ((a%100==11) ||(a%100==12) ||(a%100==13)) return (a/100)*100+10;
else return a;
}
void pic(int num)
{
char fl;
int po_num;
fl = num / 100;
po_num = num % 100;
switch (po_num)
{
case 1:
{
printf("*******\n");
printf("* *\n");
printf("* %c *\n", fl);
printf("* A *\n");
printf("* *\n");
printf("*******\n");
break;
}
case 2:
case 3:
case 4:
case 5:
case 6:
case 7:
case 8:
case 9:
case 10:
{
printf("*******\n");
printf("* *\n");
printf("* %c *\n", fl);
printf("* %2d *\n", po_num);
printf("* *\n");
printf("*******\n");
break;
}
case 11:
{
printf("*******\n");
printf("* *\n");
printf("* %c *\n", fl);
printf("* J *\n");
printf("* *\n");
printf("*******\n");
break;
}
case 12:
{
printf("*******\n");
printf("* *\n");
printf("* %c *\n", fl);
printf("* Q *\n");
printf("* *\n");
printf("*******\n");
break;
}
case 13:
{
printf("*******\n");
printf("* *\n");
printf("* %c *\n", fl);
printf("* K *\n");
printf("* *\n");
printf("*******\n");
break;
}
}
}
int play(void)
{
int i;
int psum=0;
int bsum=0;
int pcards[5]={0};
int bcards[5]={0};
int cards[52];
char go_on;
char d;
printf("Welcome to SimpleBlackJack!\n"
"Anytime you can press Ctrl+C to exit.\n"
"Enjoy! Press Enter to go on......\n");
do{
go_on = getchar();
} while (go_on != '\n');
printf("\n");
//shuff the cards
shuff(cards);
//give the cards
pcards[0]=cards[0];
pcards[1]=cards[1];
bcards[0]=cards[2];
bcards[1]=cards[3];
//the 2 cards player get
printf("One of computer's cards:\n");
pic(bcards[0]);
printf("\n");
printf("Cards of player:\n");
pic(pcards[0]);
//printf("\n");
pic(pcards[1]);
//printf("\n");
i=0;
for (i=0; i<2; i++)
{
if (pcards[i]%100 == 1)
{
printf("choose A value of the card %d, input 'y' for 11 or 'n' for 1 :\n", i+1);
do{
d = getchar();
} while (d!='y' && d!='n');
if (d == 'y')
{
printf("You've chosen value 11 for card A.\n");
psum = psum + 11;
}
else if(d == 'n')
{
printf("You've chosen value 1 for card A.\n");
psum = psum +1;
}
}
else if (convert_jkq(pcards[i]) %100 ==10) psum = psum + 10;
else psum = psum + pcards[i]%100;
if (psum > 21)
{
printf("Sum of player's cards now:%d\n\n",psum);
printf("Computer win!\n");
return 1;
}
else if (psum == 21)
{
printf("Sum of player's cards now:%d\n\n",psum);
printf("Player win!\n");
return 0;
}
}
printf("Sum of player's cards now:%d\n\n",psum);
//whether player get another cards
i=0;
for (i=0; i<3; i++)
{
char j = 'n';
printf("Want more cards? Input y or n:\n");
do{
j = getchar();
} while (j!='y' &&j!='n');
if (j=='y')
{
printf("You've got another card now.\n");
pcards[i+2]=cards[i+4];
printf("and your card %d is:\n", i+3);
pic(pcards[i+2]);
if (pcards[i+2]%100 == 1)
{
printf("Choose A value of the card %d, input 'y' for 11 or 'n' for 1:\n", i+3);
do{
d = getchar();
} while (d!='y' && d!='n');
if (d == 'y')
{
printf("You've chosen value 11 for card A.\n");
psum = psum + 11;
}
else if(d == 'n')
{
printf("You've chosen value 1 for card A.\n");
psum = psum +1;
}
}
else if (convert_jkq(pcards[i+2]) %100 ==10) psum = psum + 10;
else psum = psum + pcards[i+2]%100;
if (psum > 21)
{
printf("Sum of player's cards now:%d\n\n",psum);
printf("Computer win!\n");
return 1;
}
else if (psum == 21)
{
printf("Sum of player's cards now:%d\n\n",psum);
printf("Player win!\n");
return 0;
}
else printf("Sum of player's cards now:%d\n\n",psum);
}
else
{
printf("Sum of player's cards now:%d\n\n",psum);
break;
}
}
if (i == 3)
{
printf("Player win! Because the sum of your 5 cards is no larger than 21! So lucky!\n");
return 0;
}
//the 2 cards of boss/computer
//i=0;
printf("Computer's cards:\n");
pic(bcards[0]);
pic(bcards[1]);
if (bcards[0]%100 + bcards[1]%100 == 2)
{
bsum=12; //two A cards
printf("Sum of computer's cards now:%d\n\n", bsum);
}
else if ((convert_jkq(bcards[0]))%100 + (convert_jkq(bcards[1]))%100 ==1)
{
bsum=21;
printf("Sum of computer's cards now:%d\n\n", bsum);
printf("Computer win!\n");
return 1;
}
else if (bcards[0]%100==1 || bcards[1]%100==1)
{
bsum=(bcards[0]+bcards[1])%100+(rand()%2)*10;
printf("Sum of computer's cards now:%d\n\n", bsum);
}
else
{
bsum = (convert_jkq(bcards[0]))%100 + (convert_jkq(bcards[1]))%100;
printf("Sum of computer's cards now:%d\n\n", bsum);
}
//whether computer get another cards until bsum>16
//i=0;
for (i=0; i<3 && bsum<17; i++)
{
bcards[i+2]=cards[i+7];
printf("Computer's card %d is:\n", i+3);
pic(bcards[i+2]);
if (bcards[i+2]%100 == 1)
{
if (bsum+11 <= 21)
{
printf("Computer has chosen A as 11\n");
bsum = bsum+11;
printf("Sum of computer's cards now:%d\n\n", bsum);
}
else
{
printf("Computer has chosen A as 1\n");
bsum = bsum+1;
printf("Sum of computer's cards now:%d\n\n", bsum);
}
}
else
{
bsum = bsum + convert_jkq(bcards[i+2])%100;
printf("Sum of computer's cards now:%d\n\n", bsum);
}
}
if (i == 3)
{
printf("Computer win! Because the sum of its 5 cards is no larger than 21! So lucky!\n");
return 1;
}
//the last step
if (bsum>21 || psum>bsum)
{
printf("Player win!\n");
return 0;
}
else if (psum == bsum)
{
printf("Oh, player and computer get the same score!\n");
return 3;
}
else if (psum < bsum)
{
printf("Computer win!\n");
return 1;
}
return 3;
}
int main(void)
{
char again;
play();
printf("\nAnd now would you like to play again? Input 'y' or 'n':\n");
do{
again = getchar();
} while (again!='y' && again!='n');
if (again == 'y')
{
printf("\nOK, let's go again!\n\n");
main();
}
return 0;
}