-
Notifications
You must be signed in to change notification settings - Fork 0
/
black.c
222 lines (155 loc) · 4.43 KB
/
black.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
#include "black.h"
#include <stdio.h>
#include <ctype.h>
#define SCREEN_LINE_COUNT 25
enum outcomes {NO_WINNER, PLY_WINS, BLACKJACK, CPU_WINS, TIE};
extern struct card deck[CARDS];
int init_game(struct black *table) {
table->player[PLY].total = table->player[CPU].total = 0;
table->player[PLY].hand = table->player[CPU].hand = 0;
table->player[PLY].aces = table->player[CPU].aces = 0;
table->check_stand = 0;
table->end = 0;
if(bet(table) == 0)
return 0;
printf("cards dealt: %d\n",table->cards_dealt);
/* Deal 1st hand: first two cards */
unsigned int i;
for (i = 0; i < 2; i++)
{
getCard(table, CPU);
getCard(table, PLY);
}
findWinner(table);
return 1;
}
void assignPoints(struct player_data *player) {
if (player->card_value == 11)
player->aces++;
if ( (player->aces > 0) && ( (player->total + player->card_value) > 21) ) {
player->total += player->card_value;
player->total = player->total - 10; /* Ace now counts as 1 */
player->aces--;
}
else
player->total += player->card_value;
}
int findWinner (struct black *table) {
enum outcomes winner;
if (table->check_stand == 1) {
if (table->player[CPU].total == table->player[PLY].total) /* Tie */
winner = TIE;
else if (table->player[CPU].total <= 21) {
if (table->player[PLY].total > table->player[CPU].total) /* Player wins */
winner = PLY_WINS;
else
winner = CPU_WINS;
}
else /* Dealer wins player->total > 21 */
winner = CPU_WINS;
}
else {
if ( (table->player[PLY].total == 21) && (table->player[CPU].total == 21) ) /* A tie */
winner = TIE;
else if (table->player[PLY].total > 21) /* If the player exceeds a sum of 21 "busts", loses, even if the dealer also exceeds 21 */
winner = CPU_WINS;
else if (table->player[PLY].total == 21)
winner = PLY_WINS;
else if (table->player[CPU].total == 21) /* Dealer wins */
winner = CPU_WINS;
else if (table->player[CPU].total > 21)
winner = PLY_WINS;
else
winner = NO_WINNER; /* no winner found */
}
if (winner != NO_WINNER) {
table->end = 1;
if (winner == BLACKJACK) {
table->credit += (table->bet * 3)/2;
printf("Congratulation you win: \n\n");
}
else if (winner == PLY_WINS) {
table->credit += table->bet;
printf("Congratulation you win: \n\n");
}
else if (winner == CPU_WINS) {
table->credit -= table->bet;
printf("Dealer wins \n\n");
}
else
printf("No one wins\n");
if (table->credit <= 0) {
table->credit = 50;
printf("Dealer wins. Sorry you bankrupted\n\n");
}
printCards(table);
printf("\nCredit: %d$\n", table->credit);
printf("Enter a key to start a new game, x or q to quit\n");
getchar();
clearScreen();
int ans;
do {
ans = getchar();
printf("ans = %d\n", ans);
if (ans == 'q' || ans =='x')
return 0;
else
break;
} while(1);
init_game(table);
}
return 1;
}
void getCard(struct black *table, enum players id)
{
if (table->cards_dealt == CARDS)
{
shuffle(deck);
table->cards_dealt = 0;
}
if (table->player[CPU].hand == 0 && id == CPU)
{
table->player[CPU].card[table->player[id].hand] = deck[table->cards_dealt].symbol;
table->covered_card = deck[table->cards_dealt].symbol;
}
else
table->player[id].card[table->player[id].hand] = deck[table->cards_dealt].symbol;
table->player[id].card_value = deck[table->cards_dealt].value;
assignPoints(&table->player[id]);
table->player[id].hand++;
table->cards_dealt++;
}
void printCards(struct black *table) {
unsigned int i;
clearScreen();
for (i = 0; i < table->player[CPU].hand; i++) { /* print Dealer's cards */
if (table->end == 0 && i == 0) {
printf("%s ", "🂠");
continue;
}
else
printf("%s ", table->player[CPU].card[i]);
}
printf("\n\n");
for (i = 0; i < table->player[PLY].hand; i++) /* print Player's cards */
printf("%s ", table->player[PLY].card[i]);
}
int bet (struct black *table) {
printf("What's your bet?\n");
int ans;
while ( (scanf("%d", &table->bet) != 1) || (table->bet > table->credit) ) {
printf ("Enter a valid sum, your credit is %d$:\n", table->credit);
ans= getchar();
if(ans == 'x' || ans == 'q')
return 0;
}
return 1;
}
void clearScreen(void)
{
unsigned int i;
while (i < SCREEN_LINE_COUNT) {
printf("\n"); /* Clears screen by printing a number of blank lines */
i++;
}
}