diff options
-rw-r--r-- | web/web_control.c | 46 | ||||
-rw-r--r-- | web/web_match.c | 47 | ||||
-rw-r--r-- | web/web_match.h | 9 |
3 files changed, 58 insertions, 44 deletions
diff --git a/web/web_control.c b/web/web_control.c index af5723a..4d62c05 100644 --- a/web/web_control.c +++ b/web/web_control.c @@ -28,11 +28,6 @@ void set_active_player(int pn) prompt_visit(); } -void toggle_active_player() -{ - set_active_player(3 - state->active_player); -} - void update_user_rem_from_pts(int pts) { update_player_rem(state->active_player, state_active_leg()->rem - pts); @@ -144,7 +139,7 @@ void handle_next() set_active_player(1); } else { if (state->active_player) - toggle_active_player(); + set_active_player(match_next_player()); else set_active_player(match_opts->throws_first); @@ -185,27 +180,11 @@ void user_visit_to_rem(int rem) user_visit(state->legs[0]->rem - rem); } -static int prev_throw_player() -{ - if (match_is_over()) - return state->active_player; - - if (state->active_player == 1) - return match_num_players(); - else - return state->active_player - 1; -} - -static bool player_is_comp(int pn) -{ - return state->mode == M_PVC && pn == 2; -} - static void undo_active() { struct leg *l = state_active_leg(); - if (player_is_comp(state->active_player)) { + if (match_player_is_comp(state->active_player)) { l->rem += l->visits[--l->n_visits].points; ++state->boat_undone; } else { @@ -217,25 +196,12 @@ static void undo_active() update_player_avg(state->active_player, 0); update_player_rem(state->active_player, l->rem); - state->active_player = prev_throw_player(); -} - -static bool first_user_has_thrown() { - for (int i = 0, np = match_num_players(); i < np; ++i) { - int pn = match_opts->throws_first + i; - if (pn > np) - pn -= np; - - if (!player_is_comp(pn)) - return !!state->legs[pn - 1]->n_visits; - } - - return false; + state->active_player = match_prev_throw_player(); } void user_undo() { - if (!first_user_has_thrown()) { + if (!match_first_user_has_thrown()) { oi(); return; } @@ -251,8 +217,8 @@ void user_undo() return; } - state->active_player = prev_throw_player(); - while (player_is_comp(state->active_player)) + state->active_player = match_prev_throw_player(); + while (match_player_is_comp(state->active_player)) undo_active(); undo_active(); diff --git a/web/web_match.c b/web/web_match.c index 432808d..aabea4d 100644 --- a/web/web_match.c +++ b/web/web_match.c @@ -24,6 +24,11 @@ void match_opts_free() match_opts = NULL; } +struct leg *state_active_leg() +{ + return state->legs[state->active_player - 1]; +} + int match_num_players() { if (state->mode == M_P) @@ -47,7 +52,45 @@ bool match_is_over() return match_winning_player() != -1; } -struct leg *state_active_leg() +int match_prev_player() { - return state->legs[state->active_player - 1]; + if (state->active_player == 1) + return match_num_players(); + else + return state->active_player - 1; +} + +int match_next_player() +{ + if (state->active_player == match_num_players()) + return 1; + else + return state->active_player + 1; +} + +int match_prev_throw_player() +{ + if (match_is_over()) + return state->active_player; + + return match_prev_player(); +} + +bool match_player_is_comp(int pn) +{ + return state->mode == M_PVC && pn == 2; +} + +bool match_first_user_has_thrown() +{ + for (int i = 0, np = match_num_players(); i < np; ++i) { + int pn = match_opts->throws_first + i; + if (pn > np) + pn -= np; + + if (!match_player_is_comp(pn)) + return !!state->legs[pn - 1]->n_visits; + } + + return false; } diff --git a/web/web_match.h b/web/web_match.h index 8f106fb..ae5eff0 100644 --- a/web/web_match.h +++ b/web/web_match.h @@ -32,10 +32,15 @@ extern struct match_opts *match_opts; void free_state(); void match_opts_free(); +struct leg *state_active_leg(); + int match_num_players(); int match_winning_player(); bool match_is_over(); - -struct leg *state_active_leg(); +int match_prev_player(); +int match_next_player(); +int match_prev_throw_player(); +bool match_player_is_comp(int pn); +bool match_first_user_has_thrown(); #endif |