From 3ffb56f167a1855731b50eebfc484b7cc5f21036 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Tue, 12 Apr 2022 04:57:01 -0700 Subject: add player names --- dartbot.c | 29 +++++++++++++++-------------- 1 file changed, 15 insertions(+), 14 deletions(-) diff --git a/dartbot.c b/dartbot.c index 1465128..abd6045 100644 --- a/dartbot.c +++ b/dartbot.c @@ -281,15 +281,17 @@ struct visit { }; struct leg { + char *name; int start; int rem; int n_visits; struct visit *visits; }; -struct leg *leg_init(int points) +struct leg *leg_init(int points, char *name) { struct leg *l = calloc(1, sizeof(*l)); + l->name = name; l->start = l->rem = points; l->visits = calloc(1000, sizeof(*(l->visits))); // FIXME @@ -334,7 +336,7 @@ void comp_visit(struct leg *l) void test_match(int start_points) { - struct leg *l = leg_init(start_points); + struct leg *l = leg_init(start_points, NULL); while(l->rem > 0) comp_visit(l); @@ -376,7 +378,7 @@ void user_visit(struct leg *l) struct visit *v = l->visits + l->n_visits++; char status[100]; - int len = sprintf(status, " %d remaining", l->rem); + int len = sprintf(status, " %s has %d remaining", l->name, l->rem); if (l->rem <= 170) { char *target = CHECKOUTS[2][l->rem-1]; if (target) { @@ -512,8 +514,8 @@ void curses_draw(struct leg *l1, struct leg *l2) wrefresh(w); } -void curses_match(int start_points, void (*f1)(struct leg *), - void (*f2)(struct leg *)) +void curses_match(int start_points, char *n1, void (*f1)(struct leg *), + char *n2, void (*f2)(struct leg *)) { initscr(); refresh(); @@ -531,8 +533,8 @@ void curses_match(int start_points, void (*f1)(struct leg *), waddstr(titlew, " dartbot"); wrefresh(titlew); - struct leg *l1 = leg_init(start_points); - struct leg *l2 = leg_init(start_points); + struct leg *l1 = leg_init(start_points, n1); + struct leg *l2 = leg_init(start_points, n2); curses_draw(l1, l2); @@ -546,10 +548,9 @@ void curses_match(int start_points, void (*f1)(struct leg *), curses_draw(l1, l2); } - if (l1->rem <= 0) - curses_status(" Player 1 wins."); - else if (l2->rem <= 0) - curses_status(" Player 2 wins."); + char status[100]; + sprintf(status, " %s wins", l1->rem <= 0 ? l1->name : l2->name); + curses_status(status); leg_free(l1); leg_free(l2); @@ -563,17 +564,17 @@ void curses_match(int start_points, void (*f1)(struct leg *), void cvc_curses_match(int start_points) { - curses_match(start_points, comp_visit, comp_visit); + curses_match(start_points, "Dartbot 1", comp_visit, "Dartbot 2", comp_visit); } void pvc_curses_match(int start_points) { - curses_match(start_points, user_visit, comp_visit); + curses_match(start_points, "David", user_visit, "Dartbot", comp_visit); } void pvp_curses_match(int start_points) { - curses_match(start_points, user_visit, user_visit); + curses_match(start_points, "David", user_visit, "Davidn't", user_visit); } int main() -- cgit v1.2.3-70-g09d2