-
Notifications
You must be signed in to change notification settings - Fork 0
/
21.c
234 lines (196 loc) · 5.65 KB
/
21.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
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <string.h>
//represents a card -- 52 per deck
typedef struct card {
char * suit;
char * rank;
} card;
//defines a player of the game
typedef struct player {
char * playersName;
int playersChipsInUSD;
int currentBet;
int handValue;
} player;
//defines a dealer
typedef struct dealer {
int handValue;
} dealer;
//score any card and add it
//to the players total
int scoreCard (card * c) {
char * rank = c->rank;
if(!strcmp(rank, "1")) return 1;
if(!strcmp(rank, "2")) return 2;
if(!strcmp(rank, "3")) return 3;
if(!strcmp(rank, "4")) return 4;
if(!strcmp(rank, "5")) return 5;
if(!strcmp(rank, "6")) return 6;
if(!strcmp(rank, "7")) return 7;
if(!strcmp(rank, "8")) return 8;
if(!strcmp(rank, "9")) return 9;
if(!strcmp(rank, "10")) return 10;
if(!strcmp(rank, "J")) return 10;
if(!strcmp(rank, "Q")) return 10;
if(!strcmp(rank, "K")) return 10;
// if(!strcmp(rank, "A")) scoreAce();
}
//makes a new deck of cards
card * makeDeck() {
int sizeOfDeck = 52;
int numOfSuits = 4;
int numOfRanks = 13;
char * suitsArray[] = {"C", "D", "H", "S"};
char * rankArray[] = {"A", "2", "3", "4", "5", "6", "7", "8", "9", "10", "J", "Q", "K"};
//allocate memory on the heap for an array of card structs (a deck of cards)
card * deck = malloc(sizeof(card) * sizeOfDeck);
int i, j;
int k = 0;
for(i = 0; i < numOfSuits; i++){
for(j = 0; j < numOfRanks; j++){
deck[k].suit = suitsArray[i];
deck[k].rank = rankArray[j];
k++;
}
}
return deck;
}
//prints the deck of cards. Useful for debugging if necessary
void printDeck(card * theDeckPointer) {
int i = 0;
for (i = 0; i < 52; i++) {
printf("%s%s\n", theDeckPointer[i].rank, theDeckPointer[i].suit);
}
}
//deal a random card from the deck any time a player or dealer
//asks to hit or is dealt hand
card * dealRandomCard(card * aDeck) {
card * cardDealt;
//static float i = 0;
int x = rand() % 53;
cardDealt = &(aDeck[x]);
return cardDealt;
}
//free the memory allocated in the creation of the deck and player
void freeAll(card * deckToFree, player * playerToFree) {
int i = 0;
while (i < 52) {
free(deckToFree + i);
i++;
}
free(playerToFree);
}
//the welcom message to be printed at the start of the program
void printWelcome() {
printf("WELCOME TO THE GAME OF 21!\n");
}
//prints the current player and his/her
//chip count to the screen
void printPlayer(player * playerToPrint) {
printf("The current player's name is: %s", playerToPrint->playersName);
printf("The current player's chip count is: $%d\n", playerToPrint->playersChipsInUSD);
printf("The current player's bet is: $%d\n", playerToPrint->currentBet);
}
//welcomes and initializes a new player with a name and
//an initial chip count of 500 USD
player * newPlayer() {
player * newPlyr = malloc(sizeof(player));
char * name = malloc(sizeof(char) * 50);
printf("Please enter your name (and press enter): \n");
fgets(name, sizeof(name) * 50, stdin);
printf("\n");
printf("You'll start with $500 worth of chips. You may bet as little or as much as you'd like on each hand. \n");
printf("A win returns 1X your total bet, while blackjack returns 1.5X your total bet.\n");
printf("The dealer will hit on anything less than 17 and stand on any hand greater or equal to 17.\n");
printf("Good luck %s\n", name);
newPlyr->playersName = name;
newPlyr->playersChipsInUSD = 500;
return newPlyr;
}
//print a card
void printCard(card * c) {
printf("Rank: %s\n", c->rank);
printf("Suit: %s\n", c->suit);
}
//initializes the game with creation of a deck
//and printing of the welcome message
card * initializeGame() {
printf("\n");
card * deckOfCardsPointer;
deckOfCardsPointer = makeDeck();
printWelcome();
return deckOfCardsPointer;
}
void placeBet(player * p) {
char line[25];
int bet;
printf("Enter your bet (then hit 'enter'): \n");
if (fgets(line, sizeof(line), stdin)) {
if (1 == sscanf(line, "%d", &bet)) {
p->currentBet = bet;
}
}
}
void dealDealer(dealer * d, card * deckOfCards) {
d->handValue = 0;
card * firstCard = dealRandomCard(deckOfCards);
int firstVal = scoreCard(firstCard);
d->handValue = firstVal;
printf("Dealer's first card dealt was %s%s. (Other's will remain hidden.)\n\n", firstCard->rank, firstCard->suit);
while (d->handValue < 17) {
card * newCard = dealRandomCard(deckOfCards);
int val = scoreCard(newCard);
d->handValue = d->handValue + val;
newCard = NULL;
}
}
void dealPlayer(player * p, card * deckOfCards) {
char hitOrStay[6];
p->handValue = 0;
card * dealt = dealRandomCard(deckOfCards);
int firstVal = scoreCard(dealt);
p->handValue = firstVal;
printf("%d ", p->handValue);
while(1) {
printf("You were dealt a %s%s\n", dealt->rank, dealt->suit);
printf("Would you like to hit or stay?\n");
fgets(hitOrStay, sizeof(char) * 6, stdin);
if (strcmp(hitOrStay, "hit")) {
break;
}
dealt = dealRandomCard(deckOfCards);
int val = scoreCard(dealt);
p->handValue = p->handValue + val;
printf("%d ", p->handValue);
}
}
void playNewHand(player * p, dealer * d, card * deckOfCards) {
placeBet(p);
dealDealer(d, deckOfCards);
dealPlayer(p, deckOfCards);
//deal both dealer and player
//check for duplicates
//score both hands once player stays
//recalculate players bank account and reset to defaults
p->handValue = 0;
d->handValue = 0;
//free hands
}
void cashOut() {
}
//the standard main method, for the blackjack game
void main() {
srand(time(NULL));
card * deckOfCards = initializeGame();
player * p = newPlayer();
dealer * d = malloc(sizeof(dealer));
// char * keepPlaying;
// while () {
playNewHand(p, d, deckOfCards);
// }
// cashOut();
// //free deck and player
// return 1;
}