From c80e3057623d92f98720b24ace095b2fe806ac5b Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Fri, 8 Apr 2022 07:39:58 -0700 Subject: add two-player test match --- dartbot.c | 99 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 89 insertions(+), 10 deletions(-) diff --git a/dartbot.c b/dartbot.c index 86d307a..8289ac8 100644 --- a/dartbot.c +++ b/dartbot.c @@ -256,6 +256,7 @@ struct visit { }; struct leg { + int start; int rem; int n_visits; struct visit *visits; @@ -264,7 +265,7 @@ struct leg { struct leg *leg_init(int points) { struct leg *l = calloc(1, sizeof(*l)); - l->rem = points; + l->start = l->rem = points; l->visits = calloc(1000, sizeof(*(l->visits))); // FIXME return l; @@ -308,14 +309,14 @@ void test_visit(struct leg *l) void test_match(int start_points) { struct leg *l = leg_init(start_points); - printf("(%2d) %3d\n", l->n_visits, l->rem); while(l->rem > 0) test_visit(l); + printf("(%2d) %3d\n", l->n_visits, l->start); for(int i = 0; i < l->n_visits; ++i) { struct visit *v = l->visits + i; - printf("(%2d) %3d", i+1, v->rem); + printf("(%2d) %3d %3d", i+1, v->points, v->rem); for (int j = 0; j < v->n_darts; ++j) { char *n = segment_name(v->darts[j]); @@ -328,7 +329,7 @@ void test_match(int start_points) leg_free(l); } -void draw(WINDOW *w, struct leg *l, int start_points) +void draw(WINDOW *w, struct leg *l) { werase(w); box(w, 0, 0); @@ -342,7 +343,7 @@ void draw(WINDOW *w, struct leg *l, int start_points) int start_visit = (LINES - 3 > l->n_visits) ? 0 : (l->n_visits - (LINES - 3)); if (start_visit == 0 && LINES - 3 > l->n_visits) { wmove(w, (offset++)-start_visit, 2); - sprintf(buf, "(%2d) %3d", 0, start_points); + sprintf(buf, "(%2d) %3d", 0, l->start); waddstr(w, buf); } @@ -350,7 +351,7 @@ void draw(WINDOW *w, struct leg *l, int start_points) wmove(w, offset+i-start_visit, 2); struct visit *v = l->visits + i; - buflen += sprintf(buf, "(%2d) %3d", i+1, v->rem); + buflen += sprintf(buf, "(%2d) %3d %3d", i+1, v->points, v->rem); for (int j = 0; j < v->n_darts; ++j) { char *n = segment_name(v->darts[j]); @@ -375,14 +376,13 @@ void test_curses_match(int start_points) struct leg *l = leg_init(start_points); - draw(w, l, start_points); - wrefresh(w); + draw(w, l); - while(l->rem > 0) { + while (l->rem > 0) { test_visit(l); usleep(250000); - draw(w, l, start_points); + draw(w, l); } leg_free(l); @@ -391,6 +391,84 @@ void test_curses_match(int start_points) endwin(); } +void draw2(WINDOW *w, struct leg *l1, struct leg *l2) +{ + werase(w); + box(w, 0, 0); + wmove(w, 0, 2); + waddstr(w, "dartbot"); + + char buf[100]; + int buflen = 0; + + int n_visits = l1->n_visits > l2->n_visits ? l1->n_visits : l2->n_visits; + + int offset = 2; + int start_visit = (LINES - 3 > n_visits) ? 0 : (n_visits - (LINES - 3)); + if (start_visit == 0 && LINES - 3 > n_visits) { + wmove(w, (offset++)-start_visit, 2); + sprintf(buf, "(%2d) %3d", 0, l1->start); + waddstr(w, buf); + } + + for (int i = start_visit; i < n_visits; ++i) { + wmove(w, offset+i-start_visit, 2); + buflen += sprintf(buf, "(%2d)", i+1); + + struct visit *v; + + if (i < l1->n_visits) { + v = l1->visits + i; + buflen += sprintf(buf + buflen, " %3d %3d", v->points, v->rem); + } + + if (i < l2->n_visits) { + v = l2->visits + i; + buflen += sprintf(buf + buflen, " %3d %3d", v->rem, v->points); + + for (int j = 0; j < v->n_darts; ++j) { + char *n = segment_name(v->darts[j]); + buflen += sprintf(buf + buflen, j == 0 ? " %4s" :" %4s", n); + free(n); + } + } + + buflen = 0; + waddstr(w, buf); + } + + wrefresh(w); +} + +void test_curses_match2(int start_points) +{ + initscr(); + curs_set(0); + noecho(); + + WINDOW *w = newwin(LINES, COLS, 0, 0); + + struct leg *l1 = leg_init(start_points); + struct leg *l2 = leg_init(start_points); + + draw2(w, l1, l2); + + while (l1->rem > 0 && l2->rem > 0) { + test_visit(l1); + if (l1->rem > 0) + test_visit(l2); + + usleep(250000); + draw2(w, l1, l2); + } + + leg_free(l1); + leg_free(l2); + wgetch(w); + delwin(w); + endwin(); +} + int main() { srand(time(NULL)); @@ -398,6 +476,7 @@ int main() init_centre_dists(); test_curses_match(501); + test_curses_match2(501); test_match(501); return 0; -- cgit v1.2.3-70-g09d2