-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgame_play.c
256 lines (214 loc) · 6.06 KB
/
game_play.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
#include "game_play.h"
#include "player.h"
#include "common.h"
#include "define.h"
#define PAUSE_FRAMES_PLAYERS 30
#define PAUSE_FRAMES_CARDS 20
/*extern data*/
extern type_t current_player;
extern SDL_Renderer *renderer;
/*local data*/
static card_num current_card = EMPTY; /*used when player is taking cards*/
static int nb_frames = 0;
/*global data*/
player_state_type state = NO_VALID_INPUT; /*player turn states*/
card_num dropped_card = EMPTY; /* last card dropped */
player *last_card_taker = NULL;
static bool take_card(player *p, card table[])
{
int index = exist(table, MAX_NB_CARDS_TABLE, current_card);
if (index != -1) {
if (passed(PAUSE_FRAMES_CARDS, &nb_frames)) {
/*the card exists and it's time*/
SDL_DestroyTexture(table[index].tex);
set_card(&table[index], EMPTY, -1, -1, 0);
p->score.gained_cards++;
if (current_card != 9)
current_card++;
}
return 1;
} else {
/*no cards to take*/
if (empty(table, MAX_NB_CARDS_TABLE)) {
p->score.points++;
add_bonus(p, MESA, -1);
}
current_card = EMPTY;
return 0;
}
}
void update_state(player p, card table[])
{
card_num selected_hand = get_sel_hand_val(p);
card_num selected_table = table[p.sel_table].value;
if ((selected_table == EMPTY) &&
(exist(table, MAX_NB_CARDS_TABLE, selected_hand) == -1))
state = PUT_CARD;
else if ((selected_table != EMPTY) &&
(equal(selected_hand, selected_table)))
state = GET_FIRST_CARD;
else
state = NO_VALID_INPUT;
}
void player_turn(player *p, card table[])
{
card_num selected_hand = get_sel_hand_val(*p);
card_num selected_table = table[p->sel_table].value;
/*what will the player do, depending on the states*/
switch (state) {
case NO_VALID_INPUT:
if ((p->sel_hand != -1) && (p->sel_table != -1))
update_state(*p, table);
break;
case PUT_CARD:
if (!passed(PAUSE_FRAMES_PLAYERS, &nb_frames) && (p->type == COMPUTER))
break;
/*moving the card from hand to table*/
if (p->type == USER)
table[p->sel_table].tex = get_sel_hand_tex(*p);
else
table[p->sel_table].tex = load_image(get_card_file(selected_hand), renderer);
table[p->sel_table].value = selected_hand;
dropped_card = selected_hand;
set_card(&p->hand[p->sel_hand], EMPTY, -1, -1, 0);
p->nb_cards_in_hand--;
/*no further actions*/
state = END_ACTIONS;
break;
case GET_FIRST_CARD:
if (!passed(PAUSE_FRAMES_PLAYERS, &nb_frames) && (p->type == COMPUTER))
break;
if (selected_table == dropped_card) {
p->score.points++;
add_bonus(p, ESTE, selected_table);
}
dropped_card = EMPTY;
last_card_taker = p;
/*removing both cards from hand/table */
set_card(&p->hand[p->sel_hand], EMPTY, -1, -1, 0);
SDL_DestroyTexture(table[p->sel_table].tex);
set_card(&table[p->sel_table], EMPTY, -1, -1, 0);
p->score.gained_cards += 2;
p->nb_cards_in_hand--;
card_num tmp = selected_hand % 10;
/*take the rest of the cards if possible*/
if (tmp != 9) {
state = GET_CARDS;
current_card = tmp + 1;
} else {
if (empty(table, MAX_NB_CARDS_TABLE)) {
p->score.points++;
add_bonus(p, MESA, -1);
}
state = END_ACTIONS;
}
break;
case GET_CARDS:
p->sel_hand = p->sel_table = -1;
if (!take_card(p, table))
state = END_ACTIONS;
break;
case END_ACTIONS:
current_player = !p->type;
p->sel_hand = p->sel_table = -1;
state = NO_VALID_INPUT;
break;
}
}
static unsigned short get_gain(card_num c, card table[])
{
unsigned short count = 0;
/*res: the more cards/points the player can gain,
the more is the variable's value */
unsigned short res = 1;
/*how many cards can the player get*/
if (c % 10 != 9) {
short index = exist(table, MAX_NB_CARDS_TABLE, c);
while ((index != -1) && (count <= (9 - c%10))) {
count++;
index = exist(table, MAX_NB_CARDS_TABLE, c+count);
}
}
res += count;
/*we make sure this is a better gain than in taking 2 cards*/
if (equal(c, dropped_card))
res += 2;
return res;
}
static short get_index_ai(player *p, card table[], short *index_tab)
{
unsigned short i, j;
unsigned short tmp_gain, gain = 0;
short best_card_index = -1;
*index_tab = -1;
/*let's choose the best card (the best gain)*/
for (i=0; i < MAX_NB_CARDS_HAND; i++) {
card_num tmp = p->hand[i].value;
if (tmp == EMPTY)
continue;
for (j=0; j < MAX_NB_CARDS_TABLE; j++) {
if (!equal(tmp, table[j].value))
continue;
tmp_gain = get_gain(tmp, table);
if (gain < tmp_gain) {
gain = tmp_gain;
best_card_index = i;
*index_tab = j;
}
}
}
return best_card_index;
}
void set_computer_choice(player *p, card table[])
{
short index_tab;
short index_hand = get_index_ai(p, table, &index_tab);
if (index_hand == -1) {
short i = rand_a_b(0, 3);
while (p->hand[i].value == EMPTY)
i = rand_a_b(0, 3);
index_hand = i;
}
if (index_tab == -1) {
short i = rand_a_b(0, 10);
while (table[i].value != EMPTY)
i = rand_a_b(0, 10);
index_tab = i;
}
p->sel_hand = index_hand;
p->sel_table = index_tab;
}
void take_all_cards(player *p, card table[])
{
unsigned short i;
for (i = 0; i < MAX_NB_CARDS_TABLE; ++i)
if (table[i].value != EMPTY){
SDL_DestroyTexture(table[i].tex);
set_card(&table[i], EMPTY, -1, -1, 0);
p->score.gained_cards++;
}
}
void handle_bonus(score_t *p1_score, bonus_t *p1_bonus, score_t *p2_score, bonus_t *p2_bonus)
{
if ((p1_bonus->type == NONE) && (p2_bonus->type == NONE))
return;
/*check "HowToPlay" for points calculation*/
if (p1_bonus->type > p2_bonus->type) {
p1_score->points += ((p1_bonus->type == RONDA)? 1:5) +
((p2_bonus->type == NONE)? 0:1);
} else if (p1_bonus->type < p2_bonus->type) {
p2_score->points += ((p2_bonus->type == RONDA)? 1:5) +
((p1_bonus->type == NONE)? 0:1);
} else if (p1_bonus->bonus_card > p2_bonus->bonus_card) {
p1_score->points += (p1_bonus->type == RONDA)? 2:10;
} else if (p1_bonus->bonus_card < p2_bonus->bonus_card) {
p2_score->points += (p2_bonus->type == RONDA)? 2:10;
} else {
p1_score->points += (p1_bonus->type == RONDA)? 1:5;
p2_score->points += (p1_bonus->type == RONDA)? 1:5;
}
p1_bonus->bonus_card = -1;
p1_bonus->type = NONE;
p2_bonus->bonus_card = -1;
p2_bonus->type = NONE;
}