-
Notifications
You must be signed in to change notification settings - Fork 0
/
player.c
152 lines (128 loc) · 4.18 KB
/
player.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
#include <string.h>
#include <stdio.h>
#include "player.h"
#include "player_types.h"
#include "card.h"
struct player_t make_player(const char *name, const char type)
{
struct player_t player;
strcpy(player.name, name);
if (type == 'h')
player.is_computer = FALSE;
else
player.is_computer = TRUE;
player.hand_size = 0;
player.face_up_size = 0;
player.face_down_size = 0;
switch(type) {
case 'r':
player.ask_swap_cards = randomcardplayer_ask_swap_cards;
player.ask_swap_choice = randomcardplayer_ask_swap_choice;
player.ask_move = randomcardplayer_ask_move;
player.ask_face_down_move = randomcardplayer_ask_face_down_move;
break;
case 'l':
player.ask_swap_cards = lowcardplayer_ask_swap_cards;
player.ask_swap_choice = lowcardplayer_ask_swap_choice;
player.ask_move = lowcardplayer_ask_move;
player.ask_face_down_move = lowcardplayer_ask_face_down_move;
break;
case 'h':
player.ask_swap_cards = human_ask_swap_cards;
player.ask_swap_choice = human_ask_swap_choice;
player.ask_move = human_ask_move;
player.ask_face_down_move = human_ask_face_down_move;
break;
case 'p':
player.ask_swap_cards = pyromaniac_ask_swap_cards;
player.ask_swap_choice = pyromaniac_ask_swap_choice;
player.ask_move = pyromaniac_ask_move;
player.ask_face_down_move = pyromaniac_ask_face_down_move;
break;
default:
player.ask_swap_cards = NULL;
player.ask_swap_choice = NULL;
player.ask_move = NULL;
player.ask_face_down_move = NULL;
}
return player;
}
struct player_helper_t make_helper(const struct card_t *pile, const int pile_size)
{
struct player_helper_t helper;
int i;
for (i = 0; i < pile_size; i++)
helper.pile[i] = pile[i];
helper.pile_size = pile_size;
return helper;
}
void deal_to_hand(struct player_t *player, const struct card_t card)
{
add_card_to_cards(player->hand, &player->hand_size, card);
}
void deal_to_face_up(struct player_t *player, const struct card_t card)
{
add_card_to_cards(player->face_up, &player->face_up_size, card);
}
void deal_to_face_down(struct player_t *player, const struct card_t card)
{
add_card_to_cards(player->face_down, &player->face_down_size, card);
}
void remove_from_hand(struct player_t *player, const struct card_t *card)
{
remove_card_from_cards(player->hand, &player->hand_size, card);
}
void remove_from_face_up(struct player_t *player, const struct card_t *card)
{
remove_card_from_cards(player->face_up, &player->face_up_size, card);
}
void remove_from_face_down(struct player_t *player, const struct card_t *card)
{
remove_card_from_cards(player->face_down, &player->face_down_size, card);
}
void find_lowest_card_by_player(const struct player_t *players,
const int num_players, struct card_t *cards)
{
int i;
for (i = 0 ; i < num_players ; i++)
cards[i] = find_lowest_card(players[i].hand, players[i].hand_size);
}
void swap(struct player_t *player, const struct swap_choice_t *choice)
{
struct card_t temp;
temp = player->hand[choice->hand_choice-1];
player->hand[choice->hand_choice-1] = player->face_up[choice->faceup_choice-1];
player->face_up[choice->faceup_choice-1] = temp;
sort_cards(player->hand, player->hand_size);
}
int has_cards(const struct player_t *player)
{
if (player->hand_size > 0)
return TRUE;
if (player->face_up_size > 0)
return TRUE;
if (player->face_down_size > 0)
return TRUE;
return FALSE;
}
int has_cards_in_hand(const struct player_t *player)
{
return player->hand_size > 0;
}
int ask_swap_cards(const struct player_t *player)
{
return player->ask_swap_cards(player);
}
struct swap_choice_t ask_swap_choice(const struct player_t *player)
{
return player->ask_swap_choice(player);
}
void ask_move(const struct player_t *player, const struct player_helper_t *helper,
int card_choices[], int *nchoices)
{
return player->ask_move(player, helper, card_choices, nchoices);
}
int ask_face_down_move(const struct player_t *player)
{
return player->ask_face_down_move();
}