#include "checkouts.h" #include #include #include #include #include #include #define HORIZONTAL_STDEV 24 #define VERTICAL_STDEV 24 // board spec from WDF rules #define WIRE_WIDTH 1.56 #define INNER_DIAMETER_BULL 12.7 #define INNER_DIAMETER_25 31.8 #define DOUBLE_OUTER_EDGE 170.0 #define TREBLE_OUTER_EDGE 107.4 #define DOUBLE_INSIDE_WIDTH 8.0 #define TREBLE_INSIDE_WIDTH 8.0 #define SEGMENT_MAX_LEN 5 // distance from centre to apex of outer wire // must be ordered outwards from centre #define FOREACH_RING(M) \ M(BULL, INNER_DIAMETER_BULL/2 + WIRE_WIDTH/2) \ M(25, INNER_DIAMETER_25/2 + WIRE_WIDTH/2) \ M(SMALL, TREBLE_OUTER_EDGE - WIRE_WIDTH - TREBLE_INSIDE_WIDTH - \ WIRE_WIDTH/2) \ M(TREBLE, TREBLE_OUTER_EDGE - WIRE_WIDTH/2) \ M(BIG, DOUBLE_OUTER_EDGE - WIRE_WIDTH - DOUBLE_INSIDE_WIDTH - \ WIRE_WIDTH/2) \ M(DOUBLE, DOUBLE_OUTER_EDGE - WIRE_WIDTH/2) #define GEN_RING_ENUM(X, D) R_ ## X, #define GEN_RING_STRING(X, D) #X, #define GEN_RING_OUTER_DIST(X, D) D, int SECTORS[] = { 20, 1, 18, 4, 13, 6, 10, 15, 2, 17, 3, 19, 7, 16, 8, 11, 14, 9, 12, 5 }; #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 < NUM_SECTORS; ++i) { SECTOR_INDS[SECTORS[i]-1] = i; double angle = 90 - (i * SECTOR_WIDTH); if (angle < 0) angle += 360; SECTOR_ANGLES[i] = angle; } } enum ring { FOREACH_RING(GEN_RING_ENUM) R_OUT }; char *RING_NAMES[] = { FOREACH_RING(GEN_RING_STRING) "OUT" }; double OUTER_DISTS[] = { FOREACH_RING(GEN_RING_OUTER_DIST) }; double CENTRE_DISTS[sizeof(OUTER_DISTS)/sizeof(*OUTER_DISTS)]; void init_centre_dists() { CENTRE_DISTS[0] = 0; for (int i = R_25; i < R_OUT; ++i) CENTRE_DISTS[i] = (OUTER_DISTS[i] - ((OUTER_DISTS[i] - OUTER_DISTS[i-1]) / 2)); } struct ccoords { double x, y; }; struct pcoords { double a, r; }; struct segment { enum ring ring; int sector; }; int get_sector(double angle) { double shifted = angle - 90 - (SECTOR_WIDTH/2); return SECTORS[NUM_SECTORS - 1 - (int)((shifted - (360 * floor(shifted/360))) / SECTOR_WIDTH)]; } enum ring get_ring(double radius) { for (int i = R_BULL; i < R_OUT; ++i) if (radius < OUTER_DISTS[i]) return i; return R_OUT; } double drand() { return (double)rand() / RAND_MAX; } double gauss(double mean, double stdev) { static bool have_next; static double next; double curr; if (have_next) { curr = next; } else { double theta = 2 * M_PI * drand(); double r = sqrt(-2 * log(1 - drand())); curr = r * cos(theta); next = r * sin(theta); } have_next = !have_next; return mean + (curr * stdev); } struct ccoords get_offset() { return (struct ccoords){ .x = gauss(0, HORIZONTAL_STDEV), .y = gauss(0, VERTICAL_STDEV) }; } struct ccoords pol_to_cart(struct pcoords c) { double t = c.a * (M_PI / 180); double x = c.r * cos(t); double y = c.r * sin(t); return (struct ccoords){ .x = x, .y = y }; } struct pcoords cart_to_pol(struct ccoords c) { double a = atan2(c.y, c.x) * (180 / M_PI); double r = sqrt(pow(c.x, 2) + pow(c.y, 2)); return (struct pcoords){ .a = a, .r = r }; } struct pcoords throw_dart(struct pcoords target) { struct ccoords cc = pol_to_cart(target); struct ccoords offset = get_offset(); return cart_to_pol((struct ccoords){ .x = cc.x + offset.x, .y = cc.y + offset.y }); } struct segment get_segment(struct pcoords c) { return (struct segment){ .sector = get_sector(c.a), .ring = get_ring(c.r) }; } char *segment_name(struct segment seg) { char *str = malloc(SEGMENT_MAX_LEN); #pragma GCC diagnostic push #pragma GCC diagnostic ignored "-Wstringop-truncation" if (seg.ring == R_BULL || seg.ring == R_25 || seg.ring == R_OUT) strncpy(str, RING_NAMES[seg.ring], SEGMENT_MAX_LEN); else snprintf(str, SEGMENT_MAX_LEN, (seg.ring == R_TREBLE ? "T%d" : (seg.ring == R_DOUBLE ? "D%d" : "%d")), seg.sector); #pragma GCC diagnostic pop return str; } int segment_points(struct segment seg) { if (seg.ring == R_BULL) return 50; else if (seg.ring == R_25) return 25; else if (seg.ring == R_TREBLE) return 3 * seg.sector; else if (seg.ring == R_DOUBLE) return 2 * seg.sector; else if (seg.ring == R_SMALL || seg.ring == R_BIG) return seg.sector; return 0; } bool segment_is_double(struct segment seg) { if (seg.ring == R_DOUBLE || seg.ring == R_BULL) return true; return false; } struct pcoords segment_centre(struct segment seg) { return (struct pcoords){ .a = SECTOR_ANGLES[SECTOR_INDS[seg.sector-1]], .r = CENTRE_DISTS[seg.ring] }; } struct segment segment_from_name(char *name) { int s = 20; enum ring r; if (!strcmp(name, "BULL")) { r = R_BULL; } else if (!strcmp(name, "25")) { r = R_25; } else if (name[0] == 'T') { r = R_TREBLE; s = atoi(name+1); } else if (name[0] == 'D') { r = R_DOUBLE; s = atoi(name+1); } else { r = R_BIG; s = atoi(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; 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, .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, .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 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); if (rem - v.points == 0 && segment_is_double(ds)) break; if (rem - v.points <= 1) { v.points = 0; break; } } return v; } 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); free(n); } printf("\n"); } } int main() { /* initscr(); addstr("test"); refresh(); getch(); endwin(); */ srand(time(NULL)); init_sectors(); init_centre_dists(); test_match(501); return 0; }