From 15ff00e056a98ab4210dde3d4461a88849d6c1d4 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Mon, 23 May 2022 15:23:48 -0700 Subject: move a bunch of general match logic from web to lib --- dartboat.c | 59 +++++++++++++++++++++++++++++++++++++---------------------- 1 file changed, 37 insertions(+), 22 deletions(-) (limited to 'dartboat.c') diff --git a/dartboat.c b/dartboat.c index c265031..7061b9a 100644 --- a/dartboat.c +++ b/dartboat.c @@ -11,7 +11,7 @@ void test_match(int start_points) { - struct leg *l = leg_init(start_points, NULL); + struct leg *l = leg_init(start_points); while (l->rem > 0) comp_visit(l); @@ -32,14 +32,14 @@ void test_match(int start_points) leg_free(l); } -void user_visit(struct leg *l) +void user_visit(struct leg *l, char *name) { if (l->n_visits == l->size_visits) leg_grow_visits(l); struct visit *v = l->visits + l->n_visits++; char status[100]; - int len = sprintf(status, " %s has %d remaining", l->name, l->rem); + int len = sprintf(status, " %s has %d remaining", name, l->rem); if (l->rem <= 170) { char *target = CHECKOUTS[2][l->rem-1]; if (target) { @@ -89,52 +89,67 @@ void user_visit(struct leg *l) v->rem = l->rem; } -void curses_match(int start_points, char *n1, void (*f1)(struct leg *), - char *n2, void (*f2)(struct leg *)) +static void player_visit(struct match *m, int pn) +{ + if (m->players[pn - 1].type == PT_USER) + user_visit(m->legs[pn - 1], m->players[pn - 1].name); + else + comp_visit(m->legs[pn - 1]); +} + +void curses_match(struct match *m) { init_curses(); - struct leg *l1 = leg_init(start_points, n1); - struct leg *l2 = leg_init(start_points, n2); + struct leg *l1 = m->legs[0]; + struct leg *l2 = m->legs[1]; curses_draw(l1, l2); while (l1->rem > 0 && l2->rem > 0) { - (*f1)(l1); + player_visit(m, 1); curses_draw(l1, l2); - if (l1->rem > 0) - (*f2)(l2); - - curses_draw(l1, l2); + if (l1->rem > 0) { + player_visit(m, 2); + curses_draw(l1, l2); + } } char status[100]; - sprintf(status, " %s wins", l1->rem <= 0 ? l1->name : l2->name); + sprintf(status, " %s wins", + m->players[match_winning_player(m) - 1].name); curses_status(status); - leg_free(l1); - leg_free(l2); wgetch(w); free_curses(); } void cvc_curses_match(int start_points) { - curses_match(start_points, "Dartboat 1", comp_visit, - "Dartboat 2", comp_visit); + struct match *m = match_init(); + match_add_player(m, PT_COMP, "Computer 1", start_points); + match_add_player(m, PT_COMP, "Computer 2", start_points); + curses_match(m); + match_free(m); } void pvc_curses_match(int start_points) { - curses_match(start_points, "David", user_visit, - "Dartboat", comp_visit); + struct match *m = match_init(); + match_add_player(m, PT_USER, "Player", start_points); + match_add_player(m, PT_COMP, "Computer", start_points); + curses_match(m); + match_free(m); } void pvp_curses_match(int start_points) { - curses_match(start_points, "David", user_visit, - "Davidn't", user_visit); + struct match *m = match_init(); + match_add_player(m, PT_USER, "Player 1", start_points); + match_add_player(m, PT_USER, "Player 2", start_points); + curses_match(m); + match_free(m); } void test_averages() @@ -146,7 +161,7 @@ void test_averages() int darts = 0; for (int i = 0; i < rounds; ++i) { - struct leg *l = leg_init(501, NULL); + struct leg *l = leg_init(501); while (l->rem > 0) comp_visit(l); leg_free(l); -- cgit v1.2.3-70-g09d2