-
Notifications
You must be signed in to change notification settings - Fork 0
/
console.c
308 lines (270 loc) · 6.39 KB
/
console.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
#include <stdio.h>
#include <string.h>
#include "player.h"
#include "game.h"
#include "console.h"
static int parse_choice(char choice[], int card_choices[], int *num_choices)
{
char *result = NULL;
int to_int;
result = strtok(choice, "," );
while( result != NULL ) {
to_int = atoi(result);
if (to_int == 0) {
return 0;
}
else {
card_choices[(*num_choices)++] = to_int-1;
result = strtok( NULL, "," );
}
}
return 1;
}
static int str_to_bool(char *choice)
{
return ((strcmp(choice, "y") == 0) | (strcmp(choice, "Y") == 0));
}
static void show_players(const struct player_t *players, const int num_players)
{
int i;
for (i = 0 ; i < num_players ; i++) {
show_player(&players[i]);
newline();
}
}
static void show_cards(const struct card_t *cards, const int num_cards)
{
int i;
int j = 1;
for (i = 0 ; i< num_cards ; i++) {
printf("(%d)%s of %s", j, show_rank(&cards[i]), show_suit(&cards[i]));
if (j <= (num_cards - 1))
printf(", ");
else
newline();
j++;
}
}
static void hide_cards(const int num_cards)
{
int i;
int j = 1;
for (i = 0 ; i< num_cards ; i++) {
printf("(%d)****", j);
if (j <= (num_cards - 1))
printf(", ");
else
newline();
j++;
}
}
static void show_deck(const int num_cards)
{
printf("%d left on deck", num_cards);
newlines(2);
}
static void show_burnt(const int num_cards)
{
printf("%d burnt", num_cards);
newlines(2);
}
static void show_pile(const struct card_t *cards, const int num_cards)
{
int i;
int j = 1;
printf("%d on pile:", num_cards);
newline();
for (i = (num_cards-1) ; i >= 0 ; i--) {
printf("\t");
if (i == (num_cards-1))
printf("(*)");
printf("%s of %s", show_rank(&cards[i]), show_suit(&cards[i]));
newline();
j++;
}
newline();
}
static void show_last_move(const char *move)
{
printf("%s", move);
}
void newline()
{
printf("\n");
}
void newlines(const int num)
{
int i;
for (i = 0 ; i < num ; i++)
newline();
}
void show_welcome_msg()
{
printf("Welcome to C-Head!");
newlines(2);
}
void show_player(const struct player_t *player)
{
printf("Player name : %s", player->name);
newline();
if (player->hand_size > 0) {
printf("HAND : ");
show_cards(player->hand, player->hand_size);
}
if (player->face_up_size > 0) {
printf("FACE UP : ");
show_cards(player->face_up, player->face_up_size);
}
if (player->face_down_size > 0) {
printf("FACE DOWN: ");
hide_cards(player->face_down_size);
}
}
void clearscreen()
{
newlines(100);
}
int request_num_players()
{
int result;
printf("Enter number of players : ");
scanf("%d", &result);
return result;
}
int request_num_cards_each()
{
int result;
printf("Enter number of cards each : ");
scanf("%d", &result);
return result;
}
void request_player_details(char names[][MAX_NAME_LEN], char types[], const int num_players)
{
char *name = malloc(sizeof(char) * MAX_NAME_LEN);
char *type = malloc(sizeof(char) * 2);
int i;
for (i = 0 ; i < num_players ; i++) {
newline();
printf("Enter name for player %d : ", i+1);
scanf("%s", name);
strcpy(names[i], name);
printf("Enter player type (h/l/r/p) : ");
scanf("%s", type);
types[i] = type[0];
}
free(name);
free(type);
newline();
}
int request_swap_cards(const char *name)
{
int result;
char *choice = malloc(sizeof(char) * 2);
newline();
printf("%s, would you like to swap cards (y/n) : ", name);
scanf("%s", choice);
result = str_to_bool(choice);
free(choice);
return result;
}
int request_swap_more(const char *name)
{
int result = 0;
char *choice = malloc(sizeof(char) * 2);
newline();
printf("%s, swap more (y/n) : ", name);
scanf("%s", choice);
result = str_to_bool(choice);
free(choice);
return result;
}
int request_hand_swap(const int size)
{
int choice;
printf("Hand card to swap (1-%d): ", size);
scanf("%d", &choice);
return choice;
}
int request_face_down_move(const struct player_t *player)
{
int choice;
newline();
printf("%s, pick a card to play (1-%d): ", player->name, player->face_down_size);
scanf("%d", &choice);
return choice;
}
int request_faceup_swap(const int size)
{
int choice;
printf("Face up card to swap (1-%d): ", size);
scanf("%d", &choice);
return choice;
}
void show_game_summary(const struct game_t *game)
{
show_pile(game->pile, game->pile_size);
show_deck(game->deck_size);
show_burnt(game->burnt);
show_players(game->players, game->num_players);
show_last_move(game->last_move);
wait_user();
}
void request_move(const struct player_t *player, int card_choices[], int *num_choices)
{
char entered[10];
printf("%s, choose cards to lay: ", player->name);
scanf("%s", entered);
while (!parse_choice(entered, card_choices, num_choices)) {
*num_choices = 0;
newline();
printf("Enter card choices seperated by commas.");
newlines(2);
printf("%s, choose cards to lay: ", player->name);
scanf("%s", entered);
}
}
void show_shithead(char *player)
{
newline();
printf("!!!! GAME OVER !!!!");
newlines(2);
printf("SHITHEAD == %s", player);
newlines(2);
}
void wait_user()
{
printf("Press ENTER to continue.");
int ch;
do
ch = fgetc(stdin);
while (ch != EOF && ch != '\n');
clearerr(stdin);
fflush(stdout);
getchar();
}
void show_bad_move(void)
{
newline();
printf("You can't do that, try again");
newline();
wait_user();
}
void show_pickup(const char *name)
{
newline();
printf("Oh dear, %s, you must pick up, press enter.", name);
newline();
}
void show_pickup_from_face_down(const struct card_t *card)
{
newline();
printf("Oh no, you picked the %s of %s, you must pickup, press enter.",
show_rank(card), show_suit(card));
newline();
}
void show_can_move_from_face_down(const struct card_t *card)
{
newline();
printf("Whew! you picked the %s of %s, press enter.", show_rank(card), show_suit(card));
newline();
}