diff options
author | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-04-06 02:34:42 -0700 |
---|---|---|
committer | David Vazgenovich Shakaryan <dvshakaryan@gmail.com> | 2022-04-06 02:34:42 -0700 |
commit | 10f7936b38592162bb3d8fac27f0f69214123ac5 (patch) | |
tree | e1f3868918358ad95f383f514c034674108a98fd /dartbot.c | |
parent | 2dfa6a526739932bba86a3b66d031e8106bd1bb3 (diff) | |
download | dartboat-10f7936b38592162bb3d8fac27f0f69214123ac5.tar.gz dartboat-10f7936b38592162bb3d8fac27f0f69214123ac5.tar.xz |
determine target using checkout table
Diffstat (limited to 'dartbot.c')
-rw-r--r-- | dartbot.c | 35 |
1 files changed, 25 insertions, 10 deletions
@@ -1,3 +1,5 @@ +#include "checkouts.h" + #include <math.h> #include <ncurses.h> #include <stdbool.h> @@ -35,13 +37,13 @@ int SECTORS[] = { 20, 1, 18, 4, 13, 6, 10, 15, 2, 17, 3, 19, 7, 16, 8, 11, 14, 9, 12, 5 }; -#define LEN_SECTORS (sizeof(SECTORS) / sizeof(*SECTORS)) -#define SECTOR_WIDTH (360.0 / LEN_SECTORS) -int SECTOR_INDS[LEN_SECTORS]; -double SECTOR_ANGLES[LEN_SECTORS]; +#define NUM_SECTORS (sizeof(SECTORS) / sizeof(*SECTORS)) +#define SECTOR_WIDTH (360.0 / NUM_SECTORS) +int SECTOR_INDS[NUM_SECTORS]; +double SECTOR_ANGLES[NUM_SECTORS]; void init_sectors() { - for (size_t i = 0; i < LEN_SECTORS; ++i) { + for (size_t i = 0; i < NUM_SECTORS; ++i) { SECTOR_INDS[SECTORS[i]-1] = i; double angle = 90 - (i * SECTOR_WIDTH); @@ -89,7 +91,7 @@ struct segment { int get_sector(double angle) { double shifted = angle - 90 - (SECTOR_WIDTH/2); - return SECTORS[LEN_SECTORS - 1 - + return SECTORS[NUM_SECTORS - 1 - (int)((shifted - (360 * floor(shifted/360))) / SECTOR_WIDTH)]; } @@ -237,16 +239,27 @@ struct segment segment_from_name(char *name) struct visit { int points; - int darts_thrown; + int n_darts; struct segment *darts; }; +struct segment next_dart(int rem, int darts_in_hand) +{ + char *c = NULL; + if (rem <= 170) + c = CHECKOUTS[darts_in_hand-1][rem-1]; + if (!c) c = "T20"; + + return segment_from_name(c); +} + struct visit test_visit(int rem) { - struct visit v = { .points = 0, .darts_thrown = 0, + struct visit v = { .points = 0, .n_darts = 0, .darts = calloc(3, sizeof(struct segment)) }; 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, @@ -255,12 +268,14 @@ struct visit test_visit(int rem) ts = segment_from_name("1"); else ts = segment_from_name("T20"); + */ + struct segment ts = next_dart(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.darts_thrown; + ++v.n_darts; v.points += segment_points(ds); if (rem - v.points == 0 && segment_is_double(ds)) @@ -287,7 +302,7 @@ void test_match(int start_points) rem -= v.points; printf("(%2d) %3d", visits, rem); - for (int i = 0; i < v.darts_thrown; ++i) { + for (int i = 0; i < v.n_darts; ++i) { char *n = segment_name(v.darts[i]); printf(i == 0 ? " %4s" :" %4s", n); free(n); |