-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathblackjack.c
355 lines (338 loc) · 11.8 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
349
350
351
352
353
354
355
/**
* Author: David Nadeau 4515649
* Purpose: Create blackjack using C
* Instructions: Run ./blackjack and follow given instructions
* Syntax: Variables prefixed with i are indexes for arrays
* Variables prefixed with n are used for counts
* */
#include <stdio.h>
int player_hand[10]; //players hand
int house_hand[10]; //house hand
int deck[52]; //starting deck
int busted = 0; //used to indicate if the player has busted
/** card handleling methods */
void qsort(int *,int,int); //quicksort i found online. used to count aces last when tallying scores
int tally_hand(int *); //counts total score of given hand
int count_cards(int *); //counts number of cards in an array
int get_value(int); //gets value from card, face =10...etc
int get_new_card(); //gets a new card ensuring no duplicates
void clear_hand(int *); //sets all elements in array to -1 (to not be confused with 0 AC)
void load_deck(); //put numbers 0-51 in the deck
void shuffle_deck(); //shuffle the deck
char get_suite(int); //return suite
char get_rank(int); //return rank
/** printing methods */
void print_hand(int *, char *);//prints an array with custom text
void show_welcome(); //ascii art welcome msg
/** game logic methods */
void initial_deal(); //deal 1 card to house and 2 to player
void player_loop(); //handles all the player logic
void house_loop(); //handles all the house logic
void judgement(); //tallies scores and sees who one
/*
---------------------------------------------------
* Methods dealing with preparing/tallying of cards.
----------------------------------------------------
*/
int tally_hand(int *hand)
{
qsort (hand,0,count_cards(hand)-1); //quick sort to get ace in the front
int ncards, i, score, card_value;
score = 0;
ncards = count_cards(hand);
for (i = ncards-1; i >= 0; --i) //count down so ace is last card read. easier to judge 11 vs 1.
{
card_value = get_value(hand[i]);
if (card_value == -1)
if ((score + 11) > 21)
++score;
else
score += 11;
else
score += card_value;
}
return score;
}
int count_cards(int *hand)
{
int ncards;
for (ncards = 0; hand[ncards] != -1; ++ncards)
;
return ncards;
}
void clear_hand(int *hand)
{
int i;
for (i = 0; i < 10; ++i)
hand[i] = -1;
}
void load_deck()
{
int i;
for (i = 0; i < 52; ++i)
deck[i]=i;
}
void shuffle_deck()
{
int ntimes, firstcard, secondcard, tempcard;
ntimes = firstcard = secondcard = tempcard = 0;
//loop random amount between 100 and 199
for (ntimes = (99 + rand() % 100); ntimes > 0; --ntimes)
{
//get 2 random cards
firstcard = rand() % 52;
secondcard = rand() % 52;
//swap cards using temp var
tempcard = deck[firstcard];
deck[firstcard] = deck[secondcard];
deck[secondcard] = tempcard;
}
}
char get_suite(int card)
{
if (card < 13)
return 'C';
else if (card < 26)
return 'D';
else if (card < 39)
return 'H';
else if (card < 52)
return 'S';
else
return ' ';
}
char get_rank(int card)
{
int c;
c = card % 13;
switch (c)
{
case 0: return 'A';
case 1: return '2';
case 2: return '3';
case 3: return '4';
case 4: return '5';
case 5: return '6';
case 6: return '7';
case 7: return '8';
case 8: return '9';
case 9: return 'T';
case 10: return 'J';
case 11: return 'Q';
case 12: return 'K';
}
}
int get_value(int card)
{
int c;
c = card % 13;
if (c == 0)
return -1;
else if (c < 10)
return c+1;
else
return 10;
}
int get_new_card()
{
int card_index, new_card;
card_index = new_card = 0;
while (1)
{
card_index = rand() % 52;
if (deck[card_index] != -1)
{
new_card = deck[card_index];
deck[card_index] = -1;
return new_card;
}
}
}
void qsort(int a[], int lo, int hi)
{
int h, l, p, t;
if (lo < hi) {
l = lo;
h = hi;
p = a[hi];
do {
while ((l < h) && (a[l] <= p))
l = l+1;
while ((h > l) && (a[h] >= p))
h = h-1;
if (l < h) {
t = a[l];
a[l] = a[h];
a[h] = t;
}
} while (l < h);
a[hi] = a[l];
a[l] = p;
qsort( a, lo, l-1 );
qsort( a, l+1, hi );
}
}
/*
---------------------------------------------------
* Methods dealing with printing info to console
----------------------------------------------------
*/
void show_welcome()
{
printf(" ....,........ ..,...,+..,:..... \n");
printf(" . .,,............ .,..N.N.......,,,.... \n");
printf(" ..,... .............. ,..D,M............:,. \n");
printf(" ...,......................,.. ......?7=................:,.... \n");
printf(".. .,,. .............,..........David Nadeau...:O........,............,:,.... \n");
printf("... ... .,..........,........,....,. ,...=M....+N....8M=.............,,. \n");
printf(":....+...............,......,......... ..,.N.8...O88D=...:8DOD8.............~, \n");
printf("......+.............,......,........,. ..:..,.......~OO..,Z$ZNMN8DN?..........+ \n");
printf("......M...................,.......Blackjack..........I=.,.7$$$Z$~..M7...D.......,\n");
printf("? .M..I?...........M.... ............,. .~........,..,.77D$7D$........?I.......Z \n");
printf("+ .,.....,........MM.................. ,.......$+8=DD?DDZOZ8$....=..7?.......~I \n");
printf("~?.................NMN7.,.............,.,.......DZONDND8NO8MD..,=...,$8.......$= \n");
printf(",+7D..............:NNNNDN..............,.......O+DOND8NDD88DDONMO..?$$.......O+, \n");
printf(".~= ..............NNNDDDDDD88.................~DN8OMNNDN8MMMNMZN88.??....... I:. \n");
printf(".,? .............NDNDDDDD88888OZ......:.......M8~7O+M:.IMNDN$DMMNMDM........O~. \n");
printf("..=+............INDDDDD888888OOOZ....,......,NZNN7=NDMD:M77NMMMM?MNN.......=I, \n");
printf(" .:+..........,IDDDDD888888OOOOOZZ...........78ZD=,7NDO8ODMD8M7MZ=MNZ......$~. \n");
printf(" .,~?.........:DDDDD88888OOOOOZZZZ..,.......IOMOMDIM7M7OMONM+NZM?,+D......O+, \n");
printf(" .,+.........DDD88888O8O8OOZZZZZZ.........MNDM+MO8NZM$NONNNIMNMO78:......?:. \n");
printf(" ..~~........8888888OOO$7OZZZZ$I..:.......OM:+MMMMMNN.MMMO7$M=M=+$....,.O~. \n");
printf(" ..,?........D8888OOOO7,I,,.: ..:.........8D+MMM87INI.MM:8MM.8MI......?I,. \n");
printf(" ...==........8OO8OZZ$..Z$,..............$?7OMNN7I8MNNNNOI+N~IOZ...... 7~.. \n");
printf(" .,+. ........=Z7....:Z$$I....,........Z:..~$NN8N8NM8N8NO.N=8D......$=... \n");
printf(" .~I ...............$$$$$$77$........IO.......MNONO8N==O7N.D.......?,.. \n");
printf(" .,+...............$$$$$7,..,.......,?~..+?O..,$OMDNDZ+8M88...... O~.. \n");
printf(" ,~:............7$$I.,....:........7Z...::...D7$$ZDNMNN887......I?,.. \n");
printf(" ..,?..........+,.. ...,...~.......,,+...~.:.ZZ7OZ+,.............$~. . \n");
printf(" .~?...............,.,...~..........:ONOM8$$$$Z,...=7,.....$..Z=. \n");
printf(" :?...............,.,...:?,...........?DN8ONN...OOI?:..O,?. .?,. \n");
printf(" .,~?................,....,=$O,............$O87..,88D8..~... Z~.. \n");
printf(" .:+.. ........... .,......:~=ION................D:...M,...7?,.. \n");
printf(" .,~~...............,....... ,7???IZO?...............=?N..,7: . \n");
printf(" .,+ ..................??+=~,,...,:=I$8,.,.........M.D..Z=,. \n");
printf(" .=+........... ..~I+=~:,.... ....,:+I$Z,, ....=.~..,?, \n");
printf(" . :+=........~I?=~:...... . . . .,~+?ZO,......=7:. \n");
printf(" ..:=II::?I==:,,.. .. ..,~+7$8ZDZ?~. \n");
printf("\n Welcome to the famous David Nadeau Casino! \n\n");
}
void print_hand(int *hand, char *user)
{
int i, card_count;
card_count = count_cards(hand);
printf("%s: ", user);
for (i = 0; i < card_count; ++i)
{
printf("%c%c\t",get_rank(hand[i]), get_suite(hand[i]));
}
printf("\n");
}
/*
---------------------------------------------------
* Methods dealing with game logic
----------------------------------------------------
*/
void initial_deal()
{
house_hand[0] = get_new_card();
player_hand[0] = get_new_card();
player_hand[1] = get_new_card();
}
void player_loop()
{
int icard;//stores index where the store new cards will be placed in players hand
int score;
while (1)
{
printf("h/s: ");
getchar();
if ( getchar() == 'h')
{
icard = count_cards(player_hand);
player_hand[icard] = get_new_card();
printf("%c%c\n", get_rank(player_hand[icard]),get_suite(player_hand[icard]));
score = tally_hand(player_hand);
if (score > 21)
{
busted = 1;
break;
}
}else
break;
}
}
void house_loop()
{
int icard;//store amt of cards in house hand
while (1)
{
//make method to print total of array
if (tally_hand(house_hand) < 18)
{
icard = count_cards(house_hand);
house_hand[icard] = get_new_card();
}else
{
print_hand(house_hand,"Dealer");
break;
}
}
}
void judgement()
{
int houseScore, playerScore, house, player;
house = tally_hand(house_hand);
player = tally_hand(player_hand);
houseScore = 21-house;
playerScore = 21-player;
if (houseScore < 0)
printf("Player: %d, Dealer: %d You Win!\n",player,house);
else if (playerScore < houseScore)
printf("Player: %d, Dealer: %d You Win!\n",player,house);
else if (playerScore > houseScore)
printf("Player: %d, Dealer: %d You Lose!\n",player,house);
else if (playerScore == houseScore)
printf("Player: %d, Dealer: %d Push, no winner!\n",player,house);
}
/*
---------------------------------------------------
* Main
----------------------------------------------------
*/
int main()
{
show_welcome();
load_deck();
while (1)
{
printf("Want to play blackjack? (y/n): ");
if (getchar() == 'y')
{
clear_hand(player_hand);
clear_hand(house_hand);
load_deck();
shuffle_deck();
initial_deal();
print_hand(house_hand,"Dealer");
print_hand(player_hand,"Player");
player_loop();//go to the player logic loop
if (busted)
{
printf("Player: %d, Dealer: %d You Lose!\n",tally_hand(player_hand),tally_hand(house_hand));
busted = 0;
}
else
{
house_loop();//go to house logic loop
judgement();
}
} else
{
printf("Thanks for playing. \n");
break;
}
getchar();
}
return 0;
}