diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-05-23 23:15:34 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-05-23 23:15:34 -0700 |
commit | 85c101979a181a76e6250fb1abb75216e08384a3 (patch) | |
tree | 00985c1d0823581376672107eab5345a31ff0443 /match.c | |
parent | ee146e45b9ccc7f3b5dfd34973df538a42772b84 (diff) | |
download | dartboat-85c101979a181a76e6250fb1abb75216e08384a3.tar.gz dartboat-85c101979a181a76e6250fb1abb75216e08384a3.tar.xz |
move more match logic from web to lib
Diffstat (limited to 'match.c')
-rw-r--r-- | match.c | 50 |
1 files changed, 46 insertions, 4 deletions
@@ -1,14 +1,15 @@ +#include "comp.h" #include "match.h" #include <stdbool.h> #include <stdlib.h> #include <string.h> -struct leg *leg_init(int points) +struct leg *leg_init(int pts) { struct leg *l = calloc(1, sizeof(*l)); - l->start = l->rem = points; + l->start = l->rem = pts; l->size_visits = 16; l->visits = calloc(l->size_visits, sizeof(*(l->visits))); @@ -36,6 +37,33 @@ void leg_grow_visits(struct leg *l) memset((char *)l->visits + bytes, 0, bytes); } +struct visit *leg_comp_visit(struct leg *l, bool redo_undone) +{ + // FIXME redo_undone=false should free next undone + if (redo_undone && l->undone_visits) + leg_redo_visit(l); + else + comp_visit(l); + + struct visit *v = l->visits + l->n_visits - 1; + return v; +} + +struct visit *leg_pts_visit(struct leg *l, int pts) +{ + if (!is_points_valid(pts, l->rem)) + return NULL; + + if (l->n_visits == l->size_visits) + leg_grow_visits(l); + struct visit *v = l->visits + l->n_visits++; + v->points = pts; + l->rem -= pts; + v->rem = l->rem; + + return v; +} + void leg_undo_visit(struct leg *l) { l->rem += l->visits[--l->n_visits].points; @@ -115,8 +143,8 @@ int match_prev_player(struct match *m) int match_prev_throw_player(struct match *m) { - int p = match_winning_player(m); - return p ? p : match_prev_player(m); + int pn = match_winning_player(m); + return pn ? pn : match_prev_player(m); } int match_winning_player(struct match *m) @@ -129,6 +157,20 @@ int match_winning_player(struct match *m) return 0; } +bool match_first_user_has_thrown(struct match *m) +{ + for (int i = 0; i < m->n_players; ++i) { + int pn = m->starting_player + i; + if (pn > m->n_players) + pn -= m->n_players; + + if (m->players[pn - 1].type == PT_USER) + return !!m->legs[pn - 1]->n_visits; + } + + return false; +} + bool is_points_valid(int pts, int rem) { return pts <= rem && |