From 357ba62d2ca8257ab171c04ffabe6ea5001e1123 Mon Sep 17 00:00:00 2001 From: David Vazgenovich Shakaryan Date: Fri, 8 Apr 2022 04:26:34 -0700 Subject: store data for entire leg in a struct --- dartbot.c | 149 +++++++++++++++++++++++++++++++------------------------------- 1 file changed, 74 insertions(+), 75 deletions(-) diff --git a/dartbot.c b/dartbot.c index bb4f16f..86d307a 100644 --- a/dartbot.c +++ b/dartbot.c @@ -238,12 +238,6 @@ struct segment segment_from_name(char *name) return (struct segment){ .sector = s, .ring = r }; } -struct visit { - int points; - int n_darts; - struct segment *darts; -}; - struct segment next_dart(int rem, int darts_in_hand) { char *c = NULL; @@ -254,99 +248,112 @@ struct segment next_dart(int rem, int darts_in_hand) return segment_from_name(c); } -struct visit test_visit(int rem) +struct visit { + int points; + int rem; + int n_darts; + struct segment *darts; +}; + +struct leg { + int rem; + int n_visits; + struct visit *visits; +}; + +struct leg *leg_init(int points) +{ + struct leg *l = calloc(1, sizeof(*l)); + l->rem = points; + l->visits = calloc(1000, sizeof(*(l->visits))); // FIXME + + return l; +} + +void leg_free(struct leg *l) +{ + for (int i = 0; i < l->n_visits; ++i) + free(l->visits[i].darts); + free(l->visits); + free(l); +} + +void test_visit(struct leg *l) { - struct visit v = { .points = 0, .n_darts = 0, - .darts = calloc(3, sizeof(struct segment)) }; + struct visit *v = l->visits + l->n_visits++; + v->darts = calloc(3, sizeof(*(v->darts))); for (int i = 0; i < 3; ++i) { - /* - struct segment ts; - if (rem - v.points <= 40 && (rem - v.points) % 2 == 0) - ts = (struct segment){ .ring = R_DOUBLE, - .sector = (rem - v.points) / 2 }; - else if (rem - v.points <= 40 && (rem - v.points) % 2 == 1) - ts = segment_from_name("1"); - else - ts = segment_from_name("T20"); - */ - struct segment ts = next_dart(rem - v.points, 3 - i); + struct segment ts = next_dart(l->rem - v->points, 3 - i); struct pcoords tc = segment_centre(ts); struct pcoords dc = throw_dart(tc); struct segment ds = get_segment(dc); - v.darts[i] = ds; - ++v.n_darts; - v.points += segment_points(ds); + v->darts[v->n_darts++] = ds; + v->points += segment_points(ds); - if (rem - v.points == 0 && segment_is_double(ds)) + if (l->rem - v->points == 0 && segment_is_double(ds)) break; - if (rem - v.points <= 1) { - v.points = 0; + if (l->rem - v->points <= 1) { + v->points = 0; break; } } - return v; + l->rem -= v->points; + v->rem = l->rem; } void test_match(int start_points) { - printf("(%2d) %3d\n", 0, start_points); - int visits = 0; - - int rem = start_points; - while(rem > 0) { - ++visits; - struct visit v = test_visit(rem); - rem -= v.points; - - printf("(%2d) %3d", visits, rem); - for (int i = 0; i < v.n_darts; ++i) { - char *n = segment_name(v.darts[i]); - printf(i == 0 ? " %4s" :" %4s", n); + struct leg *l = leg_init(start_points); + printf("(%2d) %3d\n", l->n_visits, l->rem); + + while(l->rem > 0) + test_visit(l); + + for(int i = 0; i < l->n_visits; ++i) { + struct visit *v = l->visits + i; + printf("(%2d) %3d", i+1, v->rem); + + for (int j = 0; j < v->n_darts; ++j) { + char *n = segment_name(v->darts[j]); + printf(j == 0 ? " %4s" :" %4s", n); free(n); } printf("\n"); - - free(v.darts); } + + leg_free(l); } -void draw(WINDOW *w, struct visit *visits, int n_visits) +void draw(WINDOW *w, struct leg *l, int start_points) { werase(w); box(w, 0, 0); wmove(w, 0, 2); waddstr(w, "dartbot"); - int rem = 501; char buf[100]; int buflen = 0; 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, rem); + 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); waddstr(w, buf); - offset = 3; } - for (int i = 0; i < n_visits; ++i) { - struct visit v = visits[i]; - rem -= v.points; - - if (i < start_visit) - continue; - + for (int i = start_visit; i < l->n_visits; ++i) { wmove(w, offset+i-start_visit, 2); + struct visit *v = l->visits + i; - buflen += sprintf(buf, "(%2d) %3d", i+1, rem); + buflen += sprintf(buf, "(%2d) %3d", i+1, v->rem); - for (int j = 0; j < v.n_darts; ++j) { - char *n = segment_name(v.darts[j]); + 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); } @@ -358,7 +365,7 @@ void draw(WINDOW *w, struct visit *visits, int n_visits) wrefresh(w); } -void test_curses_match() +void test_curses_match(int start_points) { initscr(); curs_set(0); @@ -366,27 +373,19 @@ void test_curses_match() WINDOW *w = newwin(LINES, COLS, 0, 0); - int rem = 501; - printf("(%2d) %3d\n", 0, rem); - struct visit *visits = calloc(1000, sizeof(*visits)); // FIXME - int n_visits = 0; + struct leg *l = leg_init(start_points); - draw(w, visits, n_visits); + draw(w, l, start_points); wrefresh(w); - while(rem > 0) { - struct visit v = test_visit(rem); - visits[n_visits] = v; - ++n_visits; - rem -= v.points; + while(l->rem > 0) { + test_visit(l); usleep(250000); - draw(w, visits, n_visits); + draw(w, l, start_points); } - for (int i = 0; i < n_visits; ++i) - free(visits[i].darts); - free(visits); + leg_free(l); wgetch(w); delwin(w); endwin(); @@ -398,7 +397,7 @@ int main() init_sectors(); init_centre_dists(); - test_curses_match(); + test_curses_match(501); test_match(501); return 0; -- cgit v1.2.3-70-g09d2