Put "capture three" logic in one place
This commit is contained in:
parent
453060ee07
commit
08823cc3b9
57
game.c
57
game.c
@ -72,8 +72,6 @@ void initialize_game(Game *g) {
|
||||
g->cards[i].selected = false;
|
||||
}
|
||||
|
||||
shuffle_hand(&g->deck);
|
||||
|
||||
g->player.points = 10 * g->kan_value;
|
||||
g->right.points = 10 * g->kan_value;
|
||||
g->left.points = 10 * g->kan_value;
|
||||
@ -123,6 +121,10 @@ bool is_player_turn(Game *g) {
|
||||
}
|
||||
|
||||
void handle_input(Game *g) {
|
||||
if (IsKeyPressed(KEY_R)) {
|
||||
g->state = GAME_STATE_INITIALIZING;
|
||||
return;
|
||||
}
|
||||
if (!is_player_turn(g)) return;
|
||||
if (g->dialog) return dialog_handle_input(g->dialog);
|
||||
|
||||
@ -193,6 +195,31 @@ void handle_input(Game *g) {
|
||||
}
|
||||
}
|
||||
|
||||
void capture_card_from_field(Game *g, Card *played, Card *target, Hand *hand, Hand *scored) {
|
||||
// capture all three cards if they play the fourth
|
||||
Card *same_month_card[3];
|
||||
int same_month_card_count = 0;
|
||||
for (int i = 0; i < g->field.count; i++) {
|
||||
Card *c = g->field.cards[i];
|
||||
if (c->month == played->month) {
|
||||
same_month_card[same_month_card_count++] = c;
|
||||
}
|
||||
}
|
||||
|
||||
remove_from_hand(hand, played);
|
||||
add_to_hand(scored, played);
|
||||
|
||||
if (same_month_card_count == 3) {
|
||||
for (int i = 0; i < 3; i++) {
|
||||
remove_from_hand(&g->field, same_month_card[i]);
|
||||
add_to_hand(scored, same_month_card[i]);
|
||||
}
|
||||
} else {
|
||||
remove_from_hand(&g->field, target);
|
||||
add_to_hand(scored, target);
|
||||
}
|
||||
}
|
||||
|
||||
void run_frame_ai_playing(Game *g) {
|
||||
Hand *hand = ¤t_player(g)->hand;
|
||||
Hand *scored = ¤t_player(g)->scored;
|
||||
@ -201,10 +228,7 @@ void run_frame_ai_playing(Game *g) {
|
||||
play.played->visible = true;
|
||||
|
||||
if (play.target) {
|
||||
remove_from_hand(hand, play.played);
|
||||
add_to_hand(scored, play.played);
|
||||
remove_from_hand(&g->field, play.target);
|
||||
add_to_hand(scored, play.target);
|
||||
capture_card_from_field(g, play.played, play.target, hand, scored);
|
||||
} else {
|
||||
remove_from_hand(hand, play.played);
|
||||
add_to_hand(&g->field, play.played);
|
||||
@ -233,6 +257,7 @@ void run_frame_initializing(Game *g) {
|
||||
g->current_play_from_hand = NULL;
|
||||
g->current_play_target = NULL;
|
||||
|
||||
g->deck.count = 0;
|
||||
for (int i = 0; i < 48; i++) {
|
||||
Card *c = &g->cards[i];
|
||||
c->visible = false;
|
||||
@ -265,6 +290,7 @@ void run_frame_dealing(Game *g) {
|
||||
} else if (g->field.count < 6) {
|
||||
deal(&g->deck, &g->field, 3, true);
|
||||
} else {
|
||||
// TODO: check for misdeals
|
||||
g->state = GAME_STATE_CALCULATING_FIELD_MULTIPLIER;
|
||||
}
|
||||
}
|
||||
@ -343,10 +369,16 @@ void run_frame_choosing_target(Game *g) {
|
||||
if (g->current_play_from_hand) {
|
||||
if (g->current_play_target) {
|
||||
g->current_play_from_hand->selected = false;
|
||||
remove_from_hand(&g->player.hand, g->current_play_from_hand);
|
||||
add_to_hand(&g->player.scored, g->current_play_from_hand);
|
||||
remove_from_hand(&g->field, g->current_play_target);
|
||||
add_to_hand(&g->player.scored, g->current_play_target);
|
||||
|
||||
capture_card_from_field(
|
||||
g,
|
||||
g->current_play_from_hand,
|
||||
g->current_play_target,
|
||||
&g->player.hand,
|
||||
&g->player.scored
|
||||
);
|
||||
|
||||
|
||||
g->current_play_from_hand = NULL;
|
||||
g->current_play_target = NULL;
|
||||
} else {
|
||||
@ -366,10 +398,7 @@ void run_frame_playing_from_deck(Game *g) {
|
||||
if (top_card->visible) {
|
||||
Card *target = valid_target(top_card, &g->field);
|
||||
if (target) {
|
||||
remove_from_hand(&g->field, target);
|
||||
add_to_hand(to_hand, target);
|
||||
remove_from_hand(&g->deck, top_card);
|
||||
add_to_hand(to_hand, top_card);
|
||||
capture_card_from_field(g, top_card, target, &g->deck, to_hand);
|
||||
} else {
|
||||
remove_from_hand(&g->deck, top_card);
|
||||
add_to_hand(&g->field, top_card);
|
||||
|
Loading…
Reference in New Issue
Block a user