-
Notifications
You must be signed in to change notification settings - Fork 0
/
game.c
247 lines (241 loc) · 5.24 KB
/
game.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
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <string.h>
#include "structs.h"
#include "hand.h"
#include "printer.h"
/**
* Print out the option menu
*/
void print_menu(void)
{
printf("[1] = Hit\n");
printf("[2] = Hold\n");
}
/**
* Play game
*
* @param deck deck to use
* @param player1 first player hand
* @param player2 second player hand
*/
void interactive(deck *deck, hand *player1, hand *player2)
{
add_card(player1, deck);
add_card(player2, deck);
add_card(player1, deck);
add_card(player2, deck);
while (true)
{
if (contains_blackjack(player1))
{
printf("Player 1 got blackjack!\n");
break;
}
else if (contains_blackjack(player2))
{
printf("Player 2 got blackjack!\n");
break;
}
if (!player1->held && !player1->has_busted)
{
char choice;
printf("Player 1: ");
print_hand(player1);
printf("\n");
print_menu();
scanf("%c", &choice);
while (choice != '1' && choice != '2')
{
printf("Invalid choice, try again!\n");
print_menu();
scanf("%c", &choice);
}
if (choice == '1')
{
add_card(player1, deck);
if (has_busted(player1))
{
bust(player1);
}
}
else if (choice == '2')
{
hold(player1);
}
}
if (!player2->held && !player2->has_busted)
{
char choice;
printf("Player 2: ");
print_hand(player2);
printf("\n");
print_menu();
scanf("%c", &choice);
while (choice != '1' && choice != '2')
{
printf("Invalid choice, try again!\n");
print_menu();
scanf("%c", &choice);
}
if (choice == '1')
{
add_card(player2, deck);
if (has_busted(player2))
{
bust(player2);
}
}
else if (choice == '2')
{
hold(player2);
}
}
if ((player1->held || player1->has_busted) && (player2->held || player2->has_busted))
{
break;
}
}
if (!contains_blackjack(player1) && !contains_blackjack(player2))
{
if (get_hand_value(player1) > get_hand_value(player2) && !player1->has_busted)
{
printf("Player 1 wins!\n");
}
else if (get_hand_value(player1) > get_hand_value(player2) && player1->has_busted)
{
printf("Player 2 wins!\n");
}
else if (get_hand_value(player1) < get_hand_value(player2) && !player2->has_busted)
{
printf("Player 2 wins!\n");
}
else if (get_hand_value(player1) < get_hand_value(player2) && player2->has_busted)
{
printf("Player 1 wins!\n");
}
else
{
printf("Tie game!\n");
}
}
printf("The hands were: \nPlayer1: ");
print_hand(player1);
printf("\n");
printf("Player 2: ");
print_hand(player2);
printf("\n");
}
/**
* Single player, play against a computer
*
* @param deck deck to use
* @param player player's hand to use
* @param cpu cpu hand to use
*/
void single_player(deck *deck, hand *player, hand *cpu, bool show_hand)
{
add_card(player, deck);
add_card(cpu, deck);
add_card(player, deck);
add_card(cpu, deck);
while (true)
{
if (contains_blackjack(player))
{
printf("You got blackjack!\n");
break;
}
else if (contains_blackjack(cpu))
{
printf("The Computer got blackjack!\n");
break;
}
if (!player->held && !player->has_busted)
{
char choice;
printf("Your turn: ");
print_hand(player);
printf("\n");
print_menu();
scanf("%c", &choice);
while (choice != '1' && choice != '2')
{
printf("Invalid choice, try again!\n");
print_menu();
scanf("%c", &choice);
}
if (choice == '1')
{
add_card(player, deck);
if (has_busted(player))
{
bust(player);
}
}
else if (choice == '2')
{
hold(player);
}
}
if (!cpu->held && !cpu->has_busted)
{
printf("Computer Choice: ");
if (get_hand_value(cpu) < 16)
{
if (show_hand)
{
print_hand(cpu);
printf("\n");
}
printf("Hit!\n");
add_card(cpu, deck);
if (has_busted(cpu))
{
bust(cpu);
}
}
else
{
printf("Hold!\n");
printf("Computer hand sums to at least 16, holding!\n");
hold(cpu);
}
}
if ((player->held || player->has_busted) && (cpu->held || cpu->has_busted))
{
break;
}
}
printf("\n");
if (!contains_blackjack(player) && !contains_blackjack(cpu))
{
if (get_hand_value(player) > get_hand_value(cpu) && !player->has_busted)
{
printf("You win!\n");
}
else if (get_hand_value(player) > get_hand_value(cpu) && player->has_busted)
{
printf("Computer wins!\n");
}
else if (get_hand_value(player) < get_hand_value(cpu) && !cpu->has_busted)
{
printf("Computer wins!\n");
}
else if (get_hand_value(player) < get_hand_value(cpu) && cpu->has_busted)
{
printf("You win!\n");
}
else
{
printf("Tie game!\n");
}
}
printf("\n");
printf("The hands were: \nYou: ");
print_hand(player);
printf("\nComputer: ");
print_hand(cpu);
printf("\n");
}