Repository navigation
Expand file tree
/
Copy pathgame.c
More file actions
319 lines (263 loc) · 8.68 KB
/
Copy pathgame.c
File metadata and controls
319 lines (263 loc) · 8.68 KB
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
#include <stdio.h>
#include <string.h>
#include <time.h>
#include "game.h"
#include "card.h"
#include "config.h"
#include "pile.h"
#include "util.h"
#include "game_rules.h"
#include "last_move.h"
static int calc_deck_size(const struct game_t *game)
{
int decks_required, total_cards, div, add;
total_cards = game->num_players * (game->num_cards_each * 3);
div = total_cards / DECK_SIZE;
add = ((total_cards % DECK_SIZE) > 0);
decks_required = div + add;
return (decks_required * DECK_SIZE);
}
static void create_deck(struct game_t *game)
{
int num_decks;
enum cardrank rank;
enum cardsuit suit;
int i = 0;
int j;
game->deck_size = calc_deck_size(game);
num_decks = game->deck_size / DECK_SIZE;
for (j = 0; j < num_decks; j++)
for (suit = HEARTS; suit <= CLUBS; suit++)
for (rank = TWO; rank <= ACE; rank++)
game->deck[i++] = make_card(rank, suit);
}
static void deal(struct game_t *game)
{
int i, j;
struct player_t *player = NULL;
struct card_t *deck = game->deck;
int *deck_size = &game->deck_size;
for (i = 0; i < game->num_players; i++) {
player = &game->players[i];
for (j = 0; j < game->num_cards_each; j++) {
deal_to_hand(player, deck[*deck_size]);
(*deck_size)--;
deal_to_face_up(player, deck[*deck_size]);
(*deck_size)--;
deal_to_face_down(player, deck[*deck_size]);
(*deck_size)--;
}
sort_cards(player->hand, player->hand_size);
}
}
static void shuffle(struct game_t *game)
{
int i, j;
struct card_t t;
if (game->deck_size > 1) {
for (i = 0; i < game->deck_size; i++) {
j = rand() / (RAND_MAX / game->deck_size + 1);
t = game->deck[j];
game->deck[j] = game->deck[i];
game->deck[i] = t;
}
}
}
static void play_from_hand(struct game_t *game, const struct card_t *to_lay,
const int num_cards)
{
int i;
struct player_t *player = &game->players[game->current_player];
for (i = 0; i < num_cards; i++) {
add_to_pile(game->pile, &game->pile_size, to_lay[i]);
remove_from_hand(player, &to_lay[i]);
if (game->deck_size > 0 && (player->hand_size < game->num_cards_each)) {
deal_to_hand(player, game->deck[game->deck_size-1]);
game->deck_size--;
}
}
sort_cards(player->hand, player->hand_size);
}
static void play_from_face_up(struct game_t *game, struct player_t *player,
const struct card_t *to_lay, const int num_cards)
{
int i;
for (i = 0; i < num_cards; i++) {
add_to_pile(game->pile, &game->pile_size, to_lay[i]);
remove_from_face_up(player, &to_lay[i]);
}
}
static void play_from_face_down(struct game_t *game, struct player_t *player,
const struct card_t *to_lay, const int num_cards)
{
int i;
for (i = 0; i < num_cards; i++) {
add_to_pile(game->pile, &game->pile_size, to_lay[i]);
remove_from_face_down(player, &to_lay[i]);
}
}
static void burn_pile(struct game_t *game)
{
game->burnt += game->pile_size;
game->pile_size = 0;
game->current_player--;
}
static void update_last_move(struct game_t *game, struct card_t *cards,
const int num_cards)
{
struct player_t *player = &game->players[game->current_player];
if (burn_cards_laid(game->pile, game->pile_size)) {
burn_pile(game);
set_last_move_was_burn(game->last_move, player->name);
}
else if (miss_a_go_card_laid(game->pile, game->pile_size)) {
game->miss_a_go = TRUE;
set_last_move_was_miss_a_go(game->last_move, player->name);
}
else {
set_last_move(game->last_move, player->name, cards, num_cards);
}
}
struct game_t make_game(const int num_players, char names[][MAX_NAME_LEN], char types[],
const int num_cards)
{
int i;
struct game_t game;
game.num_players = num_players;
game.num_cards_each = num_cards;
game.pile_size = 0;
game.deck_size = 0;
game.burnt = 0;
game.current_player = 0;
game.miss_a_go = FALSE;
for(i = 0; i<num_players; i++)
game.players[i] = make_player(names[i], types[i]);
return game;
}
void init_game(struct game_t *game)
{
create_deck(game);
shuffle(game);
deal(game);
}
void first_move(struct game_t *game)
{
int num_to_lay = 0;
struct card_t lowest;
struct card_t to_lay[MAX_HAND_SIZE];
game->current_player = 0;
struct card_t current_lowest = game->players[game->current_player].hand[0];
int i;
for (i = 1; i < game->num_players; i++) {
struct card_t player_lowest = game->players[i].hand[0];
if (card_cmp(&player_lowest, ¤t_lowest) == LT)
game->current_player = i;
}
lowest = game->players[game->current_player].hand[0];
for (i = 0; i < game->num_cards_each; i++)
if (lowest.rank == game->players[game->current_player].hand[i].rank)
to_lay[num_to_lay++] = game->players[game->current_player].hand[i];
play_from_hand(game, to_lay, num_to_lay);
set_last_move(game->last_move, game->players[game->current_player].name,
to_lay, num_to_lay);
move_to_next_player(game);
}
void make_move(struct game_t *game, const int card_choices[], const int num_choices)
{
int i = 0;
struct card_t to_lay[num_choices];
struct player_t *player = &game->players[game->current_player];
if (player->hand_size > 0) {
for (i = 0 ; i < num_choices ; i++)
to_lay[i] = player->hand[card_choices[i]];
play_from_hand(game, to_lay, num_choices);
}
else if (player->face_up_size > 0) {
for (i = 0; i < num_choices; i++)
to_lay[i] = player->face_up[card_choices[i]];
play_from_face_up(game, player, to_lay, num_choices);
}
else {
for (i = 0; i < num_choices; i++)
to_lay[i] = player->face_down[card_choices[i]];
play_from_face_down(game, player, to_lay, num_choices);
}
update_last_move(game, to_lay, num_choices);
}
void make_move_from_face_down(struct game_t *game, const int face_down_choice)
{
struct card_t to_lay[1];
struct player_t *player = &game->players[game->current_player];
to_lay[0] = player->face_down[face_down_choice];
play_from_face_down(game, player, to_lay, 1);
update_last_move(game, to_lay, 1);
}
int continue_play(const struct game_t *game)
{
int i, players_with_cards = 0;
for (i = 0; i < game->num_players; i++)
if (has_cards(&game->players[i]))
players_with_cards++;
return (players_with_cards > 1);
}
void get_shithead(const struct game_t *game, char *name)
{
int i;
for (i = 0; i < game->num_players; i++)
if (has_cards(&game->players[i])) {
strcpy(name, game->players[i].name);
return;
}
strcpy(name, game->players[0].name);
}
void move_to_next_player(struct game_t *game)
{
game->current_player++;
if (game->current_player == game->num_players)
game->current_player = 0;
while (!has_cards(&game->players[game->current_player])) {
game->current_player++;
if (game->current_player == game->num_players)
game->current_player = 0;
}
if (game->miss_a_go) {
game->current_player++;
if (game->current_player == game->num_players)
game->current_player = 0;
while (!has_cards(&game->players[game->current_player])) {
game->current_player++;
if (game->current_player == game->num_players)
game->current_player = 0;
}
}
game->miss_a_go = FALSE;
}
void pick_up_pile(struct game_t *game)
{
struct player_t *player = &game->players[game->current_player];
int i;
for (i = 0; i < game->pile_size; i++)
deal_to_hand(player, game->pile[i]);
sort_cards(player->hand, player->hand_size);
game->pile_size = 0;
set_last_move_pickup(game->last_move, player->name);
}
void pick_up_pile_and_face_down(struct game_t *game, const int face_down_choice)
{
struct player_t *player = &game->players[game->current_player];
const struct card_t card = player->face_down[face_down_choice];
pick_up_pile(game);
deal_to_hand(player, card);
sort_cards(player->hand, player->hand_size);
remove_from_face_down(player, &card);
}
int player_on_last_cards(const struct game_t *game)
{
const struct player_t player = game->players[game->current_player];
return ((player.hand_size == 0) && (player.face_up_size == 0));
}
struct player_helper_t get_player_helper(const struct game_t *game)
{
struct player_helper_t helper = make_helper(game->pile, game->pile_size);
return helper;
}